Merge remote-tracking branch 'yudao/develop' into develop

This commit is contained in:
puhui999
2023-12-24 00:32:47 +08:00
37 changed files with 217 additions and 105 deletions

View File

@ -36,9 +36,9 @@ public class LoginLogCreateReqDTO {
private Integer userType;
/**
* 用户账号
*
* 不再强制校验 username 非空,因为 Member 社交登录时,此时暂时没有 username(mobile
*/
@NotBlank(message = "用户账号不能为空")
@Size(max = 30, message = "用户账号长度不能超过30个字符")
private String username;
/**

View File

@ -29,6 +29,16 @@ public interface SocialUserApi {
*/
void unbindSocialUser(@Valid SocialUserUnbindReqDTO reqDTO);
/**
* 获得社交用户,基于 userId
*
* @param userType 用户类型
* @param userId 用户编号
* @param socialType 社交平台的类型
* @return 社交用户
*/
SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType);
/**
* 获得社交用户
*
@ -40,7 +50,6 @@ public interface SocialUserApi {
* @param state state
* @return 社交用户
*/
SocialUserRespDTO getSocialUser(Integer userType, Integer socialType,
String code, String state);
SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state);
}

View File

@ -15,9 +15,17 @@ import lombok.NoArgsConstructor;
public class SocialUserRespDTO {
/**
* 社交用户 openid
* 社交用户 openid
*/
private String openid;
/**
* 社交用户的昵称
*/
private String nickname;
/**
* 社交用户的头像
*/
private String avatar;
/**
* 关联的用户编号

View File

@ -3,10 +3,12 @@ package cn.iocoder.yudao.module.system.api.social.dto;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.NoArgsConstructor;
/**
* 社交绑定 Request DTO使用 code 授权码
@ -14,6 +16,8 @@ import jakarta.validation.constraints.NotNull;
* @author 芋道源码
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SocialUserUnbindReqDTO {
/**

View File

@ -33,8 +33,13 @@ public class SocialUserApiImpl implements SocialUserApi {
}
@Override
public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) {
return socialUserService.getSocialUser(userType, socialType, code, state);
public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) {
return socialUserService.getSocialUserByUserId(userType, userId, socialType);
}
@Override
public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
return socialUserService.getSocialUserByCode(userType, socialType, code, state);
}
}

View File

@ -34,4 +34,11 @@ public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
.eq(SocialUserBindDO::getUserType, userType));
}
default SocialUserBindDO selectByUserIdAndUserTypeAndSocialType(Long userId, Integer userType, Integer socialType) {
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserId, userId)
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getSocialType, socialType));
}
}

View File

@ -156,9 +156,9 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Override
public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) {
// 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserService.getSocialUser(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState());
if (socialUser == null) {
if (socialUser == null || socialUser.getUserId() == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}

View File

@ -45,6 +45,16 @@ public interface SocialUserService {
*/
void unbindSocialUser(Long userId, Integer userType, Integer socialType, String openid);
/**
* 获得社交用户,基于 userId
*
* @param userType 用户类型
* @param userId 用户编号
* @param socialType 社交平台的类型
* @return 社交用户
*/
SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType);
/**
* 获得社交用户
*
@ -56,7 +66,7 @@ public interface SocialUserService {
* @param state state
* @return 社交用户
*/
SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state);
SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state);
// ==================== 社交用户 CRUD ====================

View File

@ -26,7 +26,6 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND;
/**
@ -94,18 +93,30 @@ public class SocialUserServiceImpl implements SocialUserService {
}
@Override
public SocialUserRespDTO getSocialUser(Integer userType, Integer socialType, String code, String state) {
public SocialUserRespDTO getSocialUserByUserId(Integer userType, Long userId, Integer socialType) {
// 获得绑定用户
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserIdAndUserTypeAndSocialType(userId, userType, socialType);
if (socialUserBind == null) {
return null;
}
// 获得社交用户
SocialUserDO socialUser = socialUserMapper.selectById(socialUserBind.getSocialUserId());
Assert.notNull(socialUser, "社交用户不能为空");
return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
socialUserBind.getUserId());
}
@Override
public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
// 获得社交用户
SocialUserDO socialUser = authSocialUser(socialType, userType, code, state);
Assert.notNull(socialUser, "社交用户不能为空");
// 如果未绑定的社交用户,则无法自动登录,进行报错
// 获得绑定用户
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType,
socialUser.getId());
if (socialUserBind == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}
return new SocialUserRespDTO(socialUser.getOpenid(), socialUserBind.getUserId());
return new SocialUserRespDTO(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getAvatar(),
socialUserBind != null ? socialUserBind.getUserId() : null);
}
/**

View File

@ -236,7 +236,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
AuthSocialLoginReqVO reqVO = randomPojo(AuthSocialLoginReqVO.class);
// mock 方法(绑定的用户编号)
Long userId = 1L;
when(socialUserService.getSocialUser(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()),
when(socialUserService.getSocialUserByCode(eq(UserTypeEnum.ADMIN.getValue()), eq(reqVO.getType()),
eq(reqVO.getCode()), eq(reqVO.getState()))).thenReturn(new SocialUserRespDTO(randomString(), userId));
// mock用户
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(userId));

View File

@ -147,7 +147,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest {
socialUserBindMapper.insert(socialUserBind);
// 调用
SocialUserRespDTO socialUser = socialUserService.getSocialUser(userType, type, code, state);
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(userType, type, code, state);
// 断言
assertEquals(userId, socialUser.getUserId());
assertEquals(socialUserDO.getOpenid(), socialUser.getOpenid());