mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-23 03:50:56 +08:00
优化公众号订阅事件
This commit is contained in:
parent
1c880d0c56
commit
d0112d501e
@ -90,6 +90,18 @@ class WeChatOfficialAccount extends Service
|
|||||||
|
|
||||||
protected function handleSubscribeEvent($message)
|
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('开心呀,我们又多了一个小伙伴!');
|
return new TextMessage('开心呀,我们又多了一个小伙伴!');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,26 +127,8 @@ class WeChatOfficialAccount extends Service
|
|||||||
|
|
||||||
$userId = str_replace('qrscene_', '', $eventKey);
|
$userId = str_replace('qrscene_', '', $eventKey);
|
||||||
|
|
||||||
$userRepo = new UserRepo();
|
if ($userId && $openId) {
|
||||||
|
$this->saveWechatSubscribe($userId, $openId);
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->emptyReply();
|
return $this->emptyReply();
|
||||||
@ -200,4 +194,31 @@ class WeChatOfficialAccount extends Service
|
|||||||
return new TextMessage('没有匹配的服务哦!');
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user