Bläddra i källkod

Merge branch 'dev/2024/0529/update-app-Y' of feifan/mall-backend-app into master

修改登录系统
Yangzw 10 månader sedan
förälder
incheckning
a43dcddcf4

+ 1 - 0
feifan-module-member/feifan-module-member-api/src/main/java/cn/newfeifan/mall/module/member/enums/ErrorCodeConstants.java

@@ -59,5 +59,6 @@ public interface ErrorCodeConstants {
     ErrorCode REDIS_ORDER_PERCENTAGE_NOT_EXISTS = new ErrorCode(1_004_012_002, "缓存中没有找到存储的签到获取的身价值");
     ErrorCode AUTH_USERNAME_EXISTS = new ErrorCode(1_004_012_003, "此用户名已经存在,请换一个!");
     ErrorCode MOBILE_IS_MAXIMIZE = new ErrorCode(1_004_012_004, "此手机号已经达到注册的上限了");
+    ErrorCode AUTH_SOCIAL_USER_BIND_MAX = new ErrorCode(1_004_012_004, "此微信号已经到达绑定的上限了");
 
 }

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

@@ -128,7 +128,7 @@ public class AppAuthController {
     @Operation(summary = "社交快捷注册,使用code授权码",description = "同一个微信账号注册多个消费者账号")
     public CommonResult<AppAuthLoginRespVO> socialRegister(@RequestBody @Valid AppAuthSocialRegisterReqVO requestVO){
 
-        return success(authService.socialRegister(requestVO, null, true, true));
+        return success(authService.socialRegister(requestVO, null, false, false));
     }
 
     @PostMapping("/select-username-login")

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

@@ -27,7 +27,6 @@ import cn.newfeifan.mall.module.system.enums.logger.LoginResultEnum;
 import cn.newfeifan.mall.module.system.enums.oauth2.OAuth2ClientConstants;
 import cn.newfeifan.mall.module.system.enums.sms.SmsSceneEnum;
 import cn.newfeifan.mall.module.system.enums.social.SocialTypeEnum;
-import jodd.util.StringUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -115,32 +114,21 @@ public class MemberAuthServiceImpl implements MemberAuthService {
             throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
         }
 
-        // 情况一:已绑定,直接读取用户信息
-        MemberUserDO user;
-        if (socialUser.getUserId() != null) {
-            user = userService.getUser(socialUser.getUserId());
-            // 情况二:未绑定,注册用户 + 绑定用户
-        } else {
-
-            //通过微信 + 手机号首次登录
-            if (!StringUtil.isEmpty(phone)) {
-                //如果会员表中有记录
-                user = userService.getUserByMobile(phone);
-                if (user == null) {
-                    //加入手机号
-                    user = userService.createUser(phone, socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal(), reqVO.getLinkId(),null);
-                }
-            } else {
-                user = userService.createUser(null, socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal(), reqVO.getLinkId(),null);
-            }
-
-            socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
-                    reqVO.getType(), reqVO.getCode(), reqVO.getState()));
+        //重名校验
+        if (userService.selectCountByUsername(reqVO.getUsername()) > 0) {
+            throw exception(AUTH_USERNAME_EXISTS);
         }
-        if (user == null) {
-            throw exception(USER_NOT_EXISTS);
+
+        //微信能绑定的账号上限校验
+        if(socialUserApi.wxBindIsMax(socialUser.getId())){
+            throw exception(AUTH_SOCIAL_USER_BIND_MAX);
         }
 
+        MemberUserDO user = userService.createUser(phone, socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal(), reqVO.getLinkId(), reqVO.getUsername());
+
+        socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
+                reqVO.getType(), reqVO.getCode(), reqVO.getState()));
+
         // 创建 Token 令牌,记录登录日志
         return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, socialUser.getOpenid());
     }
@@ -155,16 +143,16 @@ public class MemberAuthServiceImpl implements MemberAuthService {
         }
 
         //如果同一个微信绑定了多个用户就返回用户集 供用户选择登录
-        if(socialUser.getUserIds().size() != 1){
+        if (socialUser.getUserIds().size() != 1) {
             List<MemberUserDO> users = userService.getUserList(socialUser.getUserIds());
             List<AppAuthMemberUserRespVO> memberUserRespVOS = users.stream().map(user -> AppAuthMemberUserRespVO
-                    .builder()
-                    .username(user.getUsername())
-                    .avatar(user.getAvatar())
-                    .build())
+                            .builder()
+                            .username(user.getUsername())
+                            .avatar(user.getAvatar())
+                            .build())
                     .collect(Collectors.toList());
             return AppAuthLoginRespVO.builder().openid(socialUser.getOpenid()).socialUsers(memberUserRespVOS).build();
-        }else{
+        } else {
             //如果只有一个账号那就直接登录
             socialUser.setUserId(socialUser.getUserIds().get(0));
         }
