FansService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ 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 think\admin\Service;
  17. /**
  18. * 微信粉丝信息
  19. * Class FansService
  20. * @package app\wechat\service
  21. */
  22. class FansService extends Service
  23. {
  24. /**
  25. * 增加或更新粉丝信息
  26. * @param array $user 粉丝信息
  27. * @param string $appid 微信APPID
  28. * @return boolean
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function set(array $user, string $appid = ''): bool
  34. {
  35. if (isset($user['subscribe_time'])) {
  36. $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
  37. }
  38. if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
  39. $user['tagid_list'] = arr2str($user['tagid_list'] ?? []);
  40. }
  41. if ($appid !== '') $user['appid'] = $appid;
  42. unset($user['privilege'], $user['groupid']);
  43. $this->app->event->trigger('WechatFansUpdate', $user);
  44. return !!data_save('WechatFans', $user, 'openid');
  45. }
  46. /**
  47. * 获取粉丝信息
  48. * @param string $openid
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function get(string $openid): array
  55. {
  56. return $this->app->db->name('WechatFans')->where(['openid' => $openid])->find() ?: [];
  57. }
  58. }