FansService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\service;
  14. use think\Db;
  15. /**
  16. * 微信粉丝信息
  17. * Class FansService
  18. * @package app\wechat\service
  19. */
  20. class FansService
  21. {
  22. /**
  23. * 增加或更新粉丝信息
  24. * @param array $user 粉丝信息
  25. * @param string $appid 微信APPID
  26. * @return boolean
  27. * @throws \think\Exception
  28. * @throws \think\exception\PDOException
  29. */
  30. public static function set(array $user, $appid = '')
  31. {
  32. if (!empty($user['subscribe_time'])) {
  33. $user['subscribe_at'] = date('Y-m-d H:i:s', $user['subscribe_time']);
  34. }
  35. if (isset($user['tagid_list']) && is_array($user['tagid_list'])) {
  36. $user['tagid_list'] = is_array($user['tagid_list']) ? join(',', $user['tagid_list']) : '';
  37. }
  38. foreach (['country', 'province', 'city', 'nickname', 'remark'] as $k) {
  39. isset($user[$k]) && $user[$k] = emoji_encode($user[$k]);
  40. }
  41. if ($appid !== '') $user['appid'] = $appid;
  42. unset($user['privilege'], $user['groupid']);
  43. return data_save('WechatFans', $user, 'openid');
  44. }
  45. /**
  46. * 获取粉丝信息
  47. * @param string $openid
  48. * @return array|null
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. public static function get($openid)
  54. {
  55. $user = Db::name('WechatFans')->where(['openid' => $openid])->find();
  56. foreach (['country', 'province', 'city', 'nickname', 'remark'] as $k) {
  57. isset($user[$k]) && $user[$k] = emoji_decode($user[$k]);
  58. }
  59. return $user;
  60. }
  61. }