1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace app\service\queue;
- use app\admin\queue\Queue;
- use app\service\service\WechatService;
- use app\wechat\service\FansService;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- class WechatQueue extends Queue
- {
-
- const URI = self::class;
-
- protected $appid;
-
- public function execute(Input $input, Output $output, array $data = [])
- {
- $this->appid = $data['appid'];
- $wechat = WechatService::WeChatUser($this->appid);
-
- list($next, $done) = ['', 0];
- $output->writeln('Start synchronizing fans from the Wechat server');
- while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
- $done += $result['count'];
- foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
- if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
- foreach ($list['user_info_list'] as $user) FansService::set($user, $this->appid);
- }
- }
- $next = $result['total'] > $done ? $result['next_openid'] : null;
- }
-
- list($next, $done) = ['', 0];
- $output->writeln('Start synchronizing black from the Wechat server');
- while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
- $done += $result['count'];
- foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
- $where = [['is_black', 'eq', '0'], ['openid', 'in', $chunk]];
- Db::name('WechatFans')->where($where)->update(['is_black' => '1']);
- }
- $next = $result['total'] > $done ? $result['next_openid'] : null;
- }
-
- $output->writeln('Start synchronizing tags from the Wechat server');
- if (is_array($list = WechatService::WeChatTags($this->appid)->getTags()) && !empty($list['tags'])) {
- foreach ($list['tags'] as &$tag) $tag['appid'] = $this->appid;
- Db::name('WechatFansTags')->where('1=1')->delete();
- Db::name('WechatFansTags')->insertAll($list['tags']);
- }
- }
- }
|