mirror of
https://gitee.com/koogua/course-tencent-cloud.git
synced 2025-06-24 20:06:09 +08:00
* Merge remote-tracking branch 'gitee/xiaochong0302/I280IZ' into xiaocho… * 初步完成开放登录,待线上测试7 * Merge branch 'demo' of gitee.com:koogua/course-tencent-cloud into xiao… * 初步完成开放登录,待线上测试6 * !30 开放登录线上测试5 * !29 开放登录线上测试5 * 初步完成开放登录,待线上测试5 * !28 开放登录线上测试4 * 初步完成开放登录,待线上测试4 * !27 开放登录线上测试3 * 初步完成开放登录,待线上测试3 * !26 开放登录线上测试2 * 初步完成开放登录,待线上测试2 * !25 开放登录线上测试 * 初步完成开放登录,待线上测试 * !22 验证更新h5支付 * Merge remote-tracking branch 'remotes/gitee/develop' into demo * !20 验证更新h5支付 * Merge branch 'develop' of https://gitee.com/koogua/course-tencent-clou… * !16 v1.2.0阶段性合并 * 删除调试断点代码 * 删除重复的signature方法 * Merge branch 'develop' of https://gitee.com/koogua/course-tencent-clou… * demo后台增加统计 * !5 更新版本号1.1.0 * !4 v1.1.0版本develop->demo * Merge branch 'develop' into demo * 1.增加changelog.md * Merge branch 'develop' into demo * Merge branch 'develop' into demo * Merge branch 'develop' into demo * !1 精简优化代码 * Merge branch 'develop' into demo * 合并修改
209 lines
5.0 KiB
PHP
209 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Home\Services;
|
|
|
|
use App\Models\Connect as ConnectModel;
|
|
use App\Models\User as UserModel;
|
|
use App\Repos\Connect as ConnectRepo;
|
|
use App\Repos\User as UserRepo;
|
|
use App\Services\Auth\Home as AuthService;
|
|
use App\Services\Logic\Account\Register as RegisterService;
|
|
use App\Services\OAuth\QQ as QQAuth;
|
|
use App\Services\OAuth\WeiBo as WeiBoAuth;
|
|
use App\Services\OAuth\WeiXin as WeiXinAuth;
|
|
use App\Validators\Account as AccountValidator;
|
|
|
|
class Connect extends Service
|
|
{
|
|
|
|
public function bindLogin()
|
|
{
|
|
$post = $this->request->getPost();
|
|
|
|
$auth = $this->getConnectAuth($post['provider']);
|
|
|
|
$auth->checkState($post['state']);
|
|
|
|
$validator = new AccountValidator();
|
|
|
|
$user = $validator->checkUserLogin($post['account'], $post['password']);
|
|
|
|
$openUser = json_decode($post['open_user'], true);
|
|
|
|
$this->handleConnectRelation($user, $openUser, $post['provider']);
|
|
|
|
$auth = $this->getAppAuth();
|
|
|
|
$auth->saveAuthInfo($user);
|
|
}
|
|
|
|
public function bindRegister()
|
|
{
|
|
$post = $this->request->getPost();
|
|
|
|
$auth = $this->getConnectAuth($post['provider']);
|
|
|
|
$auth->checkState($post['state']);
|
|
|
|
$openUser = json_decode($post['open_user'], true);
|
|
|
|
$registerService = new RegisterService();
|
|
|
|
$account = $registerService->handle();
|
|
|
|
$userRepo = new UserRepo();
|
|
|
|
$user = $userRepo->findById($account->id);
|
|
|
|
$this->handleConnectRelation($user, $openUser, $post['provider']);
|
|
|
|
$auth = $this->getAppAuth();
|
|
|
|
$auth->saveAuthInfo($user);
|
|
}
|
|
|
|
public function bindUser($openUser, $provider)
|
|
{
|
|
$user = $this->getLoginUser();
|
|
|
|
$this->handleConnectRelation($user, $openUser, $provider);
|
|
}
|
|
|
|
public function authLogin(ConnectModel $connect)
|
|
{
|
|
$userRepo = new UserRepo();
|
|
|
|
$user = $userRepo->findById($connect->user_id);
|
|
|
|
$auth = $this->getAppAuth();
|
|
|
|
$auth->saveAuthInfo($user);
|
|
}
|
|
|
|
public function getAuthorizeUrl($provider)
|
|
{
|
|
$auth = $this->getConnectAuth($provider);
|
|
|
|
return $auth->getAuthorizeUrl();
|
|
}
|
|
|
|
public function getOpenUserInfo($code, $state, $provider)
|
|
{
|
|
$auth = $this->getConnectAuth($provider);
|
|
|
|
$auth->checkState($state);
|
|
|
|
$token = $auth->getAccessToken($code);
|
|
|
|
$openId = $auth->getOpenId($token);
|
|
|
|
return $auth->getUserInfo($token, $openId);
|
|
}
|
|
|
|
public function getConnectRelation($openId, $provider)
|
|
{
|
|
$connectRepo = new ConnectRepo();
|
|
|
|
return $connectRepo->findByOpenId($openId, $provider);
|
|
}
|
|
|
|
public function getConnectAuth($provider)
|
|
{
|
|
$auth = null;
|
|
|
|
switch ($provider) {
|
|
case ConnectModel::PROVIDER_QQ:
|
|
$auth = $this->getQQAuth();
|
|
break;
|
|
case ConnectModel::PROVIDER_WEIXIN:
|
|
$auth = $this->getWeiXinAuth();
|
|
break;
|
|
case ConnectModel::PROVIDER_WEIBO:
|
|
$auth = $this->getWeiBoAuth();
|
|
break;
|
|
}
|
|
|
|
if (!$auth) {
|
|
throw new \Exception('Invalid OAuth Provider');
|
|
}
|
|
|
|
return $auth;
|
|
}
|
|
|
|
protected function getQQAuth()
|
|
{
|
|
$settings = $this->getSettings('oauth.qq');
|
|
|
|
return new QQAuth(
|
|
$settings['client_id'],
|
|
$settings['client_secret'],
|
|
$settings['redirect_uri']
|
|
);
|
|
}
|
|
|
|
protected function getWeiXinAuth()
|
|
{
|
|
$settings = $this->getSettings('oauth.weixin');
|
|
|
|
return new WeiXinAuth(
|
|
$settings['client_id'],
|
|
$settings['client_secret'],
|
|
$settings['redirect_uri']
|
|
);
|
|
}
|
|
|
|
protected function getWeiBoAuth()
|
|
{
|
|
$settings = $this->getSettings('oauth.weibo');
|
|
|
|
return new WeiBoAuth(
|
|
$settings['client_id'],
|
|
$settings['client_secret'],
|
|
$settings['redirect_uri']
|
|
);
|
|
}
|
|
|
|
protected function getAppAuth()
|
|
{
|
|
/**
|
|
* @var $auth AuthService
|
|
*/
|
|
$auth = $this->getDI()->get('auth');
|
|
|
|
return $auth;
|
|
}
|
|
|
|
protected function handleConnectRelation(UserModel $user, array $openUser, $provider)
|
|
{
|
|
$connectRepo = new ConnectRepo();
|
|
|
|
$connect = $connectRepo->findByOpenId($openUser['id'], $provider);
|
|
|
|
if ($connect) {
|
|
|
|
if (time() - $connect->update_time > 86400) {
|
|
$connect->open_name = $openUser['name'];
|
|
$connect->open_avatar = $openUser['avatar'];
|
|
}
|
|
|
|
if ($connect->deleted == 1) {
|
|
$connect->deleted = 0;
|
|
$connect->update();
|
|
}
|
|
|
|
} else {
|
|
|
|
$connect = new ConnectModel();
|
|
|
|
$connect->user_id = $user->id;
|
|
$connect->open_id = $openUser['id'];
|
|
$connect->open_name = $openUser['name'];
|
|
$connect->open_avatar = $openUser['avatar'];
|
|
$connect->provider = $provider;
|
|
|
|
$connect->create();
|
|
}
|
|
}
|
|
|
|
}
|