@@ -184,7 +172,7 @@ public class MemberAuthServiceImpl implements MemberAuthService {
 
         //如果微信绑定的账号中没有此账号那就返回报错
         Boolean flag = socialUserApi.wxIsBindByUserId(reqVO.getOpenId(), user.getId());
-        if(!flag){
+        if (!flag) {
             throw exception(AUTH_SOCIAL_USER_BIND_USER_NOT_FOUND);
         }
 
@@ -199,7 +187,7 @@ public class MemberAuthServiceImpl implements MemberAuthService {
         Assert.notNull(phoneNumberInfo, "获得手机信息失败,结果为空");
 
         // 获得获得注册用户
-        MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(),null,
+        MemberUserDO user = userService.createUserIfAbsent(phoneNumberInfo.getPurePhoneNumber(), null,
                 getClientIP(), TerminalEnum.WECHAT_MINI_PROGRAM.getTerminal());
         Assert.notNull(user, "获取用户失败,结果为空");
 
@@ -336,6 +324,7 @@ public class MemberAuthServiceImpl implements MemberAuthService {
 
     /**
      * 校验手机号是否达到上限
+     *
      * @param mobile 手机号
      */
     private void verifyMobileIsMaximize(String mobile) {
@@ -347,9 +336,10 @@ public class MemberAuthServiceImpl implements MemberAuthService {
 
     /**
      * 校验用户名
+     *
      * @param username 用户名
      */
-    private void verifyUsername(String username){
+    private void verifyUsername(String username) {
         if (userService.selectCountByUsername(username) > 0) {
             throw exception(AUTH_USERNAME_EXISTS);
         }

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

@@ -60,4 +60,11 @@ public interface SocialUserApi {
      */
     Boolean wxIsBindByUserId(String openId,Long userId);
 
+    /**
+     * 判断微信是否绑定到上限
+     * @param socialUserId 社交用户编号
+     * @return 是否
+     */
+    Boolean wxBindIsMax(Long socialUserId);
+
 }

+ 5 - 0
feifan-module-system/feifan-module-system-api/src/main/java/cn/newfeifan/mall/module/system/api/social/dto/SocialUserRespDTO.java

@@ -16,6 +16,11 @@ import java.util.List;
 @AllArgsConstructor
 public class SocialUserRespDTO {
 
+    /**
+     * 社交平台的编号
+     */
+    private Long id;
+
     /**
      * 社交用户的 openid
      */

+ 1 - 2
feifan-module-system/feifan-module-system-api/src/main/java/cn/newfeifan/mall/module/system/enums/ErrorCodeConstants.java

@@ -4,7 +4,6 @@ import cn.newfeifan.mall.framework.common.exception.ErrorCode;
 
 /**
  * System 错误码枚举类
- *
  * system 系统,使用 1-002-000-000 段
  */
 public interface ErrorCodeConstants {
@@ -12,7 +11,7 @@ public interface ErrorCodeConstants {
     // ========== AUTH 模块 1-002-000-000 ==========
     ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_002_000_000, "登录失败,账号密码不正确");
     ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_002_000_001, "登录失败,账号被禁用");
-    ErrorCode AUTH_LOGIN_USER_IS_FIRST = new ErrorCode(1_002_000_002, "第一次登录请输入手机号校验");
+    ErrorCode AUTH_LOGIN_USER_IS_FIRST = new ErrorCode(1_002_000_002, "注册请输入用户名 + 手机号校验");
     ErrorCode AUTH_LOGIN_USER_ERROR_CODE = new ErrorCode(1_002_000_002, "code校验失败");
     ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_004, "验证码不正确,原因:{}");
     ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定");

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

@@ -47,4 +47,9 @@ public class SocialUserApiImpl implements SocialUserApi {
         return socialUserService.wxIsBindByUserId(openId,userId);
     }
 
+    @Override
+    public Boolean wxBindIsMax(Long socialUserId) {
+        return socialUserService.wxBindIsMax(socialUserId);
+    }
+
 }

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

@@ -103,9 +103,16 @@ public interface SocialUserService {
 
     /**
      * 判断微信是否绑定此账号
-     * @param openId
-     * @param userId
-     * @return
+     * @param openId openId
+     * @param userId 用户编号
+     * @return 是否
      */
     Boolean wxIsBindByUserId(String openId, Long userId);
+
+    /**
+     * 判断微信是否绑定到上限
+     * @param socialUserId 社交用户编号
+     * @return 是否
+     */
+    Boolean wxBindIsMax(Long socialUserId);
 }

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

@@ -5,6 +5,7 @@ import cn.hutool.core.lang.Assert;
 import cn.newfeifan.mall.framework.common.exception.ServiceException;
 import cn.newfeifan.mall.framework.common.pojo.PageResult;
 import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
+import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
 import cn.newfeifan.mall.module.system.api.social.dto.SocialUserBindReqDTO;
 import cn.newfeifan.mall.module.system.api.social.dto.SocialUserRespDTO;
 import cn.newfeifan.mall.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO;
@@ -74,7 +75,7 @@ public class SocialUserServiceImpl implements SocialUserService {
         Assert.notNull(socialUser, "社交用户不能为空");
 
         // 社交用户可能之前绑定过别的用户,需要进行解绑
-        socialUserBindMapper.deleteByUserTypeAndSocialUserId(reqDTO.getUserType(), socialUser.getId());
+//        socialUserBindMapper.deleteByUserTypeAndSocialUserId(reqDTO.getUserType(), socialUser.getId());
 
         // 用户可能之前已经绑定过该社交类型,需要进行解绑
         socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(reqDTO.getUserType(), reqDTO.getUserId(),
@@ -110,7 +111,7 @@ public class SocialUserServiceImpl implements SocialUserService {
         // 获得社交用户
         SocialUserDO socialUser = socialUserMapper.selectById(socialUserBind.getSocialUserId());
         Assert.notNull(socialUser, "社交用户不能为空");
-        return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
+        return new SocialUserRespDTO(socialUser.getId(),socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
                 socialUserBind.getUserId(),null);
     }
 
@@ -123,7 +124,7 @@ public class SocialUserServiceImpl implements SocialUserService {
         // 获得绑定用户
         SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType,
                 socialUser.getId());
-        return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
+        return new SocialUserRespDTO(socialUser.getId(),socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
                 socialUserBind != null ? socialUserBind.getUserId() : null,null);
     }
 
@@ -137,7 +138,7 @@ public class SocialUserServiceImpl implements SocialUserService {
         List<SocialUserBindDO> socialUserBindDOS = socialUserBindMapper.selectsByUserTypeAndSocialUserId(userType,
                 socialUser.getId());
         List<Long> userIds = socialUserBindDOS.stream().map(SocialUserBindDO::getUserId).collect(Collectors.toList());
-        return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
+        return new SocialUserRespDTO(socialUser.getId(),socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
                  null,userIds);
     }
 
@@ -175,9 +176,9 @@ public class SocialUserServiceImpl implements SocialUserService {
         }
         Assert.notNull(authUser, "三方用户不能为空");
 
-        // 保存到 DB 中
+        // =====保存到 DB 中=====
         socialUser = socialUserMapper.selectByTypeAndOpenid(socialType, authUser.getUuid());
-        if (isFirst && socialUser == null) {
+        if ((isFirst && socialUser == null) || (!isFirst && !isRegister)) {
             //首次登录就将code缓存到redis中
             stringRedisTemplate.opsForValue().set("auth_user:" + code, JsonUtils.toJsonString(authUser) , 60 * 10 , TimeUnit.SECONDS);
             throw exception(AUTH_LOGIN_USER_IS_FIRST);
@@ -210,7 +211,8 @@ public class SocialUserServiceImpl implements SocialUserService {
         }
         String json = stringRedisTemplate.opsForValue().get("auth_user:" + code);
         if (json != null) {
-            // 假设存在一个方法从JSON字符串中安全地解析AuthUser对象
+            stringRedisTemplate.delete("auth_user:" + code);
+            // 从JSON字符串中安全地解析AuthUser对象
             return JSONObject.parseObject(json,AuthUser.class);
         }
 
@@ -241,4 +243,9 @@ public class SocialUserServiceImpl implements SocialUserService {
         return socialUserBindMapper.selectOne(SocialUserBindDO::getSocialUserId,socialUserDO.getId(),SocialUserBindDO::getUserId,userId) != null;
     }
 
+    @Override
+    public Boolean wxBindIsMax(Long socialUserId) {
+        return socialUserBindMapper.selectCount(new LambdaQueryWrapperX<SocialUserBindDO>().eq(SocialUserBindDO::getSocialUserId,socialUserId)) >= 10;
+    }
+
 }