WechatQueue.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\wechat\queue;
  15. use app\wechat\service\FansService;
  16. use app\wechat\service\WechatService;
  17. use think\console\Input;
  18. use think\console\Output;
  19. use think\Db;
  20. /**
  21. * Class Jobs
  22. * @package app\wechat
  23. */
  24. class WechatQueue
  25. {
  26. /**
  27. * 当前类名
  28. * @var string
  29. */
  30. const URI = self::class;
  31. /**
  32. * 执行任务
  33. * @param Input $input
  34. * @param Output $output
  35. * @param array $data
  36. * @throws \WeChat\Exceptions\InvalidResponseException
  37. * @throws \WeChat\Exceptions\LocalCacheException
  38. * @throws \think\Exception
  39. * @throws \think\exception\PDOException
  40. */
  41. public function execute(Input $input, Output $output, array $data = [])
  42. {
  43. $appid = WechatService::getAppid();
  44. $wechat = WechatService::WeChatUser();
  45. // 获取远程粉丝
  46. list($next, $done) = ['', 0];
  47. while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
  48. $done += $result['count'];
  49. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  50. if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
  51. foreach ($list['user_info_list'] as $user) FansService::set($user, $appid);
  52. }
  53. }
  54. $next = $result['total'] > $done ? $result['next_openid'] : null;
  55. }
  56. // 同步粉丝黑名单
  57. list($next, $done) = ['', 0];
  58. while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
  59. $done += $result['count'];
  60. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  61. Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
  62. }
  63. $next = $result['total'] > $done ? $result['next_openid'] : null;
  64. }
  65. // 同步粉丝标签
  66. if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
  67. foreach ($list['tags'] as &$tag) $tag['appid'] = $appid;
  68. Db::name('WechatFansTags')->where('1=1')->delete();
  69. Db::name('WechatFansTags')->insertAll($list['tags']);
  70. }
  71. }
  72. }