소스 검색

修复:登录时判断oauth_id是否已绑定

萤火科技 5 년 전
부모
커밋
60252addaf
3개의 변경된 파일과 43개의 추가작업 그리고 27개의 파일을 삭제
  1. 0 1
      app/api/model/UserOauth.php
  2. 23 16
      app/api/service/user/Oauth.php
  3. 20 10
      app/common/model/UserOauth.php

+ 0 - 1
app/api/model/UserOauth.php

@@ -39,5 +39,4 @@ class UserOauth extends UserOauthModel
     {
         return $this->save($data);
     }
-
 }

+ 23 - 16
app/api/service/user/Oauth.php

@@ -37,22 +37,31 @@ class Oauth extends BaseService
      */
     public function party(int $userId, array $partyData = [])
     {
-        // 是否存在第三方用户
-        $model = new UserOauthModel;
-        $isExistOauthType = $model->isExistOauthType($userId, $partyData['oauth']);
-        if ($isExistOauthType) {
-            return true;
+        try {
+            // 获取oauthId和unionId
+            $oauthInfo = $this->getOauthInfo($partyData);
+        } catch (BaseException $e) {
+            // isBack参数代表需重新获取code, 前端拿到该参数进行页面返回
+            throwError($e->getMessage(), null, ['isBack' => true]);
+            return false;
         }
-        // 获取oauth_id和unionId
-        $oauthInfo = $this->getOauthInfo($partyData);
+        // 是否存在第三方用户
+        $oauthId = UserOauthModel::getOauthIdByUserId($userId, $partyData['oauth']);
         // 如果不存在oauth则写入
-        return $model->add([
-            'user_id' => $userId,
-            'oauth_type' => $partyData['oauth'],
-            'oauth_id' => $oauthInfo['oauth_id'],
-            'unionid' => $oauthInfo['unionid'] ?? '',   // unionid可以不存在
-            'store_id' => $this->storeId
-        ]);
+        if (empty($oauthId)) {
+            return (new UserOauthModel)->add([
+                'user_id' => $userId,
+                'oauth_type' => $partyData['oauth'],
+                'oauth_id' => $oauthInfo['oauth_id'],
+                'unionid' => $oauthInfo['unionid'] ?? '',   // unionid可以不存在
+                'store_id' => $this->storeId
+            ]);
+        }
+        // 如果存在第三方用户, 需判断oauthId是否相同
+        if ($oauthId != $oauthInfo['oauth_id']) {
+            throwError('很抱歉,当前手机号已绑定其他微信号', null, ['isBack' => true]);
+        }
+        return true;
     }
 
     /**
@@ -103,6 +112,4 @@ class Oauth extends BaseService
     {
         return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
     }
-
-
 }

+ 20 - 10
app/common/model/UserOauth.php

@@ -26,31 +26,41 @@ class UserOauth extends BaseModel
     protected $pk = 'id';
 
     /**
-     * 是否存在第三方用户
-     * @param int $userId 用户ID
+     * 根据openid获取用户ID
+     * @param string $oauthId 第三方用户唯一标识 (openid)
      * @param string $oauthType 第三方登陆类型
      * @return mixed
      */
-    public function isExistOauthType(int $userId, string $oauthType)
+    public static function getUserIdByOauthId(string $oauthId, string $oauthType)
     {
-        return $this->where('user_id', '=', $userId)
+        return (new static)->where('oauth_id', '=', $oauthId)
             ->where('oauth_type', '=', $oauthType)
             ->where('is_delete', '=', 0)
-            ->value($this->getPk());
+            ->value('user_id');
     }
 
     /**
-     * 根据openid获取用户ID
-     * @param string $oauthId 第三方用户唯一标识 (openid)
+     * 根据用户ID获取OpenID
+     * @param int $userId 用户ID
      * @param string $oauthType 第三方登陆类型
      * @return mixed
      */
-    public static function getUserIdByOauthId(string $oauthId, string $oauthType)
+    public static function getOauthIdByUserId(int $userId, string $oauthType)
     {
-        return (new static)->where('oauth_id', '=', $oauthId)
+        return (new static)->where('user_id', '=', $userId)
             ->where('oauth_type', '=', $oauthType)
             ->where('is_delete', '=', 0)
-            ->value('user_id');
+            ->value('oauth_id');
+    }
+
+    /**
+     * 根据用户ID获取微信小程序OpenID
+     * @param int $userId 用户ID
+     * @return mixed
+     */
+    public static function getMpWeiXinOpenId(int $userId)
+    {
+        return static::getOauthIdByUserId($userId, 'MP-WEIXIN');
     }
 
     /**