From d0112d501ec898d4452131b9bd24a714e556558c Mon Sep 17 00:00:00 2001 From: koogua Date: Mon, 13 Sep 2021 17:30:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Home/Services/WeChatOfficialAccount.php | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index 567f2fc4..d04ec722 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -90,6 +90,18 @@ class WeChatOfficialAccount extends Service protected function handleSubscribeEvent($message) { + $openId = $message['FromUserName'] ?? ''; + $eventKey = $message['EventKey'] ?? ''; + + /** + * 带场景值的关注事件 + */ + $userId = str_replace('qrscene_', '', $eventKey); + + if ($userId && $openId) { + $this->saveWechatSubscribe($userId, $openId); + } + return new TextMessage('开心呀,我们又多了一个小伙伴!'); } @@ -115,26 +127,8 @@ class WeChatOfficialAccount extends Service $userId = str_replace('qrscene_', '', $eventKey); - $userRepo = new UserRepo(); - - $user = $userRepo->findById($userId); - - if (!$user) return $this->emptyReply(); - - $subscribeRepo = new WeChatSubscribeRepo(); - - $subscribe = $subscribeRepo->findByOpenId($openId); - - if ($subscribe) { - if ($subscribe->user_id != $userId) { - $subscribe->user_id = $userId; - } - $subscribe->update(); - } else { - $subscribe = new WeChatSubscribeModel(); - $subscribe->user_id = $userId; - $subscribe->open_id = $openId; - $subscribe->create(); + if ($userId && $openId) { + $this->saveWechatSubscribe($userId, $openId); } return $this->emptyReply(); @@ -200,4 +194,31 @@ class WeChatOfficialAccount extends Service return new TextMessage('没有匹配的服务哦!'); } + protected function saveWechatSubscribe($userId, $openId) + { + if (!$userId || !$openId) return; + + $userRepo = new UserRepo(); + + $user = $userRepo->findById($userId); + + if (!$user) return; + + $subscribeRepo = new WeChatSubscribeRepo(); + + $subscribe = $subscribeRepo->findByOpenId($openId); + + if ($subscribe) { + if ($subscribe->user_id != $userId) { + $subscribe->user_id = $userId; + $subscribe->update(); + } + } else { + $subscribe = new WeChatSubscribeModel(); + $subscribe->user_id = $userId; + $subscribe->open_id = $openId; + $subscribe->create(); + } + } + }