mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
v1.4.5 beta2
This commit is contained in:
parent
b65c66d99c
commit
616c7aa40d
@ -1,4 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Http\Api\Controllers;
|
||||
|
||||
@ -16,8 +21,14 @@ class ConnectController extends Controller
|
||||
*/
|
||||
public function wechatAction()
|
||||
{
|
||||
$redirect = $this->request->getQuery('redirect', 'trim');
|
||||
|
||||
$service = new ConnectService();
|
||||
|
||||
if ($redirect) {
|
||||
$service->setWechatRedirectCache($redirect);
|
||||
}
|
||||
|
||||
$url = $service->getAuthorizeUrl(ConnectModel::PROVIDER_WECHAT);
|
||||
|
||||
return $this->response->redirect($url, true);
|
||||
@ -32,7 +43,18 @@ class ConnectController extends Controller
|
||||
|
||||
$data = $service->handleCallback(ConnectModel::PROVIDER_WECHAT);
|
||||
|
||||
$location = kg_h5_index_url() . '?' . http_build_query($data);
|
||||
$redirect = $service->getWechatRedirectCache();
|
||||
|
||||
if ($redirect) {
|
||||
$service->removeWechatRedirectCache();
|
||||
if (strpos($redirect, '?')) {
|
||||
$location = $redirect . '&' . http_build_query($data);
|
||||
} else {
|
||||
$location = $redirect . '?' . http_build_query($data);
|
||||
}
|
||||
} else {
|
||||
$location = kg_h5_index_url() . '?' . http_build_query($data);
|
||||
}
|
||||
|
||||
return $this->response->redirect($location, true);
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Http\Api\Services;
|
||||
|
||||
@ -24,6 +29,21 @@ class Connect extends Service
|
||||
];
|
||||
}
|
||||
|
||||
public function getWechatRedirectCache()
|
||||
{
|
||||
return $this->session->get('wechat_redirect');
|
||||
}
|
||||
|
||||
public function setWechatRedirectCache($redirect)
|
||||
{
|
||||
$this->session->set('wechat_redirect', $redirect);
|
||||
}
|
||||
|
||||
public function removeWechatRedirectCache()
|
||||
{
|
||||
$this->session->remove('wechat_redirect');
|
||||
}
|
||||
|
||||
public function handleCallback($provider)
|
||||
{
|
||||
$code = $this->request->getQuery('code');
|
||||
|
@ -119,6 +119,19 @@ class ConnectController extends Controller
|
||||
|
||||
$service = new ConnectService();
|
||||
|
||||
$openUser = $service->getOpenUserInfo($code, $state, $provider);
|
||||
|
||||
/**
|
||||
* 微信扫码登录检查是否关注过公众号,关注过直接登录
|
||||
*/
|
||||
if ($provider == ConnectModel::PROVIDER_WEIXIN && !empty($openUser['unionid'])) {
|
||||
$subscribe = $service->getWeChatSubscribe($openUser['unionid']);
|
||||
if ($subscribe && $subscribe->deleted == 0) {
|
||||
$service->authSubscribeLogin($subscribe);
|
||||
return $this->response->redirect(['for' => 'home.index']);
|
||||
}
|
||||
}
|
||||
|
||||
$openUser = $service->getOpenUserInfo($code, $state, $provider);
|
||||
$connect = $service->getConnectRelation($openUser['id'], $openUser['provider']);
|
||||
|
||||
@ -129,7 +142,7 @@ class ConnectController extends Controller
|
||||
}
|
||||
} else {
|
||||
if ($connect) {
|
||||
$service->authLogin($connect);
|
||||
$service->authConnectLogin($connect);
|
||||
return $this->response->redirect(['for' => 'home.index']);
|
||||
}
|
||||
}
|
||||
|
@ -9,11 +9,14 @@ namespace App\Http\Home\Services;
|
||||
|
||||
use App\Models\Connect as ConnectModel;
|
||||
use App\Models\User as UserModel;
|
||||
use App\Models\WeChatSubscribe as WeChatSubscribeModel;
|
||||
use App\Repos\Connect as ConnectRepo;
|
||||
use App\Repos\User as UserRepo;
|
||||
use App\Repos\WeChatSubscribe as WeChatSubscribeRepo;
|
||||
use App\Services\Auth\Home as AuthService;
|
||||
use App\Services\Auth\Home as HomeAuthService;
|
||||
use App\Services\Logic\Account\Register as RegisterService;
|
||||
use App\Services\Logic\Notice\AccountLogin as AccountLoginNoticeService;
|
||||
use App\Services\Logic\Notice\AccountLogin as AccountLoginNotice;
|
||||
use App\Services\OAuth\QQ as QQAuth;
|
||||
use App\Services\OAuth\WeiBo as WeiBoAuth;
|
||||
use App\Services\OAuth\WeiXin as WeiXinAuth;
|
||||
@ -79,7 +82,20 @@ class Connect extends Service
|
||||
$this->handleConnectRelation($user, $openUser);
|
||||
}
|
||||
|
||||
public function authLogin(ConnectModel $connect)
|
||||
public function authSubscribeLogin(WeChatSubscribeModel $subscribe)
|
||||
{
|
||||
$userRepo = new UserRepo();
|
||||
|
||||
$user = $userRepo->findById($subscribe->user_id);
|
||||
|
||||
$this->handleLoginNotice($user);
|
||||
|
||||
$auth = new HomeAuthService();
|
||||
|
||||
$auth->saveAuthInfo($user);
|
||||
}
|
||||
|
||||
public function authConnectLogin(ConnectModel $connect)
|
||||
{
|
||||
$userRepo = new UserRepo();
|
||||
|
||||
@ -112,6 +128,13 @@ class Connect extends Service
|
||||
return $auth->getUserInfo($token, $openId);
|
||||
}
|
||||
|
||||
public function getWeChatSubscribe($unionId)
|
||||
{
|
||||
$subscribeRepo = new WeChatSubscribeRepo();
|
||||
|
||||
return $subscribeRepo->findByUnionId($unionId);
|
||||
}
|
||||
|
||||
public function getConnectRelation($openId, $provider)
|
||||
{
|
||||
$connectRepo = new ConnectRepo();
|
||||
@ -200,6 +223,14 @@ class Connect extends Service
|
||||
$connect->user_id = $user->id;
|
||||
}
|
||||
|
||||
if (empty($connect->union_id) && !empty($openUser['unionid'])) {
|
||||
$connect->union_id = $openUser['unionid'];
|
||||
}
|
||||
|
||||
if ($connect->deleted == 1) {
|
||||
$connect->deleted = 0;
|
||||
}
|
||||
|
||||
$connect->update();
|
||||
|
||||
} else {
|
||||
@ -207,6 +238,7 @@ class Connect extends Service
|
||||
$connect = new ConnectModel();
|
||||
|
||||
$connect->user_id = $user->id;
|
||||
$connect->union_id = $openUser['unionid'];
|
||||
$connect->open_id = $openUser['id'];
|
||||
$connect->open_name = $openUser['name'];
|
||||
$connect->open_avatar = $openUser['avatar'];
|
||||
@ -218,9 +250,9 @@ class Connect extends Service
|
||||
|
||||
protected function handleLoginNotice(UserModel $user)
|
||||
{
|
||||
$service = new AccountLoginNoticeService();
|
||||
$notice = new AccountLoginNotice();
|
||||
|
||||
$service->createTask($user);
|
||||
$notice->createTask($user);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -114,7 +114,8 @@ class WeChatOfficialAccount extends Service
|
||||
$subscribe = $subscribeRepo->findByOpenId($openId);
|
||||
|
||||
if ($subscribe) {
|
||||
$subscribe->delete();
|
||||
$subscribe->deleted = 1;
|
||||
$subscribe->update();
|
||||
}
|
||||
|
||||
return new TextMessage('伤心呀,我们又少了一个小伙伴!');
|
||||
@ -128,7 +129,9 @@ class WeChatOfficialAccount extends Service
|
||||
$userId = str_replace('qrscene_', '', $eventKey);
|
||||
|
||||
if ($userId && $openId) {
|
||||
$this->saveWechatSubscribe($userId, $openId);
|
||||
$userInfo = $this->getUserInfo($openId);
|
||||
$unionId = $userInfo['unionid'] ?: '';
|
||||
$this->saveWechatSubscribe($userId, $openId, $unionId);
|
||||
}
|
||||
|
||||
return $this->emptyReply();
|
||||
@ -194,7 +197,7 @@ class WeChatOfficialAccount extends Service
|
||||
return new TextMessage('没有匹配的服务哦!');
|
||||
}
|
||||
|
||||
protected function saveWechatSubscribe($userId, $openId)
|
||||
protected function saveWechatSubscribe($userId, $openId, $unionId = '')
|
||||
{
|
||||
if (!$userId || !$openId) return;
|
||||
|
||||
@ -211,14 +214,35 @@ class WeChatOfficialAccount extends Service
|
||||
if ($subscribe) {
|
||||
if ($subscribe->user_id != $userId) {
|
||||
$subscribe->user_id = $userId;
|
||||
$subscribe->update();
|
||||
}
|
||||
if (empty($subscribe->union_id) && !empty($unionId)) {
|
||||
$subscribe->union_id = $unionId;
|
||||
}
|
||||
if ($subscribe->deleted == 1) {
|
||||
$subscribe->deleted = 0;
|
||||
}
|
||||
$subscribe->update();
|
||||
} else {
|
||||
$subscribe = new WeChatSubscribeModel();
|
||||
$subscribe->user_id = $userId;
|
||||
$subscribe->open_id = $openId;
|
||||
$subscribe->union_id = $unionId;
|
||||
$subscribe->create();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getUserInfo($openId)
|
||||
{
|
||||
$app = $this->getOfficialAccount();
|
||||
|
||||
return $app->user->get($openId);
|
||||
}
|
||||
|
||||
protected function getWechatLogger()
|
||||
{
|
||||
$service = new WeChatService();
|
||||
|
||||
return $service->logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -62,4 +62,16 @@ class WeChatSubscribe extends Repository
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unionId
|
||||
* @return WeChatSubscribeModel|Model|bool
|
||||
*/
|
||||
public function findByUnionId($unionId)
|
||||
{
|
||||
return WeChatSubscribeModel::findFirst([
|
||||
'conditions' => 'union_id = :union_id:',
|
||||
'bind' => ['union_id' => $unionId],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
|
||||
* @license https://opensource.org/licenses/GPL-2.0
|
||||
* @link https://www.koogua.com
|
||||
*/
|
||||
|
||||
namespace App\Services\OAuth;
|
||||
|
||||
|
@ -18,22 +18,39 @@ class V20210917093354 extends Phinx\Migration\AbstractMigration
|
||||
|
||||
protected function alterConnectTable()
|
||||
{
|
||||
$this->table('kg_connect')
|
||||
->addColumn('deleted', 'integer', [
|
||||
$table = $this->table('kg_connect');
|
||||
|
||||
if (!$table->hasColumn('deleted')) {
|
||||
$table->addColumn('deleted', 'integer', [
|
||||
'null' => false,
|
||||
'default' => '0',
|
||||
'limit' => MysqlAdapter::INT_REGULAR,
|
||||
'signed' => false,
|
||||
'comment' => '删除标识',
|
||||
'after' => 'provider',
|
||||
])
|
||||
->save();
|
||||
]);
|
||||
}
|
||||
if (!$table->hasIndexByName('user_id')) {
|
||||
$table->addIndex(['user_id'], [
|
||||
'name' => 'user_id',
|
||||
'unique' => false,
|
||||
]);
|
||||
}
|
||||
if (!$table->hasIndexByName('open_id')) {
|
||||
$table->addIndex(['open_id'], [
|
||||
'name' => 'open_id',
|
||||
'unique' => false,
|
||||
]);
|
||||
}
|
||||
$table->save();
|
||||
}
|
||||
|
||||
protected function alterWechatSubscribeTable()
|
||||
{
|
||||
$this->table('kg_wechat_subscribe')
|
||||
->addColumn('union_id', 'string', [
|
||||
$table = $this->table('kg_wechat_subscribe');
|
||||
|
||||
if (!$table->hasColumn('union_id')) {
|
||||
$table->addColumn('union_id', 'string', [
|
||||
'null' => false,
|
||||
'default' => '',
|
||||
'limit' => 64,
|
||||
@ -41,16 +58,37 @@ class V20210917093354 extends Phinx\Migration\AbstractMigration
|
||||
'encoding' => 'utf8mb4',
|
||||
'comment' => '联合ID',
|
||||
'after' => 'open_id',
|
||||
])
|
||||
->addColumn('deleted', 'integer', [
|
||||
]);
|
||||
}
|
||||
if (!$table->hasColumn('deleted')) {
|
||||
$table->addColumn('deleted', 'integer', [
|
||||
'null' => false,
|
||||
'default' => '0',
|
||||
'limit' => MysqlAdapter::INT_REGULAR,
|
||||
'signed' => false,
|
||||
'comment' => '删除标识',
|
||||
'after' => 'open_id',
|
||||
])
|
||||
->save();
|
||||
'after' => 'union_id',
|
||||
]);
|
||||
}
|
||||
if (!$table->hasIndexByName('user_id')) {
|
||||
$table->addIndex(['user_id'], [
|
||||
'name' => 'user_id',
|
||||
'unique' => false,
|
||||
]);
|
||||
}
|
||||
if (!$table->hasIndexByName('open_id')) {
|
||||
$table->addIndex(['open_id'], [
|
||||
'name' => 'open_id',
|
||||
'unique' => false,
|
||||
]);
|
||||
}
|
||||
if (!$table->hasIndexByName('union_id')) {
|
||||
$table->addIndex(['union_id'], [
|
||||
'name' => 'union_id',
|
||||
'unique' => false,
|
||||
]);
|
||||
}
|
||||
$table->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user