From 69550c29c0fe25092e477e40b24f806547f1200a Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Fri, 26 Jul 2024 14:25:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7h5?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=B5=8B=E8=AF=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/wechat/Login.php | 67 +++++++++++++++++++++++++++++++++ app/validate/Login.php | 4 +- route/wechatRoute.php | 2 + 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/app/controller/wechat/Login.php b/app/controller/wechat/Login.php index 2f00102..87d258f 100644 --- a/app/controller/wechat/Login.php +++ b/app/controller/wechat/Login.php @@ -2,8 +2,12 @@ namespace app\controller\wechat; +use app\model\WechatUser; use app\service\user\LoginService; +use app\util\ReturnCode; +use think\facade\Cache; use think\facade\Request; +use think\Response; class Login extends Base { @@ -51,6 +55,69 @@ class Login extends Base } } + public function getWxCode() { + $state = md5(uniqid() . time()); + cache($state, $state, 300); + + return $this->buildSuccess([ + 'appId' => env('wechat.appid'), + 'redirectUri' => urlencode(env('wechat.redirect_uri')), + 'state' => $state + ]); + } + + public function wx(): Response { + validate($this->valid)->scene('wxLogin')->check(Request::post()); + $state = $this->request->post('state', ''); + $code = $this->request->post('code', ''); + + //验证合法性 + $cacheData = Cache::has($state); + if (!$cacheData) { + return $this->buildFailed(ReturnCode::SESSION_TIMEOUT, 'state已过期'); + } else { + cache($state, null); + } + + //获取AccessToken + $getAccessTokenUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . + env('wechat.appid') . '&secret=' . env('app.appsecret') . '&code=' . $code . + '&grant_type=authorization_code'; + + $tokenArr = file_get_contents($getAccessTokenUrl); + $accessTokenArr = json_decode($tokenArr, true); + + //获取openId + $getUserIdUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessTokenArr['access_token'] . '&openid=' . $accessTokenArr['openid']; + $userIdArr = file_get_contents($getUserIdUrl); + $userIdArr = json_decode($userIdArr, true); + + return $this->doLogin($userIdArr['openid'], [ + 'nickname' => $userIdArr['nickname'], + 'head_img' => $userIdArr['headimgurl'] + ]); + } + + private function doLogin(string $openid, array $userDetail): Response { + $userInfo = (new WechatUser())->where('openid', $openid)->find(); + if (empty($userInfo)) { + $userInfo = WechatUser::create([ + 'nickname' => $userDetail['nickname'], + 'openid' => $openid, + 'create_time' => time(), + 'headimgurl' => $userDetail['head_img'], + ]); + } else { + if (!$userInfo['status']) { + return $this->buildFailed(ReturnCode::LOGIN_ERROR, '用户已被封禁,请联系管理员'); + } + } + + $data = $userInfo->toArray(); + unset($data['delete_time'],$data['status']); + return $this->buildSuccess($data, '登录成功'); + } + public function userLogout() { diff --git a/app/validate/Login.php b/app/validate/Login.php index e9f0ec2..6f59923 100644 --- a/app/validate/Login.php +++ b/app/validate/Login.php @@ -16,6 +16,7 @@ class Login extends Validate 'openid|openid' => 'require', 'iv|iv' => 'require', 'encryptedData|encryptedData' => 'require', + 'state|state' => 'require', #'unionid' => 'require', ]; @@ -36,6 +37,7 @@ class Login extends Validate */ protected $scene = [ 'code2session' => ['code', 'iv', 'encryptedData'], - 'login' => ['phone', 'openid', 'unionid'] + 'login' => ['phone', 'openid', 'unionid'], + 'wxLogin' => ['code', 'state'], ]; } \ No newline at end of file diff --git a/route/wechatRoute.php b/route/wechatRoute.php index 9f883b9..3a3c940 100644 --- a/route/wechatRoute.php +++ b/route/wechatRoute.php @@ -9,6 +9,8 @@ Route::group('wechat', function() { // 微信验证路由地址 Route::rule('Login/code2session', 'wechat.Login/code2session', 'post'); Route::rule('Login/login', 'wechat.Login/login', 'post'); + Route::rule('Login/getWxCode', 'wechat.Login/getWxCode', 'get'); + Route::rule('Login/wx', 'wechat.Login/wx', 'post'); Route::rule('Wechat/index', 'wechat.Wechat/index', 'get'); });