Fans.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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\WechatService;
  16. use think\console\Command;
  17. use think\console\Input;
  18. use think\console\Output;
  19. use think\Db;
  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', 'tags', 'black'];
  32. /**
  33. * 执行指令
  34. * @param Input $input
  35. * @param Output $output
  36. * @return int|void|null
  37. */
  38. protected function execute(Input $input, Output $output)
  39. {
  40. foreach ($this->module as $m) {
  41. if (method_exists($this, $fun = "_{$m}")) $this->$fun();
  42. }
  43. }
  44. /**
  45. * 同步微信粉丝列表
  46. * @param string $next
  47. * @param integer $done
  48. * @throws \WeChat\Exceptions\InvalidResponseException
  49. * @throws \WeChat\Exceptions\LocalCacheException
  50. * @throws \think\Exception
  51. * @throws \think\exception\PDOException
  52. */
  53. protected function _list($next = '', $done = 0)
  54. {
  55. $appid = WechatService::getAppid();
  56. $wechat = WechatService::WeChatUser();
  57. $this->output->comment('开始同步微信粉丝数据 ...');
  58. while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
  59. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  60. if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
  61. foreach ($list['user_info_list'] as $user) {
  62. $indexString = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT);
  63. $this->output->writeln("({$indexString}/{$result['total']}) 正在更新粉丝 {$user['openid']} {$user['nickname']}");
  64. \app\wechat\service\FansService::set($user, $appid);
  65. }
  66. }
  67. }
  68. $next = $result['total'] > $done ? $result['next_openid'] : null;
  69. }
  70. $this->output->comment('微信粉丝数据同步完成');
  71. $this->output->newLine();
  72. }
  73. /**
  74. * 同步粉丝黑名单列表
  75. * @param string $next
  76. * @param integer $done
  77. * @throws \WeChat\Exceptions\InvalidResponseException
  78. * @throws \WeChat\Exceptions\LocalCacheException
  79. * @throws \think\Exception
  80. * @throws \think\exception\PDOException
  81. */
  82. public function _black($next = '', $done = 0)
  83. {
  84. $wechat = WechatService::WeChatUser();
  85. $this->output->comment('开始同步微信黑名单数据 ...');
  86. while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
  87. $done += $result['count'];
  88. foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
  89. Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
  90. }
  91. $this->output->writeln("--> 共计同步微信黑名单{$result['total']}人");
  92. $next = $result['total'] > $done ? $result['next_openid'] : null;
  93. }
  94. $this->output->comment('微信黑名单数据同步完成');
  95. $this->output->newLine();
  96. }
  97. /**
  98. * 同步粉丝标签列表
  99. * @param integer $index
  100. * @throws \WeChat\Exceptions\InvalidResponseException
  101. * @throws \WeChat\Exceptions\LocalCacheException
  102. * @throws \think\Exception
  103. * @throws \think\exception\PDOException
  104. */
  105. public function _tags($index = 0)
  106. {
  107. $appid = WechatService::getAppid();
  108. $wechat = WechatService::WeChatTags();
  109. $this->output->comment('同步微信粉丝标签数据...');
  110. if (is_array($list = $wechat->getTags()) && !empty($list['tags'])) {
  111. $count = count($list['tags']);
  112. foreach ($list['tags'] as &$tag) {
  113. $tag['appid'] = $appid;
  114. $indexString = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT);
  115. $this->output->writeln("({$indexString}/{$count}) 更新粉丝标签 {$tag['name']}");
  116. }
  117. Db::name('WechatFansTags')->where(['appid' => $appid])->delete();
  118. Db::name('WechatFansTags')->insertAll($list['tags']);
  119. }
  120. $this->output->comment('微信粉丝标签数据同步完成');
  121. $this->output->newLine();
  122. }
  123. }