From 4a5cc68915052163e0e741badf70378a5a05e86e Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Mon, 1 Jul 2024 07:21:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E5=85=B3=E6=B3=A8=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Api/Services/Connect.php | 6 ++- app/Http/Home/Services/Connect.php | 4 ++ .../Home/Services/WeChatOfficialAccount.php | 4 ++ .../Home/Views/user/console/account_info.volt | 2 +- app/Repos/Connect.php | 13 +++++ app/Services/Logic/WeChat/OfficialAccount.php | 54 +++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/app/Http/Api/Services/Connect.php b/app/Http/Api/Services/Connect.php index 5d334be4..e15e23ce 100644 --- a/app/Http/Api/Services/Connect.php +++ b/app/Http/Api/Services/Connect.php @@ -56,6 +56,10 @@ class Connect extends Service if ($relation) { + $relation->update_time = time(); + + $relation->update(); + $userRepo = new UserRepo(); $user = $userRepo->findById($relation->user_id); @@ -111,7 +115,7 @@ class Connect extends Service case ConnectModel::PROVIDER_WEIBO: $auth = $this->getWeiBoAuth(); break; - case ConnectModel::PROVIDER_WECHAT: + case ConnectModel::PROVIDER_WECHAT_OA: $auth = $this->getWeChatAuth(); break; } diff --git a/app/Http/Home/Services/Connect.php b/app/Http/Home/Services/Connect.php index 0d96ddaf..0cf0b180 100644 --- a/app/Http/Home/Services/Connect.php +++ b/app/Http/Home/Services/Connect.php @@ -91,6 +91,10 @@ class Connect extends Service $validator->checkIfAllowLogin($user); + $connect->update_time = time(); + + $connect->update(); + $this->handleLoginNotice($user); $auth = $this->getAppAuth(); diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index 1126665d..b923ea91 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -40,6 +40,10 @@ class WeChatOfficialAccount extends Service $validator->checkIfAllowLogin($user); + $connect->update_time = time(); + + $connect->update(); + $this->handleLoginNotice($user); $auth = $this->getAppAuth(); diff --git a/app/Http/Home/Views/user/console/account_info.volt b/app/Http/Home/Views/user/console/account_info.volt index f7c7d254..55325b13 100644 --- a/app/Http/Home/Views/user/console/account_info.volt +++ b/app/Http/Home/Views/user/console/account_info.volt @@ -98,7 +98,7 @@ 提供方 用户信息 - 更新日期 + 最后登录 操作 {% for connect in connects %} diff --git a/app/Repos/Connect.php b/app/Repos/Connect.php index 82a56b5b..bd687a9c 100644 --- a/app/Repos/Connect.php +++ b/app/Repos/Connect.php @@ -67,6 +67,19 @@ class Connect extends Repository ]); } + /** + * @param string $openId + * @param int $provider + * @return ConnectModel|Model|bool + */ + public function findByOpenIdShallow($openId, $provider) + { + return ConnectModel::findFirst([ + 'conditions' => 'open_id = ?1 AND provider = ?2', + 'bind' => [1 => $openId, 2 => $provider], + ]); + } + /** * @param int $userId * @param int $provider diff --git a/app/Services/Logic/WeChat/OfficialAccount.php b/app/Services/Logic/WeChat/OfficialAccount.php index 5f3c6cd4..6038b560 100644 --- a/app/Services/Logic/WeChat/OfficialAccount.php +++ b/app/Services/Logic/WeChat/OfficialAccount.php @@ -9,6 +9,7 @@ namespace App\Services\Logic\WeChat; use App\Models\Connect as ConnectModel; use App\Repos\Connect as ConnectRepo; +use App\Repos\User as UserRepo; use App\Services\Service as AppService; use App\Services\WeChat as WeChatService; use EasyWeChat\Kernel\Messages\Text as TextMessage; @@ -150,9 +151,56 @@ class OfficialAccount extends AppService protected function handleSubscribeEvent($message) { $openId = $message['FromUserName'] ?? ''; + $eventKey = $message['EventKey'] ?? ''; if (empty($openId)) return null; + $connectRepo = new ConnectRepo(); + + $connect = $connectRepo->findByOpenId($openId, ConnectModel::PROVIDER_WECHAT_OA); + + if ($connect) return null; + + /** + * 尼玛不知道为什么又多了个"qrscene_"前缀,SCAN事件里面又不带这个前缀 + */ + $subscribeScene = sprintf('qrscene_%s', self::QR_SCENE_SUBSCRIBE); + + $userId = 0; + + if (Text::startsWith($eventKey, $subscribeScene)) { + + $userId = str_replace($subscribeScene, '', $eventKey); + + } else { + + $connect = $connectRepo->findByOpenIdShallow($openId, ConnectModel::PROVIDER_WECHAT_OA); + + if ($connect) $userId = $connect->user_id; + } + + if ($userId > 0) { + + $userRepo = new UserRepo(); + + $user = $userRepo->findById($userId); + + if (!$user) return null; + + $userInfo = $this->getUserInfo($openId); + + $unionId = $userInfo['unionid'] ?: ''; + + $connect = new ConnectModel(); + + $connect->user_id = $userId; + $connect->open_id = $openId; + $connect->union_id = $unionId; + $connect->provider = ConnectModel::PROVIDER_WECHAT_OA; + + $connect->create(); + } + return new TextMessage('开心呀,我们又多了一个小伙伴!'); } @@ -218,6 +266,12 @@ class OfficialAccount extends AppService if (empty($userId) || empty($openId)) return null; + $userRepo = new UserRepo(); + + $user = $userRepo->findById($userId); + + if (!$user) return null; + $userInfo = $this->getUserInfo($openId); $unionId = $userInfo['unionid'] ?: '';