Browse Source

微信首次登录添加手机验证码并查询同手机号会员用户绑定

杨展威 1 năm trước cách đây
mục cha
commit
25aa06ec85

+ 4 - 2
feifan-module-member/feifan-module-member-biz/src/main/java/cn/newfeifan/mall/module/member/controller/app/auth/AppAuthController.java

@@ -95,7 +95,9 @@ public class AppAuthController {
     @Operation(summary = "社交快捷登录-校验手机验证码")
     public CommonResult<AppAuthLoginRespVO> socialLoginValidateSmsCode( @RequestBody @Validated SocialLoginValidateSmsCodeReqVO requestVO) {
         authService.validateSmsCode(getLoginUserId(), requestVO.getReqVO());
-        return success(authService.firstSocialLogin(requestVO.getLoginReqVO(),requestVO.getReqVO().getCode()));
+        AppAuthSocialLoginReqVO loginReqVO = requestVO.getLoginReqVO();
+        String code = requestVO.getReqVO().getCode();
+        return success(authService.socialLogin(loginReqVO, code, false));
     }
 
     // ========== 社交登录相关 ==========
@@ -114,7 +116,7 @@ public class AppAuthController {
     @PostMapping("/social-login")
     @Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户")
     public CommonResult<AppAuthLoginRespVO> socialLogin(@RequestBody @Valid AppAuthSocialLoginReqVO reqVO) {
-        return success(authService.socialLogin(reqVO,null));
+        return success(authService.socialLogin(reqVO,null, true));
     }
 
     @PostMapping("/weixin-mini-app-login")

+ 2 - 2
feifan-module-member/feifan-module-member-biz/src/main/java/cn/newfeifan/mall/module/member/service/auth/MemberAuthService.java

@@ -43,7 +43,7 @@ public interface MemberAuthService {
      * @param phone 用户手机号
      * @return 登录结果
      */
-    AppAuthLoginRespVO socialLogin(@Valid AppAuthSocialLoginReqVO reqVO, String phone);
+    AppAuthLoginRespVO socialLogin(@Valid AppAuthSocialLoginReqVO reqVO, String phone, Boolean isFirst);
 
     /**
      * 首次社交登录,自动注册账号
@@ -52,7 +52,7 @@ public interface MemberAuthService {
      * @param phone 手机号
      * @return 登录结果
      */
-    AppAuthLoginRespVO firstSocialLogin(AppAuthSocialLoginReqVO reqVO,String phone);
+    AppAuthLoginRespVO smsCodeLogin(AppAuthSocialLoginReqVO reqVO, String phone);
 
     /**
      * 微信小程序的一键登录

+ 10 - 10
feifan-module-member/feifan-module-member-biz/src/main/java/cn/newfeifan/mall/module/member/service/auth/MemberAuthServiceImpl.java

@@ -102,10 +102,10 @@ public class MemberAuthServiceImpl implements MemberAuthService {
 
     @Override
     @Transactional
-    public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO, String phone) {
+    public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO, String phone, Boolean isFirst) {
         // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
-        SocialUserRespDTO socialUser = socialUserApi.getFirstSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
-                reqVO.getCode(), reqVO.getState());
+        SocialUserRespDTO socialUser = socialUserApi.getSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
+                reqVO.getCode(), reqVO.getState(), isFirst);
         if (socialUser == null) {
             throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
         }
@@ -141,13 +141,13 @@ public class MemberAuthServiceImpl implements MemberAuthService {
 
     @Override
     @Transactional
-    public AppAuthLoginRespVO firstSocialLogin(AppAuthSocialLoginReqVO reqVO, String phone) {
-        SocialUserRespDTO socialUser = socialUserApi.getFirstSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
-                reqVO.getCode(), reqVO.getState());
-        if (socialUser == null) {
-            throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
-        }
-        return socialLogin(reqVO, phone);
+    public AppAuthLoginRespVO smsCodeLogin(AppAuthSocialLoginReqVO reqVO, String phone) {
+//        SocialUserRespDTO socialUser = socialUserApi.getFirstSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
+//                reqVO.getCode(), reqVO.getState());
+//        if (socialUser == null) {
+//            throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
+//        }
+        return socialLogin(reqVO, phone, false);
     }
 
     @Override

+ 1 - 1
feifan-module-system/feifan-module-system-api/src/main/java/cn/newfeifan/mall/module/system/api/social/SocialUserApi.java

@@ -50,6 +50,6 @@ public interface SocialUserApi {
      * @param state state
      * @return 社交用户
      */
-    SocialUserRespDTO getFirstSocialUserByCode(Integer userType, Integer socialType, String code, String state);
+    SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state, Boolean isFirst);
 
 }

+ 2 - 2
feifan-module-system/feifan-module-system-biz/src/main/java/cn/newfeifan/mall/module/system/api/social/SocialUserApiImpl.java

@@ -38,8 +38,8 @@ public class SocialUserApiImpl implements SocialUserApi {
     }
 
     @Override
-    public SocialUserRespDTO getFirstSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
-       return socialUserService.getFirstSocialUserByCode(userType, socialType, code, state);
+    public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state, Boolean isFirst) {
+       return socialUserService.getSocialUserByCode(userType, socialType, code, state, isFirst);
     }
 
 }

