Fans.php 6.1 KB

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