WechatQueue.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\service\queue;
  15. use app\admin\queue\Queue;
  16. use app\service\service\WechatService;
  17. use app\wechat\service\FansService;
  18. use think\console\Input;
  19. use think\console\Output;
  20. use think\Db;
  21. /**
  22. * Class Jobs
  23. * @package app\wechat
  24. */
  25. class WechatQueue extends Queue
  26. {
  27. /**
  28. * 当前类名
  29. * @var string
  30. */
  31. const URI = self::class;
  32. /**
  33. * 当前操作APPID
  34. * @var string
  35. */
  36. protected $appid;
  37. /**
  38. * 执行任务
  39. * @param Input $input
  40. * @param Output $output
  41. * @param array $data
  42. * @throws \WeChat\Exceptions\InvalidResponseException
  43. * @throws \WeChat\Exceptions\LocalCacheException
  44. * @throws \think\Exception
  45. * @throws \think\exception\PDOException
  46. */
  47. public function execute(Input $input, Output $output, array $data = [])
  48. {
  49. $this->appid = $data['appid'];
  50. $wechat = WechatService::WeChatUser($this->appid);
  51. // 获取远程粉丝
  52. list($next, $done) = ['', 0];
  53. $output->writeln('Start synchronizing fans from the Wechat server');
  54. while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
  55. $done += $result['count'];
  56. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  57. if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
  58. foreach ($list['user_info_list'] as $user) FansService::set($user, $this->appid);
  59. }
  60. }
  61. $next = $result['total'] > $done ? $result['next_openid'] : null;
  62. }
  63. // 同步粉丝黑名单
  64. list($next, $done) = ['', 0];
  65. $output->writeln('Start synchronizing black from the Wechat server');
  66. while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
  67. $done += $result['count'];
  68. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  69. $where = [['is_black', 'eq', '0'], ['openid', 'in', $chunk]];
  70. Db::name('WechatFans')->where($where)->update(['is_black' => '1']);
  71. }
  72. $next = $result['total'] > $done ? $result['next_openid'] : null;
  73. }
  74. // 同步粉丝标签列表
  75. $output->writeln('Start synchronizing tags from the Wechat server');
  76. if (is_array($list = WechatService::WeChatTags($this->appid)->getTags()) && !empty($list['tags'])) {
  77. foreach ($list['tags'] as &$tag) $tag['appid'] = $this->appid;
  78. Db::name('WechatFansTags')->where('1=1')->delete();
  79. Db::name('WechatFansTags')->insertAll($list['tags']);
  80. }
  81. }
  82. }