From 0fb344fcf3d013c23f4beb7b59526c1da996aad5 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 19 Jul 2020 16:55:38 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E7=B2=89=E4=B8=9D=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/wechat/command/SyncUsersCommand.php | 155 ++----------------- catch/wechat/controller/Users.php | 8 +- catch/wechat/library/SyncWechatUsers.php | 173 ++++++++++++++++++++++ 3 files changed, 186 insertions(+), 150 deletions(-) create mode 100644 catch/wechat/library/SyncWechatUsers.php diff --git a/catch/wechat/command/SyncUsersCommand.php b/catch/wechat/command/SyncUsersCommand.php index 69d600a..f9403aa 100644 --- a/catch/wechat/command/SyncUsersCommand.php +++ b/catch/wechat/command/SyncUsersCommand.php @@ -11,6 +11,7 @@ namespace catchAdmin\wechat\command; +use catchAdmin\wechat\library\SyncWechatUsers; use catchAdmin\wechat\model\WechatUsers; use catcher\exceptions\FailedException; use catcher\facade\Trie; @@ -33,153 +34,15 @@ class SyncUsersCommand extends Command ->setDescription('sync wechat users'); } + /** + * + * @time 2020年07月19日 + * @param Input $input + * @param Output $output + * @return int|void|null + */ public function execute(Input $input, Output $output) { - $this->officialAccount = WeChat::officialAccount(); - - $latest = WechatUsers::order('subscribe_time')->find(); - - if ($latest) { - throw new FailedException('暂时无法增量同步'); - } - - $this->sync($latest ? $latest->openid : null); - - $this->syncTags(); - } - - protected function syncTags() - { - $users = WechatUsers::cursor(); - - foreach ($users as $user) { - if ($user->tag_list) { - $tagIds = Utils::stringToArrayBy($user->tag_list); - $relate = []; - - foreach ($tagIds as $id) { - $relate[] = [ - 'user_id' => $user->id, - 'tag_id' => $id, - ]; - } - - Db::name('wechat_user_has_tags')->insertAll($relate); - } - } - } - /** - * 同步 - * - * @time 2020年06月20日 - * @param $nextOpenid - * @return void - */ - protected function sync($nextOpenid) - { - $userOpenids = $this->getWechatUserOpenids($nextOpenid); - - if ($userOpenids['next_openid']) { - $this->getUsersBy($userOpenids['data']['openid']); - $this->sync($userOpenids['next_openid']); - } else { - if ($userOpenids['count']) { - $openids = $userOpenids['data']['openid']; - $this->getUsersBy($openids); - } - } - } - - /** - * 获取用户 - * - * @time 2020年06月20日 - * @param $openids - * @return void - */ - protected function getUsersBy($openids) - { - $chunks = array_chunk($openids, $this->getChunkSize($openids)); - - $total = count($chunks); - - $bar = new ProgressBar($this->output, $total); - - $bar->setHeader('[开始同步]'); - - $bar->start(); - foreach ($chunks as $chunk) { - $users = $this->officialAccount->user->select($chunk); - $this->syncToDatabase($users); - $bar->advance(); - } - $bar->finished(); - } - - - /** - * 同步到数据库 - * - * @time 2020年06月20日 - * @param $users - * @return void - */ - protected function syncToDatabase($users) - { - $users = $users['user_info_list']; - - foreach ($users as &$user) { - $user['avatar'] = $user['headimgurl']; - $user['unionid'] = $user['unionid'] ?? ''; - $user['created_at'] = time(); - $user['updated_at'] = time(); - if (!empty($user['tagid_list'])) { - $user['tagid_list'] = trim(implode(',', $user['tagid_list']), ','); - } - unset($user['headimgurl']); - unset($user['qr_scene'], $user['qr_scene_str']); - } - - WechatUsers::insertAll($users); - } - - /** - * 获取 chunk size - * - * @time 2020年06月20日 - * @param $openids - * @return int - */ - protected function getChunkSize($openids) - { - $size = count($openids); - - if ($size < 10) { - return 1; - } - - if ($size > 10 && $size < 100) { - return 10; - } - - if ($size > 100 && $size < 1000) { - return 100; - } - - if ($size > 1000 && $size < 10000) { - return 100; - } - } - - /** - * 获取微信 openids - * - * @time 2020年06月20日 - * @param $nextOpenId - * @return mixed - */ - public function getWechatUserOpenids($nextOpenId) - { - return $this->officialAccount->user->list($nextOpenId); + (new SyncWechatUsers())->start(); } } diff --git a/catch/wechat/controller/Users.php b/catch/wechat/controller/Users.php index 0b0d6c5..8040296 100644 --- a/catch/wechat/controller/Users.php +++ b/catch/wechat/controller/Users.php @@ -10,6 +10,7 @@ */ namespace catchAdmin\wechat\controller; +use catchAdmin\wechat\library\SyncWechatUsers; use catchAdmin\wechat\repository\WechatUsersRepository; use catcher\base\CatchController; use catcher\CatchResponse; @@ -80,13 +81,12 @@ class Users extends CatchController * 用户同步 * * @time 2020年06月26日 + * @param SyncWechatUsers $users * @return \think\response\Json */ - public function sync() + public function sync(SyncWechatUsers $users) { - Console::call('sync:users'); - - return CatchResponse::success('', 'success'); + return CatchResponse::success($users->start(), 'success'); } public function subscribe() diff --git a/catch/wechat/library/SyncWechatUsers.php b/catch/wechat/library/SyncWechatUsers.php new file mode 100644 index 0000000..7721bec --- /dev/null +++ b/catch/wechat/library/SyncWechatUsers.php @@ -0,0 +1,173 @@ +officialAccount = WeChat::officialAccount(); + + $latest = WechatUsers::order('subscribe_time')->find(); + + if ($latest) { + throw new FailedException('暂时无法增量同步'); + } + + $this->sync($latest ? $latest->openid : null); + + $this->syncTags(); + } + + protected function syncTags() + { + $users = WechatUsers::cursor(); + + foreach ($users as $user) { + if ($user->tag_list) { + $tagIds = Utils::stringToArrayBy($user->tag_list); + $relate = []; + + foreach ($tagIds as $id) { + $relate[] = [ + 'user_id' => $user->id, + 'tag_id' => $id, + ]; + } + + Db::name('wechat_user_has_tags')->insertAll($relate); + } + } + } + /** + * 同步 + * + * @time 2020年06月20日 + * @param $nextOpenid + * @return void + */ + protected function sync($nextOpenid) + { + $userOpenids = $this->getWechatUserOpenids($nextOpenid); + + if ($userOpenids['next_openid']) { + $this->getUsersBy($userOpenids['data']['openid']); + $this->sync($userOpenids['next_openid']); + } else { + if ($userOpenids['count']) { + $openids = $userOpenids['data']['openid']; + $this->getUsersBy($openids); + } + } + } + + /** + * 获取用户 + * + * @time 2020年06月20日 + * @param $openids + * @return void + */ + protected function getUsersBy($openids) + { + $chunks = array_chunk($openids, $this->getChunkSize($openids)); + + $total = count($chunks); + + $bar = new ProgressBar($this->output, $total); + + $bar->setHeader('[开始同步]'); + + $bar->start(); + foreach ($chunks as $chunk) { + $users = $this->officialAccount->user->select($chunk); + $this->syncToDatabase($users); + $bar->advance(); + } + $bar->finished(); + } + + + /** + * 同步到数据库 + * + * @time 2020年06月20日 + * @param $users + * @return void + */ + protected function syncToDatabase($users) + { + $users = $users['user_info_list']; + + foreach ($users as &$user) { + $user['avatar'] = $user['headimgurl']; + $user['unionid'] = $user['unionid'] ?? ''; + $user['created_at'] = time(); + $user['updated_at'] = time(); + if (!empty($user['tagid_list'])) { + $user['tagid_list'] = trim(implode(',', $user['tagid_list']), ','); + } + unset($user['headimgurl']); + unset($user['qr_scene'], $user['qr_scene_str']); + } + + WechatUsers::insertAll($users); + } + + /** + * 获取 chunk size + * + * @time 2020年06月20日 + * @param $openids + * @return int + */ + protected function getChunkSize($openids) + { + $size = count($openids); + + if ($size < 10) { + return 1; + } + + if ($size > 10 && $size < 100) { + return 10; + } + + if ($size > 100 && $size < 1000) { + return 100; + } + + if ($size > 1000 && $size < 10000) { + return 100; + } + } + + /** + * 获取微信 openids + * + * @time 2020年06月20日 + * @param $nextOpenId + * @return mixed + */ + public function getWechatUserOpenids($nextOpenId) + { + return $this->officialAccount->user->list($nextOpenId); + } +} \ No newline at end of file