diff --git a/app/Http/Api/Controllers/ConnectController.php b/app/Http/Api/Controllers/ConnectController.php index cf34611c..9f06fbd2 100644 --- a/app/Http/Api/Controllers/ConnectController.php +++ b/app/Http/Api/Controllers/ConnectController.php @@ -1,4 +1,9 @@ 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); } diff --git a/app/Http/Api/Services/Connect.php b/app/Http/Api/Services/Connect.php index f64b01e5..fde7af49 100644 --- a/app/Http/Api/Services/Connect.php +++ b/app/Http/Api/Services/Connect.php @@ -1,4 +1,9 @@ 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'); diff --git a/app/Http/Home/Controllers/ConnectController.php b/app/Http/Home/Controllers/ConnectController.php index f9bf060d..395556b3 100644 --- a/app/Http/Home/Controllers/ConnectController.php +++ b/app/Http/Home/Controllers/ConnectController.php @@ -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']); } } diff --git a/app/Http/Home/Services/Connect.php b/app/Http/Home/Services/Connect.php index 3f598fab..df7b7c9f 100644 --- a/app/Http/Home/Services/Connect.php +++ b/app/Http/Home/Services/Connect.php @@ -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); } } diff --git a/app/Http/Home/Services/WeChatOfficialAccount.php b/app/Http/Home/Services/WeChatOfficialAccount.php index d04ec722..038581dd 100644 --- a/app/Http/Home/Services/WeChatOfficialAccount.php +++ b/app/Http/Home/Services/WeChatOfficialAccount.php @@ -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; + } + } diff --git a/app/Repos/WeChatSubscribe.php b/app/Repos/WeChatSubscribe.php index f00ce785..bec577d5 100644 --- a/app/Repos/WeChatSubscribe.php +++ b/app/Repos/WeChatSubscribe.php @@ -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], + ]); + } + } diff --git a/app/Services/OAuth/WeChat.php b/app/Services/OAuth/WeChat.php index 29e541a3..98a1390e 100644 --- a/app/Services/OAuth/WeChat.php +++ b/app/Services/OAuth/WeChat.php @@ -1,4 +1,9 @@ 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(); } }