Fans.php 5.4 KB

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