소스 검색

手机号一键登录

whitefang 1 년 전
부모
커밋
425ee30a89
3개의 변경된 파일97개의 추가작업 그리고 3개의 파일을 삭제
  1. 19 2
      app/api/controller/LoginController.php
  2. 66 1
      app/api/logic/LoginLogic.php
  3. 12 0
      app/api/validate/WechatLoginValidate.php

+ 19 - 2
app/api/controller/LoginController.php

@@ -28,7 +28,7 @@ use app\api\logic\LoginLogic;
 class LoginController extends BaseApiController
 class LoginController extends BaseApiController
 {
 {
 
 
-    public array $notNeedLogin = ['account', 'logout', 'codeUrl', 'oaLogin',  'mnpLogin', 'getScanCode', 'scanLogin', 'firmLogin','getMnpSessionKey'];
+    public array $notNeedLogin = ['account', 'logout', 'codeUrl', 'oaLogin',  'mnpLogin', 'getScanCode', 'scanLogin', 'firmLogin','getMnpSessionKey','mnpAuthPhone'];
 
 
     /**
     /**
      * @notes 账号密码/手机号密码/手机号验证码登录
      * @notes 账号密码/手机号密码/手机号验证码登录
@@ -121,12 +121,14 @@ class LoginController extends BaseApiController
     }
     }
 
 
     /**
     /**
+     *
+     * 手机号一键登录获取key
      * @return \think\response\Json
      * @return \think\response\Json
      */
      */
     public function getMnpSessionKey()
     public function getMnpSessionKey()
     {
     {
         $params = (new WechatLoginValidate())->post()->goCheck('wechatAuth');
         $params = (new WechatLoginValidate())->post()->goCheck('wechatAuth');
-        $res = LoginLogic::mnpPhoneCode($params);
+        $res = LoginLogic::mnpSessionKey($params);
         if (false === $res) {
         if (false === $res) {
             return $this->fail(LoginLogic::getError());
             return $this->fail(LoginLogic::getError());
         }
         }
@@ -134,6 +136,21 @@ class LoginController extends BaseApiController
     }
     }
 
 
 
 
+    /**
+     *
+     * 微信小程序手机号一键登录
+     * @return \think\response\Json
+     */
+    public function mnpAuthPhone()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck('mnpAuthPhone');
+        $result = LoginLogic::mnpPhoneLogin($params);
+        if (false === $result) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->data($result);
+    }
+
     /**
     /**
      * @notes 小程序绑定微信
      * @notes 小程序绑定微信
      * @return \think\response\Json
      * @return \think\response\Json

+ 66 - 1
app/api/logic/LoginLogic.php

@@ -243,7 +243,7 @@ class LoginLogic extends BaseLogic
      * @return array|false
      * @return array|false
      *
      *
      */
      */
-    public static function mnpPhoneCode(array $params)
+    public static function mnpSessionKey(array $params)
     {
     {
         Db::startTrans();
         Db::startTrans();
         try {
         try {
@@ -258,6 +258,71 @@ class LoginLogic extends BaseLogic
         }
         }
     }
     }
 
 
+    /**
+     * @notes 小程序-手机号授权一键登录
+     * @param $params
+     * @return array
+     * @author 段誉
+     * @date 2022/9/6 19:26
+     */
+    public static function mnpPhoneLogin(array $params)
+    {
+        try {
+            $where = ['mobile' => $params['mobile']];
+            $params['account'] = $params['mobile'];
+            $user = User::where($where)->findOrEmpty();
+
+            //先检查openid是否有记录
+            $isAuth = UserAuth::where('openid', '=', $params['openid'])->findOrEmpty();
+            if (!$isAuth->isEmpty() && !$user->isEmpty()) {
+                $authUser = UserAuth::where('user_id', '=', $user['id'])->findOrEmpty();
+                if($isAuth->user_id != $authUser->user_id) {
+                    throw new \Exception('该微信已被绑定');
+                }
+            }
+            if ($user->isEmpty()) {
+                //直接注册用户
+                $params['channel'] = 1;
+                $user = self::register($params);
+            }
+
+            //更新登录信息
+            $user->login_time = time();
+            $user->login_ip = request()->ip();
+            $user->save();
+
+            if($isAuth->isEmpty()){
+                UserAuth::create([
+                    'user_id' => $user->id,
+                    'openid' => $params['openid'],
+                    'unionid' => $params['unionid'] ?? '',
+                    'terminal' => 1,
+                ]);
+            }
+
+            if(!$isAuth->isEmpty() && $isAuth->user_id==0){
+                $isAuth->user_id = $user->id;
+                $isAuth->save();
+            }
+            $userInfo = UserTokenService::setToken($user->id, 1);
+
+            //返回登录信息
+            $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
+            $avatar = FileService::getFileUrl($avatar);
+
+            return [
+                'nickname' => $userInfo['nickname'],
+                'sn' => $userInfo['sn'],
+                'mobile' => $userInfo['mobile'],
+                'avatar' => $avatar,
+                'token' => $userInfo['token'],
+            ];
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return [];
+        }
+    }
+
 
 
     /**
     /**
      * @notes 更新登录信息
      * @notes 更新登录信息

+ 12 - 0
app/api/validate/WechatLoginValidate.php

@@ -32,6 +32,7 @@ class WechatLoginValidate extends BaseValidate
         'access_token' => 'require',
         'access_token' => 'require',
         'terminal' => 'require',
         'terminal' => 'require',
         'avatar' => 'require',
         'avatar' => 'require',
+        'mobile' => 'require',
     ];
     ];
 
 
     protected $message = [
     protected $message = [
@@ -42,6 +43,7 @@ class WechatLoginValidate extends BaseValidate
         'access_token.require' => 'access_token缺少',
         'access_token.require' => 'access_token缺少',
         'terminal.require' => '终端参数缺少',
         'terminal.require' => '终端参数缺少',
         'avatar.require' => '头像缺少',
         'avatar.require' => '头像缺少',
+        'mobile.require' => '手机号缺少',
     ];
     ];
 
 
 
 
@@ -92,5 +94,15 @@ class WechatLoginValidate extends BaseValidate
         return $this->only(['nickname', 'avatar']);
         return $this->only(['nickname', 'avatar']);
     }
     }
 
 
+    /**
+     * @notes
+     * @return WechatLoginValidate
+     * @author 段誉
+     * @date 2022/9/16 11:15
+     */
+    public function sceneMnpAuthPhone()
+    {
+        return $this->only(['openid','mobile']);
+    }
 
 
 }
 }