From e9c36078dc4e14184c666ae69dc0f9305f404bc7 Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 10 Dec 2019 10:08:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E7=B2=89?= =?UTF-8?q?=E4=B8=9D=E5=90=8C=E6=AD=A5=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/wechat/command/Fans.php | 133 ++++++++++++++++++++++++++ app/wechat/command/WechatFans.php | 12 --- app/wechat/command/fans/FansAll.php | 32 +++++++ app/wechat/command/fans/FansBlack.php | 32 +++++++ app/wechat/command/fans/FansList.php | 33 +++++++ app/wechat/command/fans/FansTags.php | 32 +++++++ app/wechat/sys.php | 12 +++ 7 files changed, 274 insertions(+), 12 deletions(-) create mode 100644 app/wechat/command/Fans.php delete mode 100644 app/wechat/command/WechatFans.php create mode 100644 app/wechat/command/fans/FansAll.php create mode 100644 app/wechat/command/fans/FansBlack.php create mode 100644 app/wechat/command/fans/FansList.php create mode 100644 app/wechat/command/fans/FansTags.php create mode 100644 app/wechat/sys.php diff --git a/app/wechat/command/Fans.php b/app/wechat/command/Fans.php new file mode 100644 index 000000000..01a9f4240 --- /dev/null +++ b/app/wechat/command/Fans.php @@ -0,0 +1,133 @@ +module as $m) { + if (method_exists($this, $fun = "_{$m}")) $this->$fun(); + } + } + + /** + * 同步微信粉丝列表 + * @param string $next + * @param integer $done + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function _list($next = '', $done = 0) + { + $appid = WechatService::instance()->getAppid(); + $this->output->comment('开始同步微信粉丝数据'); + while (!is_null($next) && is_array($result = WechatService::WeChatUser()->getUserList($next)) && !empty($result['data']['openid'])) { + foreach (array_chunk($result['data']['openid'], 100) as $openids) { + if (is_array($list = WechatService::WeChatUser()->getBatchUserInfo($openids)) && !empty($list['user_info_list'])) { + foreach ($list['user_info_list'] as $user) { + $string = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT); + $this->output->writeln("({$string}/{$result['total']}) -> {$user['openid']} {$user['nickname']}"); + \app\wechat\service\FansService::instance()->set($user, $appid); + } + } + } + $next = $result['total'] > $done ? $result['next_openid'] : null; + } + $this->output->comment('微信粉丝数据同步完成!'); + $this->output->newLine(); + } + + /** + * 同步粉丝黑名单列表 + * @param string $next + * @param integer $done + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\db\exception\DbException + */ + public function _black($next = '', $done = 0) + { + $wechat = WechatService::WeChatUser(); + $this->output->comment('开始同步微信黑名单数据 ...'); + 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) { + $this->app->db->name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']); + } + $this->output->writeln("共计同步微信黑名单{$result['total']}人"); + $next = $result['total'] > $done ? $result['next_openid'] : null; + } + $this->output->comment('微信黑名单数据同步完成!'); + $this->output->newLine(); + } + + /** + * 同步粉丝标签列表 + * @param integer $index + * @throws \WeChat\Exceptions\InvalidResponseException + * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function _tags($index = 0) + { + $wechat = WechatService::WeChatTags(); + $appid = WechatService::instance()->getAppid(); + $this->output->comment('同步微信粉丝标签数据...'); + if (is_array($list = $wechat->getTags()) && !empty($list['tags'])) { + $count = count($list['tags']); + foreach ($list['tags'] as &$tag) { + $tag['appid'] = $appid; + $indexString = str_pad(++$index, strlen($count), '0', STR_PAD_LEFT); + $this->output->writeln("({$indexString}/{$count}) -> {$tag['name']}"); + } + $this->app->db->name('WechatFansTags')->where(['appid' => $appid])->delete(); + $this->app->db->name('WechatFansTags')->insertAll($list['tags']); + } + $this->output->comment('微信粉丝标签数据同步完成!'); + $this->output->newLine(); + } + +} \ No newline at end of file diff --git a/app/wechat/command/WechatFans.php b/app/wechat/command/WechatFans.php deleted file mode 100644 index c0b6ce463..000000000 --- a/app/wechat/command/WechatFans.php +++ /dev/null @@ -1,12 +0,0 @@ -module = ['list', 'black', 'tags']; + $this->setName('xfans:all')->setDescription('[同步]所有微信粉丝的数据'); + } +} diff --git a/app/wechat/command/fans/FansBlack.php b/app/wechat/command/fans/FansBlack.php new file mode 100644 index 000000000..e0aac7c90 --- /dev/null +++ b/app/wechat/command/fans/FansBlack.php @@ -0,0 +1,32 @@ +module = ['black']; + $this->setName('xfans:black')->setDescription('[同步]微信黑名单粉丝数据'); + } +} diff --git a/app/wechat/command/fans/FansList.php b/app/wechat/command/fans/FansList.php new file mode 100644 index 000000000..32b98710b --- /dev/null +++ b/app/wechat/command/fans/FansList.php @@ -0,0 +1,33 @@ +module = ['list']; + $this->setName('xfans:list')->setDescription('[同步]微信粉丝的资料数据'); + } + +} diff --git a/app/wechat/command/fans/FansTags.php b/app/wechat/command/fans/FansTags.php new file mode 100644 index 000000000..317b26e52 --- /dev/null +++ b/app/wechat/command/fans/FansTags.php @@ -0,0 +1,32 @@ +module = ['tags']; + $this->setName('xfans:tags')->setDescription('[同步]粉丝的标签记录数据'); + } +} diff --git a/app/wechat/sys.php b/app/wechat/sys.php new file mode 100644 index 000000000..e5d3993d1 --- /dev/null +++ b/app/wechat/sys.php @@ -0,0 +1,12 @@ +addCommands([ + 'app\wechat\command\fans\fansAll', + 'app\wechat\command\fans\fansBlack', + 'app\wechat\command\fans\fansList', + 'app\wechat\command\fans\fansTags', + ]); +}); \ No newline at end of file