Member:修复社交登录时,自动创建 user 后,未进行绑定的问题

This commit is contained in:
YunaiV 2023-12-23 17:54:13 +08:00
parent d65c28d7f7
commit 59752d43b7
3 changed files with 18 additions and 10 deletions

View File

@ -18,7 +18,7 @@ public interface ErrorCodeConstants {
// ========== AUTH 模块 1-004-003-000 ========== // ========== AUTH 模块 1-004-003-000 ==========
ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确"); ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1_004_003_000, "登录失败,账号密码不正确");
ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用"); ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_004_003_001, "登录失败,账号被禁用");
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_004_003_005, "未绑定账号,需要进行绑定"); ErrorCode AUTH_SOCIAL_USER_NOT_FOUND = new ErrorCode(1_004_003_005, "登录失败,解析不到三方登录信息");
ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用"); ErrorCode AUTH_MOBILE_USED = new ErrorCode(1_004_003_007, "手机号已经被使用");
// ========== 用户收件地址 1-004-004-000 ========== // ========== 用户收件地址 1-004-004-000 ==========

View File

@ -100,21 +100,27 @@ public class MemberAuthServiceImpl implements MemberAuthService {
} }
@Override @Override
@Transactional
public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) { public AppAuthLoginRespVO socialLogin(AppAuthSocialLoginReqVO reqVO) {
// 使用 code 授权码进行登录然后获得到绑定的用户编号 // 使用 code 授权码进行登录然后获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserApi.getSocialUser(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), SocialUserRespDTO socialUser = socialUserApi.getSocialUser(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState()); reqVO.getCode(), reqVO.getState());
if (socialUser == null) { if (socialUser == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND); throw exception(AUTH_SOCIAL_USER_NOT_FOUND);
} }
// 情况一已绑定自动登录 // 情况一已绑定直接读取用户信息
MemberUserDO user = userService.getUser(socialUser.getUserId()); MemberUserDO user;
if (socialUser.getUserId() != null) {
user = userService.getUser(socialUser.getUserId());
// 情况二未绑定注册用户 + 绑定用户
} else {
user = userService.createUser(socialUser.getNickname(), socialUser.getAvatar(), getClientIP(), getTerminal());
socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
reqVO.getType(), reqVO.getCode(), reqVO.getState()));
}
if (user == null) { if (user == null) {
throw exception(USER_NOT_EXISTS); throw exception(USER_NOT_EXISTS);
// 情况二未绑定注册登录
} else {
user = userService.createUser(user.getNickname(), user.getAvatar(), getClientIP(), getTerminal());
} }
// 创建 Token 令牌记录登录日志 // 创建 Token 令牌记录登录日志

View File

@ -2,9 +2,7 @@ package cn.iocoder.yudao.module.member.service.user;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.*;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -101,6 +99,10 @@ public class MemberUserServiceImpl implements MemberUserService {
user.setPassword(encodePassword(password)); // 加密密码 user.setPassword(encodePassword(password)); // 加密密码
user.setRegisterIp(registerIp).setRegisterTerminal(terminal); user.setRegisterIp(registerIp).setRegisterTerminal(terminal);
user.setNickname(nickname).setAvatar(avtar); // 基础信息 user.setNickname(nickname).setAvatar(avtar); // 基础信息
if (StrUtil.isEmpty(nickname)) {
// 昵称为空时随机一个名字避免一些依赖 nickname 的逻辑报错或者有点丑例如说短信发送有昵称时~
user.setNickname("用户" + RandomUtil.randomNumbers(6));
}
memberUserMapper.insert(user); memberUserMapper.insert(user);
// 发送 MQ 消息用户创建 // 发送 MQ 消息用户创建