Explorar o código

新增:微信小程序快捷登录

萤火科技 %!s(int64=5) %!d(string=hai) anos
pai
achega
bedf24d0d6

+ 22 - 2
app/api/controller/Passport.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\api\controller;
 
@@ -24,7 +24,7 @@ use app\api\service\passport\SmsCaptcha as SmsCaptchaService;
 class Passport extends Controller
 {
     /**
-     * 登录接口
+     * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
      * @return array|\think\response\Json
      * @throws \app\common\exception\BaseException
      * @throws \think\db\exception\DataNotFoundException
@@ -46,6 +46,26 @@ class Passport extends Controller
         ], '登录成功');
     }
 
+    /**
+     * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
+     * 实现流程:判断openid是否存在 -> 存在:  更新用户登录信息 -> 返回userId和token
+     *                          -> 不存在: 返回false, 跳转到注册页面
+     */
+    public function mpWxLogin()
+    {
+        // 执行登录
+        $LoginService = new LoginService;
+        if (!$LoginService->mpWxLogin($this->postForm())) {
+            return $this->renderError($LoginService->getError());
+        }
+        // 用户信息
+        $userInfo = $LoginService->getUserInfo();
+        return $this->renderSuccess([
+            'userId' => (int)$userInfo['user_id'],
+            'token' => $LoginService->getToken((int)$userInfo['user_id'])
+        ], '登录成功');
+    }
+
     /**
      * 图形验证码
      * @return array|\think\response\Json

+ 27 - 0
app/api/service/passport/Login.php

@@ -57,6 +57,33 @@ class Login extends BaseService
         return $this->session();
     }
 
+    /**
+     * 快捷登录:微信小程序用户
+     * @param array $data
+     * @return bool
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function mpWxLogin(array $data)
+    {
+        // 根据code换取openid
+        $wxSession = OauthService::wxCode2Session($data['code']);
+        // 判断openid是否存在
+        $userId = OauthService::getUserIdByOauthId($wxSession['openid'], 'MP-WEIXIN');
+        // 获取用户信息
+        $userInfo = !empty($userId) ? UserModel::detail($userId) : null;
+        if (empty($userId) || empty($userInfo)) {
+            $this->error = '第三方用户不存在';
+            return false;
+        }
+        // 更新用户登录信息
+        $this->updateUser($userInfo, true, $data);
+        // 记录登录态
+        return $this->session();
+    }
+
     /**
      * 保存oauth信息
      * @param array $data

+ 14 - 2
app/api/service/user/Oauth.php

@@ -67,7 +67,7 @@ class Oauth extends BaseService
     private function getOauthInfo(array $partyData)
     {
         if ($partyData['oauth'] === 'MP-WEIXIN') {
-            $wxSession = $this->wxCode2Session($partyData['code']);
+            $wxSession = static::wxCode2Session($partyData['code']);
             return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
         }
         return null;
@@ -82,7 +82,7 @@ class Oauth extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function wxCode2Session(string $code)
+    public static function wxCode2Session(string $code)
     {
         // 获取当前小程序信息
         $wxConfig = WxappModel::getWxappCache();
@@ -93,4 +93,16 @@ class Oauth extends BaseService
         return $result;
     }
 
+    /**
+     * 根据openid获取用户ID
+     * @param string $oauthId 第三方用户唯一标识 (openid)
+     * @param string $oauthType 第三方登陆类型
+     * @return mixed
+     */
+    public static function getUserIdByOauthId(string $oauthId, string $oauthType)
+    {
+        return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
+    }
+
+
 }

+ 14 - 1
app/common/model/UserOauth.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model;
 
@@ -38,6 +38,19 @@ class UserOauth extends BaseModel
             ->value($this->getPk());
     }
 
+    /**
+     * 根据openid获取用户ID
+     * @param string $oauthId 第三方用户唯一标识 (openid)
+     * @param string $oauthType 第三方登陆类型
+     * @return mixed
+     */
+    public static function getUserIdByOauthId(string $oauthId, string $oauthType)
+    {
+        return (new static)->where('oauth_id', '=', $oauthId)
+            ->where('oauth_type', '=', $oauthType)
+            ->value('user_id');
+    }
+
     /**
      * 获取第三方用户信息
      * @param int $userId 用户ID