+ 1 - 1
feifan-module-system/feifan-module-system-biz/src/main/java/cn/newfeifan/mall/module/system/service/social/SocialUserService.java

@@ -79,7 +79,7 @@ public interface SocialUserService {
      * @param state state
      * @return 社交用户
      */
-    SocialUserRespDTO getFirstSocialUserByCode(Integer userType, Integer socialType, String code, String state);
+    SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state, Boolean isFirst);
 
     // ==================== 社交用户 CRUD ====================
 

+ 11 - 22
feifan-module-system/feifan-module-system-biz/src/main/java/cn/newfeifan/mall/module/system/service/social/SocialUserServiceImpl.java

@@ -26,8 +26,7 @@ import java.util.List;
 import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.newfeifan.mall.framework.common.util.collection.CollectionUtils.convertSet;
 import static cn.newfeifan.mall.framework.common.util.json.JsonUtils.toJsonString;
-import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.AUTH_LOGIN_USER_IS_FIRST;
-import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND;
+import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.*;
 
 /**
  * 社交用户 Service 实现类
@@ -120,9 +119,9 @@ public class SocialUserServiceImpl implements SocialUserService {
                 socialUserBind != null ? socialUserBind.getUserId() : null);
     }
     @Override
-    public SocialUserRespDTO getFirstSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
+    public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state, Boolean isFirst) {
         // 获得社交用户
-        SocialUserDO socialUser = firstAuthSocialUser(socialType, userType, code, state);
+        SocialUserDO socialUser = authSocialUser(socialType, userType, code, state,isFirst);
         Assert.notNull(socialUser, "社交用户不能为空");
 
         // 获得绑定用户
@@ -132,6 +131,10 @@ public class SocialUserServiceImpl implements SocialUserService {
                 socialUserBind != null ? socialUserBind.getUserId() : null);
     }
 
+    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state) {
+        return this.authSocialUser(socialType, userType, code, state, false);
+    }
+
     /**
      * 授权获得对应的社交用户
      * 如果授权失败,则会抛出 {@link ServiceException} 异常
@@ -143,7 +146,7 @@ public class SocialUserServiceImpl implements SocialUserService {
      * @return 授权用户
      */
     @NotNull
-    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state) {
+    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state, boolean isFirst) {
         // 优先从 DB 中获取,因为 code 有且可以使用一次。
         // 在社交登录时,当未绑定 User 时,需要绑定登录,此时需要 code 使用两次
         SocialUserDO socialUser = socialUserMapper.selectByTypeAndCodeAnState(socialType, code, state);
@@ -157,6 +160,9 @@ public class SocialUserServiceImpl implements SocialUserService {
 
         // 保存到 DB 中
         socialUser = socialUserMapper.selectByTypeAndOpenid(socialType, authUser.getUuid());
+        if (isFirst && socialUser == null) {
+            throw exception(AUTH_LOGIN_USER_IS_FIRST);
+        }
         if (socialUser == null) {
             socialUser = new SocialUserDO();
         }
@@ -171,23 +177,6 @@ public class SocialUserServiceImpl implements SocialUserService {
         return socialUser;
     }
 
-    /**
-     * 调用authSocialUser方法做一个中转判断
-     * @param socialType 社交平台的类型 {@link SocialTypeEnum}
-     * @param userType 用户类型
-     * @param code     授权码
-     * @param state    state
-     * @return 授权用户
-     */
-    public SocialUserDO firstAuthSocialUser(Integer socialType, Integer userType, String code, String state) {
-        // 优先从 DB 中获取,因为 code 有且可以使用一次。
-        SocialUserDO socialUser = socialUserMapper.selectByTypeAndCodeAnState(socialType, code, state);
-        if(socialUser == null){
-            throw exception(AUTH_LOGIN_USER_IS_FIRST);
-        }
-
-        return authSocialUser(socialType, userType, code, state);
-    }
 
     // ==================== 社交用户 CRUD ====================