Jelajahi Sumber

优化:微信快捷登录失败时显示错误信息

萤火科技 5 tahun lalu
induk
melakukan
4b1e4e1ca0
2 mengubah file dengan 15 tambahan dan 4 penghapusan
  1. 7 2
      app/api/controller/Passport.php
  2. 8 2
      app/api/service/passport/Login.php

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

@@ -50,15 +50,20 @@ class Passport extends Controller
      * 微信小程序快捷登录 (需提交wx.login接口返回的code、微信用户公开信息)
      * 实现流程:判断openid是否存在 -> 存在:  更新用户登录信息 -> 返回userId和token
      *                          -> 不存在: 返回false, 跳转到注册页面
+     * @return array|\think\response\Json
+     * @throws \app\common\exception\BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
     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'],

+ 8 - 2
app/api/service/passport/Login.php

@@ -68,8 +68,14 @@ class Login extends BaseService
      */
     public function mpWxLogin(array $data)
     {
-        // 根据code换取openid
-        $wxSession = OauthService::wxCode2Session($data['code']);
+        try {
+            // 根据code换取openid
+            $wxSession = OauthService::wxCode2Session($data['code']);
+        } catch (BaseException $e) {
+            // showError参数表示让前端显示错误
+            throwError($e->getMessage(), null, ['showError' => true]);
+            return false;
+        }
         // 判断openid是否存在
         $userId = OauthService::getUserIdByOauthId($wxSession['openid'], 'MP-WEIXIN');
         // 获取用户信息