Commit 59752d43b7dcd2f1cc3e767187640f31c2753e3c
1 parent
d65c28d7
✨ Member:修复社交登录时,自动创建 user 后,未进行绑定的问题
Showing
3 changed files
with
18 additions
and
10 deletions
yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java
| ... | ... | @@ -18,7 +18,7 @@ public interface ErrorCodeConstants { |
| 18 | 18 | // ========== AUTH 模块 1-004-003-000 ========== |
| 19 | 19 | ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确"); |
| 20 | 20 | ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用"); |
| 21 | - ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_004_003_005, "未绑定账号,需要进行绑定"); | |
| 21 | + ErrorCode AUTH_SOCIAL_USER_NOT_FOUND = new ErrorCode(1_004_003_005, "登录失败,解析不到三方登录信息"); | |
| 22 | 22 | ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用"); |
| 23 | 23 | |
| 24 | 24 | // ========== 用户收件地址 1-004-004-000 ========== | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java
| ... | ... | @@ -100,21 +100,27 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | @Override |
| 103 | + @Transactional | |
| 103 | 104 | public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) { |
| 104 | 105 | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| 105 | 106 | SocialUserRespDTO socialUser = socialUserApi.getSocialUser(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), |
| 106 | 107 | reqVO.getCode(), reqVO.getState()); |
| 107 | 108 | if (socialUser == null) { |
| 108 | - throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | |
| 109 | + throw exception(AUTH_SOCIAL_USER_NOT_FOUND); | |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | - // 情况一:已绑定,自动登录 | |
| 112 | - MemberUserDO user = userService.getUser(socialUser.getUserId()); | |
| 112 | + // 情况一:已绑定,直接读取用户信息 | |
| 113 | + MemberUserDO user; | |
| 114 | + if (socialUser.getUserId() != null) { | |
| 115 | + user = userService.getUser(socialUser.getUserId()); | |
| 116 | + // 情况二:未绑定,注册用户 + 绑定用户 | |
| 117 | + } else { | |
| 118 | + user = userService.createUser(socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal()); | |
| 119 | + socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(), | |
| 120 | + reqVO.getType(), reqVO.getCode(), reqVO.getState())); | |
| 121 | + } | |
| 113 | 122 | if (user == null) { |
| 114 | 123 | throw exception(USER_NOT_EXISTS); |
| 115 | - // 情况二:未绑定,注册登录 | |
| 116 | - } else { | |
| 117 | - user = userService.createUser(user.getNickname(), user.getAvatar(), getClientIP(), getTerminal()); | |
| 118 | 124 | } |
| 119 | 125 | |
| 120 | 126 | // 创建 Token 令牌,记录登录日志 | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java
| ... | ... | @@ -2,9 +2,7 @@ package cn.iocoder.yudao.module.member.service.user; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.collection.ListUtil; |
| 5 | -import cn.hutool.core.util.IdUtil; | |
| 6 | -import cn.hutool.core.util.ObjectUtil; | |
| 7 | -import cn.hutool.core.util.StrUtil; | |
| 5 | +import cn.hutool.core.util.*; | |
| 8 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 9 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 10 | 8 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| ... | ... | @@ -101,6 +99,10 @@ public class MemberUserServiceImpl implements MemberUserService { |
| 101 | 99 | user.setPassword(encodePassword(password)); // 加密密码 |
| 102 | 100 | user.setRegisterIp(registerIp).setRegisterTerminal(terminal); |
| 103 | 101 | user.setNickname(nickname).setAvatar(avtar); // 基础信息 |
| 102 | + if (StrUtil.isEmpty(nickname)) { | |
| 103 | + // 昵称为空时,随机一个名字,避免一些依赖 nickname 的逻辑报错,或者有点丑。例如说,短信发送有昵称时~ | |
| 104 | + user.setNickname("用户" + RandomUtil.randomNumbers(6)); | |
| 105 | + } | |
| 104 | 106 | memberUserMapper.insert(user); |
| 105 | 107 | |
| 106 | 108 | // 发送 MQ 消息:用户创建 | ... | ... |