Fans.php 6.3 KB

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