Fans.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $this->setQueueSuccess($this->_list() . $this->_tags() . $this->_black());
  48. }
  49. /**
  50. * 同步微信粉丝列表
  51. * @param string $next
  52. * @param integer $done
  53. * @return string
  54. * @throws \WeChat\Exceptions\InvalidResponseException
  55. * @throws \WeChat\Exceptions\LocalCacheException
  56. * @throws \think\admin\Exception
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. protected function _list(string $next = '', int $done = 0): string
  62. {
  63. $appid = WechatService::getAppid();
  64. $this->output->comment('开始获取微信用户数据');
  65. while (is_string($next)) {
  66. $result = WechatService::WeChatUser()->getUserList($next);
  67. if (is_array($result) && !empty($result['data']['openid'])) {
  68. foreach (array_chunk($result['data']['openid'], 100) as $openids) {
  69. $info = WechatService::WeChatUser()->getBatchUserInfo($openids);
  70. if (is_array($info) && !empty($info['user_info_list'])) {
  71. foreach ($info['user_info_list'] as $user) if (isset($user['nickname'])) {
  72. $this->queue->message($result['total'], ++$done, "-> {$user['openid']} {$user['nickname']}");
  73. FansService::set($user, $appid);
  74. }
  75. }
  76. }
  77. $next = $result['total'] > $done ? $result['next_openid'] : null;
  78. } else {
  79. $next = null;
  80. }
  81. }
  82. $this->output->comment($done > 0 ? '微信用户数据获取完成' : '未获取到微信用户数据');
  83. $this->output->newLine();
  84. return "共获取 {$done} 个用户数据";
  85. }
  86. /**
  87. * 同步粉丝黑名单列表
  88. * @param string $next
  89. * @param integer $done
  90. * @return string
  91. * @throws \WeChat\Exceptions\InvalidResponseException
  92. * @throws \WeChat\Exceptions\LocalCacheException
  93. */
  94. public function _black(string $next = '', int $done = 0): string
  95. {
  96. $wechat = WechatService::WeChatUser();
  97. $this->setQueueProgress("开始更新黑名单列表");
  98. // 清理原来的黑名单,重新批量更新黑名单列表
  99. WechatFans::mk()->where(['is_black' => 1])->update(['is_black' => 0]);
  100. $result = ['total' => 0];
  101. while (!is_null($next) && is_array($result = $wechat->getBlackList($next))) {
  102. if (empty($result['data']['openid'])) break;
  103. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  104. $done += count($chunk);
  105. WechatFans::mk()->whereIn('openid', $chunk)->update(['is_black' => 1]);
  106. }
  107. $next = $result['total'] > $done ? $result['next_openid'] : null;
  108. }
  109. $this->setQueueProgress("完成更新 {$result['total']} 个黑名单", null, 1);
  110. $this->output->newLine();
  111. if (empty($result['total'])) {
  112. return ', 其中黑名单 0 人';
  113. } else {
  114. return ", 其中黑名单 {$result['total']} 人";
  115. }
  116. }
  117. /**
  118. * 同步粉丝标签列表
  119. * @param integer $done
  120. * @return string
  121. * @throws \WeChat\Exceptions\InvalidResponseException
  122. * @throws \WeChat\Exceptions\LocalCacheException
  123. * @throws \think\admin\Exception
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function _tags(int $done = 0): string
  129. {
  130. $appid = WechatService::getAppid();
  131. $this->output->comment('开始获取微信用户标签数据');
  132. if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
  133. $count = count($list['tags']);
  134. foreach ($list['tags'] as &$tag) {
  135. $tag['appid'] = $appid;
  136. $this->queue->message($count, ++$done, "-> {$tag['name']}");
  137. }
  138. WechatFansTags::mk()->where(['appid' => $appid])->delete();
  139. WechatFansTags::mk()->insertAll($list['tags']);
  140. }
  141. $this->output->comment($done > 0 ? '微信用户标签数据获取完成' : '未获取到微信用户标签数据');
  142. $this->output->newLine();
  143. return ", 获取到 {$done} 个标签";
  144. }
  145. }