mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-07-13 03:49:11 +08:00
优化微信公众号相关
This commit is contained in:
parent
72deb17daa
commit
02983a27c9
@ -135,12 +135,9 @@ class ConnectController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$captcha = $service->getSettings('captcha');
|
|
||||||
|
|
||||||
$this->seo->prependTitle('绑定帐号');
|
$this->seo->prependTitle('绑定帐号');
|
||||||
|
|
||||||
$this->view->pick('connect/bind');
|
$this->view->pick('connect/bind');
|
||||||
$this->view->setVar('captcha', $captcha);
|
|
||||||
$this->view->setVar('provider', $provider);
|
$this->view->setVar('provider', $provider);
|
||||||
$this->view->setVar('open_user', $openUser);
|
$this->view->setVar('open_user', $openUser);
|
||||||
}
|
}
|
||||||
|
@ -24,12 +24,9 @@ class WeChatOfficialAccountController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function bindAction()
|
public function bindAction()
|
||||||
{
|
{
|
||||||
$captcha = $this->getSettings('captcha');
|
|
||||||
|
|
||||||
$this->seo->prependTitle('绑定帐号');
|
$this->seo->prependTitle('绑定帐号');
|
||||||
|
|
||||||
$this->view->pick('wechat/oa/bind');
|
$this->view->pick('wechat/oa/bind');
|
||||||
$this->view->setVar('captcha', $captcha);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,6 +14,7 @@ use App\Repos\User as UserRepo;
|
|||||||
use App\Services\Auth\Home as AuthService;
|
use App\Services\Auth\Home as AuthService;
|
||||||
use App\Services\Logic\Account\Register as RegisterService;
|
use App\Services\Logic\Account\Register as RegisterService;
|
||||||
use App\Services\Logic\Notice\External\AccountLogin as AccountLoginNotice;
|
use App\Services\Logic\Notice\External\AccountLogin as AccountLoginNotice;
|
||||||
|
use App\Services\Logic\WeChat\OfficialAccount as WeChatOAService;
|
||||||
use App\Validators\Account as AccountValidator;
|
use App\Validators\Account as AccountValidator;
|
||||||
use App\Validators\WeChatOfficialAccount as WeChatOAValidator;
|
use App\Validators\WeChatOfficialAccount as WeChatOAValidator;
|
||||||
|
|
||||||
@ -63,10 +64,13 @@ class WeChatOfficialAccount extends Service
|
|||||||
|
|
||||||
$openId = $validator->checkLoginOpenId($post['ticket']);
|
$openId = $validator->checkLoginOpenId($post['ticket']);
|
||||||
|
|
||||||
|
$unionId = $this->getUnionId($openId);
|
||||||
|
|
||||||
$connect = new ConnectModel();
|
$connect = new ConnectModel();
|
||||||
|
|
||||||
$connect->user_id = $user->id;
|
$connect->user_id = $user->id;
|
||||||
$connect->open_id = $openId;
|
$connect->open_id = $openId;
|
||||||
|
$connect->union_id = $unionId;
|
||||||
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
||||||
|
|
||||||
$connect->create();
|
$connect->create();
|
||||||
@ -86,6 +90,8 @@ class WeChatOfficialAccount extends Service
|
|||||||
|
|
||||||
$openId = $validator->checkLoginOpenId($post['ticket']);
|
$openId = $validator->checkLoginOpenId($post['ticket']);
|
||||||
|
|
||||||
|
$unionId = $this->getUnionId($openId);
|
||||||
|
|
||||||
$registerService = new RegisterService();
|
$registerService = new RegisterService();
|
||||||
|
|
||||||
$account = $registerService->handle();
|
$account = $registerService->handle();
|
||||||
@ -98,6 +104,7 @@ class WeChatOfficialAccount extends Service
|
|||||||
|
|
||||||
$connect->user_id = $user->id;
|
$connect->user_id = $user->id;
|
||||||
$connect->open_id = $openId;
|
$connect->open_id = $openId;
|
||||||
|
$connect->union_id = $unionId;
|
||||||
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
||||||
|
|
||||||
$connect->create();
|
$connect->create();
|
||||||
@ -111,6 +118,17 @@ class WeChatOfficialAccount extends Service
|
|||||||
$this->eventsManager->fire('Account:afterRegister', $this, $user);
|
$this->eventsManager->fire('Account:afterRegister', $this, $user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getUnionId($openId)
|
||||||
|
{
|
||||||
|
$service = new WeChatOAService();
|
||||||
|
|
||||||
|
$app = $service->getOfficialAccount();
|
||||||
|
|
||||||
|
$user = $app->user->get($openId);
|
||||||
|
|
||||||
|
return $user['unionid'] ?: '';
|
||||||
|
}
|
||||||
|
|
||||||
protected function getAppAuth()
|
protected function getAppAuth()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -29,8 +29,16 @@ class Connect extends Repository
|
|||||||
$query->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]);
|
$query->andWhere('user_id = :user_id:', ['user_id' => $where['user_id']]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($where['open_id'])) {
|
||||||
|
$query->andWhere('open_id = :open_id:', ['open_id' => $where['open_id']]);
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($where['provider'])) {
|
if (!empty($where['provider'])) {
|
||||||
$query->andWhere('provider = :provider:', ['provider' => $where['provider']]);
|
if (is_array($where['provider'])) {
|
||||||
|
$query->inWhere('provider', $where['provider']);
|
||||||
|
} else {
|
||||||
|
$query->andWhere('provider = :provider:', ['provider' => $where['provider']]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($where['deleted'])) {
|
if (isset($where['deleted'])) {
|
||||||
@ -67,19 +75,6 @@ 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
|
||||||
|
@ -111,6 +111,9 @@ class OfficialAccount extends AppService
|
|||||||
|
|
||||||
$logger->debug('Received Message: ' . json_encode($message));
|
$logger->debug('Received Message: ' . json_encode($message));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 事件类型文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
|
||||||
|
*/
|
||||||
switch ($message['MsgType']) {
|
switch ($message['MsgType']) {
|
||||||
case 'event':
|
case 'event':
|
||||||
switch ($message['Event']) {
|
switch ($message['Event']) {
|
||||||
@ -161,44 +164,24 @@ class OfficialAccount extends AppService
|
|||||||
|
|
||||||
if ($connect) return null;
|
if ($connect) return null;
|
||||||
|
|
||||||
/**
|
$loginScene = sprintf('qrscene_%s', self::QR_SCENE_LOGIN);
|
||||||
* 尼玛不知道为什么又多了个"qrscene_"前缀,SCAN事件里面又不带这个前缀
|
|
||||||
*/
|
|
||||||
$subscribeScene = sprintf('qrscene_%s', self::QR_SCENE_SUBSCRIBE);
|
$subscribeScene = sprintf('qrscene_%s', self::QR_SCENE_SUBSCRIBE);
|
||||||
|
|
||||||
$userId = 0;
|
/**
|
||||||
|
* 未关注过服务号,在登录页扫登录场景码,关注服务号
|
||||||
if (Text::startsWith($eventKey, $subscribeScene)) {
|
*/
|
||||||
|
if (Text::startsWith($eventKey, $loginScene)) {
|
||||||
$userId = str_replace($subscribeScene, '', $eventKey);
|
$ticket = str_replace($loginScene, '', $eventKey);
|
||||||
|
$this->handleLoginPageSubscribe($ticket, $openId);
|
||||||
} else {
|
|
||||||
|
|
||||||
$connect = $connectRepo->findByOpenIdShallow($openId, ConnectModel::PROVIDER_WECHAT_OA);
|
|
||||||
|
|
||||||
if ($connect) $userId = $connect->user_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($userId > 0) {
|
/**
|
||||||
|
* 未关注过服务号,在用户中心扫关注场景码,关注服务号
|
||||||
$userRepo = new UserRepo();
|
*/
|
||||||
|
if (Text::startsWith($eventKey, $subscribeScene)) {
|
||||||
$user = $userRepo->findById($userId);
|
$userId = str_replace($subscribeScene, '', $eventKey);
|
||||||
|
$this->handleAccountPageSubscribe($userId, $openId);
|
||||||
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('开心呀,我们又多了一个小伙伴!');
|
||||||
@ -228,19 +211,19 @@ class OfficialAccount extends AppService
|
|||||||
$eventKey = $message['EventKey'] ?? '';
|
$eventKey = $message['EventKey'] ?? '';
|
||||||
|
|
||||||
if (Text::startsWith($eventKey, self::QR_SCENE_LOGIN)) {
|
if (Text::startsWith($eventKey, self::QR_SCENE_LOGIN)) {
|
||||||
return $this->handleLoginScanEvent($eventKey, $openId);
|
$ticket = str_replace(self::QR_SCENE_LOGIN, '', $eventKey);
|
||||||
|
$this->handleLoginPageSubscribe($ticket, $openId);
|
||||||
} elseif (Text::startsWith($eventKey, self::QR_SCENE_SUBSCRIBE)) {
|
} elseif (Text::startsWith($eventKey, self::QR_SCENE_SUBSCRIBE)) {
|
||||||
return $this->handleSubscribeScanEvent($eventKey, $openId);
|
$userId = str_replace(self::QR_SCENE_SUBSCRIBE, '', $eventKey);
|
||||||
|
$this->handleAccountPageSubscribe($userId, $openId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->emptyReply();
|
return $this->emptyReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function handleLoginScanEvent($eventKey, $openId)
|
protected function handleLoginPageSubscribe($ticket, $openId)
|
||||||
{
|
{
|
||||||
$ticket = str_replace(self::QR_SCENE_LOGIN, '', $eventKey);
|
if (empty($ticket) || empty($openId)) return;
|
||||||
|
|
||||||
if (empty($ticket) || empty($openId)) return null;
|
|
||||||
|
|
||||||
$connectRepo = new ConnectRepo();
|
$connectRepo = new ConnectRepo();
|
||||||
|
|
||||||
@ -256,31 +239,27 @@ class OfficialAccount extends AppService
|
|||||||
];
|
];
|
||||||
|
|
||||||
$cache->save($keyName, $content, 30 * 60);
|
$cache->save($keyName, $content, 30 * 60);
|
||||||
|
|
||||||
return $this->emptyReply();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function handleSubscribeScanEvent($eventKey, $openId)
|
protected function handleAccountPageSubscribe($userId, $openId)
|
||||||
{
|
{
|
||||||
$userId = str_replace(self::QR_SCENE_SUBSCRIBE, '', $eventKey);
|
if (empty($userId) || empty($openId)) return;
|
||||||
|
|
||||||
if (empty($userId) || empty($openId)) return null;
|
|
||||||
|
|
||||||
$userRepo = new UserRepo();
|
$userRepo = new UserRepo();
|
||||||
|
|
||||||
$user = $userRepo->findById($userId);
|
$user = $userRepo->findById($userId);
|
||||||
|
|
||||||
if (!$user) return null;
|
if (!$user) return;
|
||||||
|
|
||||||
$userInfo = $this->getUserInfo($openId);
|
|
||||||
|
|
||||||
$unionId = $userInfo['unionid'] ?: '';
|
|
||||||
|
|
||||||
$connectRepo = new ConnectRepo();
|
$connectRepo = new ConnectRepo();
|
||||||
|
|
||||||
$connect = $connectRepo->findByOpenId($openId, ConnectModel::PROVIDER_WECHAT_OA);
|
$connect = $connectRepo->findByOpenId($openId, ConnectModel::PROVIDER_WECHAT_OA);
|
||||||
|
|
||||||
if ($connect) return null;
|
if ($connect) return;
|
||||||
|
|
||||||
|
$userInfo = $this->getUserInfo($openId);
|
||||||
|
|
||||||
|
$unionId = $userInfo['unionid'] ?: '';
|
||||||
|
|
||||||
$connect = new ConnectModel();
|
$connect = new ConnectModel();
|
||||||
|
|
||||||
@ -290,8 +269,6 @@ class OfficialAccount extends AppService
|
|||||||
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
$connect->provider = ConnectModel::PROVIDER_WECHAT_OA;
|
||||||
|
|
||||||
$connect->create();
|
$connect->create();
|
||||||
|
|
||||||
return $this->emptyReply();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function handleClickEvent($message)
|
protected function handleClickEvent($message)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user