FansService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\wechat\service;
  3. use think\admin\Service;
  4. /**
  5. * 微信粉丝信息
  6. * Class FansService
  7. * @package app\wechat\service
  8. */
  9. class FansService extends Service
  10. {
  11. /**
  12. * 增加或更新粉丝信息
  13. * @param array $user 粉丝信息
  14. * @param string $appid 微信APPID
  15. * @return boolean
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\DbException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. */
  20. public function set(array $user, string $appid = ''): bool
  21. {
  22. if (isset($user['subscribe_time'])) {
  23. $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
  24. }
  25. if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
  26. $user['tagid_list'] = arr2str($user['tagid_list'] ?? []);
  27. }
  28. if ($appid !== '') $user['appid'] = $appid;
  29. unset($user['privilege'], $user['groupid']);
  30. $this->app->event->trigger('WechatFansUpdate', $user);
  31. return !!data_save('WechatFans', $user, 'openid');
  32. }
  33. /**
  34. * 获取粉丝信息
  35. * @param string $openid
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function get(string $openid): array
  42. {
  43. return $this->app->db->name('WechatFans')->where(['openid' => $openid])->find() ?: [];
  44. }
  45. }