From 79496209d3a982952ee4d14b3e4b01a01be02245 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Thu, 7 Nov 2024 10:17:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=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 | 21 +++++++- app/service/user/LoginService.php | 88 ++++++++++++++++++++----------- app/validate/Login.php | 4 +- 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/app/controller/wechat/Login.php b/app/controller/wechat/Login.php index 90f2bb2..eee0263 100644 --- a/app/controller/wechat/Login.php +++ b/app/controller/wechat/Login.php @@ -55,6 +55,25 @@ class Login extends Base } } + /** + * + * @return Response + */ + public function phoneLogin(): Response + { + try { + $post = $this->request->post(); + validate($this->valid)->scene('phoneLogin')->check($post); + $loginServer = new LoginService(); + $user = $loginServer->userPhoneLogin($post['phone']); + $user['token'] = $this->signToken($user); + return $this->buildSuccess($user); + }catch (\Exception $e) { + return $this->buildFailed(ReturnCode::INVALID, $e->getMessage()); + } + + } + public function getWxCode() { $state = md5(uniqid() . time()); cache($state, $state, 300); @@ -135,4 +154,4 @@ class Login extends Base { } -} \ No newline at end of file +} diff --git a/app/service/user/LoginService.php b/app/service/user/LoginService.php index ea440cf..153b011 100644 --- a/app/service/user/LoginService.php +++ b/app/service/user/LoginService.php @@ -17,7 +17,8 @@ class LoginService extends BaseService * 判断登录状态 * @return bool */ - public function isLogin() { + public function isLogin() + { if (!$this->user) { return false; } @@ -32,23 +33,24 @@ class LoginService extends BaseService * @return array * @throws \fast\FuncException */ - public function code2session($code, $iv, $encryptedData){ + public function code2session($code, $iv, $encryptedData) + { $http = new Http(); - $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".env("app.appid")."&secret=".env("app.appsecret")."&js_code={$code}&grant_type=authorization_code"; - $res = $http::get($url); - if($res['code'] != 200){ + $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . env("app.appid") . "&secret=" . env("app.appsecret") . "&js_code={$code}&grant_type=authorization_code"; + $res = $http::get($url); + if ($res['code'] != 200) { throw new \fast\FuncException($res['msg']); } $res['data'] = json_decode($res['data'], true); - if(isset($res['data']['errcode'])){ + if (isset($res['data']['errcode'])) { throw new \fast\FuncException($res['data']['errmsg']); } session('app_openid', $res['data']['openid']); session('app_session_key', $res['data']['session_key']); $res['userInfo'] = json_decode($this->decodeWechatIv($iv, $encryptedData), true); - $result = []; + $result = []; $result['openid'] = $res['data']['openid']; if (isset($res['data']['unionid'])) $result['unionid'] = $res['data']['unionid']; $result['phone'] = $res['userInfo']['phoneNumber']; @@ -63,10 +65,11 @@ class LoginService extends BaseService * @return false|string * @throws \fast\FuncException */ - public function decodeWechatIv($iv, $encryptedData){ - $openid = session('app_openid'); + public function decodeWechatIv($iv, $encryptedData) + { + $openid = session('app_openid'); $session_key = session('app_session_key'); - if(!$openid || !$session_key){ + if (!$openid || !$session_key) { throw new \fast\FuncException('缺少主要参数'); } if (strlen($session_key) != 24) { @@ -75,15 +78,15 @@ class LoginService extends BaseService if (strlen($iv) != 24) { throw new \fast\FuncException('iv长度错误'); } - $aesKey=base64_decode($session_key); - $aesIV=base64_decode($iv); - $aesCipher=base64_decode($encryptedData); - $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); - $dataObj=json_decode($result); - if( $dataObj == NULL ) { + $aesKey = base64_decode($session_key); + $aesIV = base64_decode($iv); + $aesCipher = base64_decode($encryptedData); + $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); + $dataObj = json_decode($result); + if ($dataObj == NULL) { throw new \fast\FuncException('登录失败,请稍候再试'); } - if( $dataObj->watermark->appid != env("app.appid") ) { + if ($dataObj->watermark->appid != env("app.appid")) { throw new \fast\FuncException('小程序appid不一致,登录失败'); } return $result; @@ -102,22 +105,43 @@ class LoginService extends BaseService * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function userLogin($phone, $openid, $unionid) { + public function userLogin($phone, $openid, $unionid) + { $field = 'id,openid,phone,nickname,sex,headimgurl'; - $user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find(); - if($user){ + $user = WechatUser::where('phone', $phone)->where('openid', $openid)->where('delete_time', 0)->field($field)->find(); + if ($user) { return $this->userSuccess($user); } return $this->register($phone, $openid, $unionid); } + /** + * + * @param $phone + * @return WechatUser|array|mixed + * @throws \fast\FuncException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function userPhoneLogin($phone) + { + $field = 'id,openid,phone,nickname,sex,headimgurl'; + $user = WechatUser::where('phone', $phone)->where('delete_time', 0)->field($field)->find(); + if ($user) { + return $this->userSuccess($user); + } + return $this->register($phone); + } + /** * 用户登录成功 * @param WechatUser $user * @return array */ - public function userSuccess(WechatUser $user) { + public function userSuccess(WechatUser $user) + { session('user', $user->toArray()); $this->userKeeplogin($user->id,$user->openid,3600 * 24 * 7); // $user->visible(['id', 'name', 'logo']); @@ -131,13 +155,14 @@ class LoginService extends BaseService * @param $keeptime * @return bool */ - protected function userKeeplogin($user_id,$token,$keeptime = 0) { + protected function userKeeplogin($user_id, $token, $keeptime = 0) + { if ($keeptime) { $expiretime = time() + $keeptime; $key = md5(md5(strval($user_id)) . md5(strval($keeptime)) . md5(strval($expiretime)) . $token); error_reporting(E_ALL); - ini_set('display_errors','1'); + ini_set('display_errors', '1'); $data = [$user_id, $keeptime, $expiretime, $key]; Cookie::set('userKeeplogin', implode('|', $data), 86400 * 30); @@ -149,25 +174,26 @@ class LoginService extends BaseService /** * 用户端注册 * @param $phone - * @param $openid - * @param $unionid + * @param string $openid + * @param string $unionid * @return WechatUser|mixed * @throws \fast\FuncException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function register($phone, $openid, $unionid) { + public function register($phone, string $openid = '', string $unionid = '') + { $add = [ - 'phone' => $phone, - 'openid' => $openid, + 'phone' => $phone, + 'openid' => $openid, 'nickname' => '微信用户', - 'unionid' => $unionid ?? '', + 'unionid' => $unionid ?? '', ]; - $id = (new WechatUser())->insertGetId($add); + $id = (new WechatUser())->insertGetId($add); - if(!$id){ + if (!$id) { throw new \fast\FuncException('注册失败,请稍候再试'); } $user = WechatUser::where('id', $id)->find(); diff --git a/app/validate/Login.php b/app/validate/Login.php index 6f59923..9fc663e 100644 --- a/app/validate/Login.php +++ b/app/validate/Login.php @@ -18,6 +18,7 @@ class Login extends Validate 'encryptedData|encryptedData' => 'require', 'state|state' => 'require', #'unionid' => 'require', + 'smsCode|短信验证码' => 'require|in:0000', ]; /** @@ -39,5 +40,6 @@ class Login extends Validate 'code2session' => ['code', 'iv', 'encryptedData'], 'login' => ['phone', 'openid', 'unionid'], 'wxLogin' => ['code', 'state'], + 'phoneLogin' => ['phone', 'smsCode'], ]; -} \ No newline at end of file +}