1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-15 12:52:21 +08:00

优化公众号关注订阅

This commit is contained in:
xiaochong0302 2024-07-01 07:21:37 +08:00
parent f0470c027d
commit 4a5cc68915
6 changed files with 81 additions and 2 deletions

View File

@ -56,6 +56,10 @@ class Connect extends Service
if ($relation) { if ($relation) {
$relation->update_time = time();
$relation->update();
$userRepo = new UserRepo(); $userRepo = new UserRepo();
$user = $userRepo->findById($relation->user_id); $user = $userRepo->findById($relation->user_id);
@ -111,7 +115,7 @@ class Connect extends Service
case ConnectModel::PROVIDER_WEIBO: case ConnectModel::PROVIDER_WEIBO:
$auth = $this->getWeiBoAuth(); $auth = $this->getWeiBoAuth();
break; break;
case ConnectModel::PROVIDER_WECHAT: case ConnectModel::PROVIDER_WECHAT_OA:
$auth = $this->getWeChatAuth(); $auth = $this->getWeChatAuth();
break; break;
} }

View File

@ -91,6 +91,10 @@ class Connect extends Service
$validator->checkIfAllowLogin($user); $validator->checkIfAllowLogin($user);
$connect->update_time = time();
$connect->update();
$this->handleLoginNotice($user); $this->handleLoginNotice($user);
$auth = $this->getAppAuth(); $auth = $this->getAppAuth();

View File

@ -40,6 +40,10 @@ class WeChatOfficialAccount extends Service
$validator->checkIfAllowLogin($user); $validator->checkIfAllowLogin($user);
$connect->update_time = time();
$connect->update();
$this->handleLoginNotice($user); $this->handleLoginNotice($user);
$auth = $this->getAppAuth(); $auth = $this->getAppAuth();

View File

@ -98,7 +98,7 @@
<tr> <tr>
<td>提供方</td> <td>提供方</td>
<td>用户信息</td> <td>用户信息</td>
<td>更新日期</td> <td>最后登录</td>
<td width="15%">操作</td> <td width="15%">操作</td>
</tr> </tr>
{% for connect in connects %} {% for connect in connects %}

View File

@ -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 $userId
* @param int $provider * @param int $provider

View File

@ -9,6 +9,7 @@ namespace App\Services\Logic\WeChat;
use App\Models\Connect as ConnectModel; use App\Models\Connect as ConnectModel;
use App\Repos\Connect as ConnectRepo; use App\Repos\Connect as ConnectRepo;
use App\Repos\User as UserRepo;
use App\Services\Service as AppService; use App\Services\Service as AppService;
use App\Services\WeChat as WeChatService; use App\Services\WeChat as WeChatService;
use EasyWeChat\Kernel\Messages\Text as TextMessage; use EasyWeChat\Kernel\Messages\Text as TextMessage;
@ -150,9 +151,56 @@ class OfficialAccount extends AppService
protected function handleSubscribeEvent($message) protected function handleSubscribeEvent($message)
{ {
$openId = $message['FromUserName'] ?? ''; $openId = $message['FromUserName'] ?? '';
$eventKey = $message['EventKey'] ?? '';
if (empty($openId)) return null; 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('开心呀,我们又多了一个小伙伴!'); return new TextMessage('开心呀,我们又多了一个小伙伴!');
} }
@ -218,6 +266,12 @@ class OfficialAccount extends AppService
if (empty($userId) || empty($openId)) return null; if (empty($userId) || empty($openId)) return null;
$userRepo = new UserRepo();
$user = $userRepo->findById($userId);
if (!$user) return null;
$userInfo = $this->getUserInfo($openId); $userInfo = $this->getUserInfo($openId);
$unionId = $userInfo['unionid'] ?: ''; $unionId = $userInfo['unionid'] ?: '';