Fans.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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\command;
  16. use app\wechat\model\WechatFans;
  17. use app\wechat\model\WechatFansTags;
  18. use app\wechat\service\FansService;
  19. use app\wechat\service\WechatService;
  20. use think\admin\Command;
  21. /**
  22. * 微信粉丝管理指令
  23. * Class Fans
  24. * @package app\wechat\command
  25. */
  26. class Fans extends Command
  27. {
  28. /**
  29. * 配置指令
  30. */
  31. protected function configure()
  32. {
  33. $this->setName('xadmin:fansall');
  34. $this->setDescription('Wechat Users Data Synchronize for ThinkAdmin');
  35. }
  36. /**
  37. * 任务执行处理
  38. * @throws \WeChat\Exceptions\InvalidResponseException
  39. * @throws \WeChat\Exceptions\LocalCacheException
  40. * @throws \think\admin\Exception
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function handle()
  46. {
  47. $message = $this->_list();
  48. $message .= $this->_tags();
  49. $message .= $this->_black();
  50. $this->setQueueSuccess($message);
  51. }
  52. /**
  53. * 同步微信粉丝列表
  54. * @param string $next
  55. * @param integer $done
  56. * @return string
  57. * @throws \WeChat\Exceptions\InvalidResponseException
  58. * @throws \WeChat\Exceptions\LocalCacheException
  59. * @throws \think\admin\Exception
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. protected function _list(string $next = '', int $done = 0): string
  65. {
  66. $appid = WechatService::instance()->getAppid();
  67. $this->output->comment('开始获取微信用户数据');
  68. while (is_string($next)) {
  69. $result = WechatService::WeChatUser()->getUserList($next);
  70. if (is_array($result) && !empty($result['data']['openid'])) {
  71. foreach (array_chunk($result['data']['openid'], 100) as $openids) {
  72. $info = WechatService::WeChatUser()->getBatchUserInfo($openids);
  73. if (is_array($info) && !empty($info['user_info_list'])) {
  74. foreach ($info['user_info_list'] as $user) if (isset($user['nickname'])) {
  75. $this->queue->message($result['total'], ++$done, "-> {$user['openid']} {$user['nickname']}");
  76. FansService::instance()->set($user, $appid);
  77. }
  78. }
  79. }
  80. $next = $result['total'] > $done ? $result['next_openid'] : null;
  81. } else {
  82. $next = null;
  83. }
  84. }
  85. $this->output->comment($done > 0 ? '微信用户数据获取完成' : '未获取到微信用户数据');
  86. $this->output->newLine();
  87. return "共获取 {$done} 个用户数据";
  88. }
  89. /**
  90. * 同步粉丝黑名单列表
  91. * @param string $next
  92. * @param integer $done
  93. * @return string
  94. * @throws \WeChat\Exceptions\InvalidResponseException
  95. * @throws \WeChat\Exceptions\LocalCacheException
  96. */
  97. public function _black(string $next = '', int $done = 0): string
  98. {
  99. $wechat = WechatService::WeChatUser();
  100. $this->setQueueProgress("开始更新黑名单列表");
  101. // 清理原来的黑名单,重新批量更新黑名单列表
  102. WechatFans::mk()->where(['is_black' => 1])->update(['is_black' => 0]);
  103. $result = ['total' => 0];
  104. while (!is_null($next) && is_array($result = $wechat->getBlackList($next))) {
  105. if (empty($result['data']['openid'])) break;
  106. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  107. $done += count($chunk);
  108. WechatFans::mk()->whereIn('openid', $chunk)->update(['is_black' => 1]);
  109. }
  110. $next = $result['total'] > $done ? $result['next_openid'] : null;
  111. }
  112. $this->setQueueProgress("完成更新 {$result['total']} 个黑名单", null, 1);
  113. $this->output->newLine();
  114. if (empty($result['total'])) {
  115. return ', 其中黑名单 0 人';
  116. } else {
  117. return ", 其中黑名单 {$result['total']} 人";
  118. }
  119. }
  120. /**
  121. * 同步粉丝标签列表
  122. * @param integer $done
  123. * @return string
  124. * @throws \WeChat\Exceptions\InvalidResponseException
  125. * @throws \WeChat\Exceptions\LocalCacheException
  126. * @throws \think\admin\Exception
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. public function _tags(int $done = 0): string
  132. {
  133. $appid = WechatService::instance()->getAppid();
  134. $this->output->comment('开始获取微信用户标签数据');
  135. if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
  136. $count = count($list['tags']);
  137. foreach ($list['tags'] as &$tag) {
  138. $tag['appid'] = $appid;
  139. $this->queue->message($count, ++$done, "-> {$tag['name']}");
  140. }
  141. WechatFansTags::mk()->where(['appid' => $appid])->delete();
  142. WechatFansTags::mk()->insertAll($list['tags']);
  143. }
  144. $this->output->comment($done > 0 ? '微信用户标签数据获取完成' : '未获取到微信用户标签数据');
  145. $this->output->newLine();
  146. return ", 获取到 {$done} 个标签";
  147. }
  148. }