FansService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\service;
  16. use app\wechat\model\WechatFans;
  17. use think\admin\Service;
  18. /**
  19. * 微信粉丝信息
  20. * Class FansService
  21. * @package app\wechat\service
  22. */
  23. class FansService extends Service
  24. {
  25. /**
  26. * 增加或更新粉丝信息
  27. * @param array $user 粉丝信息
  28. * @param string $appid 微信APPID
  29. * @return boolean
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function set(array $user, string $appid = ''): bool
  35. {
  36. if (isset($user['subscribe_time'])) {
  37. $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
  38. }
  39. if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
  40. $user['tagid_list'] = arr2str($user['tagid_list'] ?? []);
  41. }
  42. if ($appid !== '') $user['appid'] = $appid;
  43. unset($user['privilege'], $user['groupid']);
  44. $this->app->event->trigger('WechatFansUpdate', $user);
  45. return !!data_save(WechatFans::class, $user, 'openid');
  46. }
  47. /**
  48. * 获取粉丝信息
  49. * @param string $openid
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function get(string $openid): array
  56. {
  57. $user = WechatFans::mk()->where(['openid' => $openid])->find();
  58. return empty($user) ? [] : $user->toArray();
  59. }
  60. }