重构社交登录,完善单元测试

This commit is contained in:
YunaiV
2022-04-26 02:17:38 +08:00
parent 89df5b3cf6
commit 878445a238
8 changed files with 110 additions and 187 deletions

View File

@@ -22,6 +22,8 @@ public class SocialUserBindDO extends BaseDO {
/**
* 关联的用户编号
*
* 关联 UserDO 的编号
*/
private Long userId;
/**
@@ -32,14 +34,16 @@ public class SocialUserBindDO extends BaseDO {
private Integer userType;
/**
* 社交平台
* 社交平台的用户编号
*
* 枚举 {@link SocialTypeEnum#getPlatform()}
* 关联 {@link SocialUserDO#getId()}
*/
private Integer platform;
private Long socialUserId;
/**
* 社交的全局编号
* 社交平台的类型
*
* 冗余 {@link SocialUserDO#getType()}
*/
private String unionId;
private Integer socialType;
}

View File

@@ -1,7 +1,5 @@
package cn.iocoder.yudao.module.system.dal.dataobject.social;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.baomidou.mybatisplus.annotation.TableId;
@@ -10,7 +8,6 @@ import lombok.*;
/**
* 社交用户
* 通过 {@link SocialUserDO#getUserId()} 关联到对应的 {@link AdminUserDO}
*
* @author weir
*/

View File

@@ -10,26 +10,23 @@ import java.util.List;
@Mapper
public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
default void deleteByUserTypeAndUserIdAndUnionId(Integer userType, Long userId, Integer platform) {
default void deleteByUserTypeAndUserIdAndSocialType(Integer userType, Long userId, Integer socialType) {
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getUserId, userId)
.eq(SocialUserBindDO::getPlatform, platform));
.eq(SocialUserBindDO::getSocialType, socialType));
}
default void deleteByUserTypeAndPlatformAndUnionId(Integer userType, Integer platform, String unionId) {
default void deleteByUserTypeAndSocialUserId(Integer userType, Long socialUserId) {
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getPlatform, platform)
.eq(SocialUserBindDO::getUnionId, unionId));
.eq(SocialUserBindDO::getSocialUserId, socialUserId));
}
default SocialUserBindDO selectByUserTypeAndPlatformAndUnionId(Integer userType,
Integer platform, String unionId) {
default SocialUserBindDO selectByUserTypeAndSocialUserId(Integer userType, Long socialUserId) {
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getPlatform, platform)
.eq(SocialUserBindDO::getUnionId, unionId));
.eq(SocialUserBindDO::getSocialUserId, socialUserId));
}
default List<SocialUserBindDO> selectListByUserIdAndUserType(Long userId, Integer userType) {

View File

@@ -22,13 +22,7 @@ public interface SocialUserMapper extends BaseMapperX<SocialUserDO> {
default SocialUserDO selectByTypeAndOpenid(Integer type, String openid) {
return selectOne(new LambdaQueryWrapper<SocialUserDO>()
.eq(SocialUserDO::getType, type)
.eq(SocialUserDO::getCode, openid));
}
default List<SocialUserDO> selectListByUnionIdAndType(Collection<String> unionIds, Collection<Integer> types) {
return selectList(new LambdaQueryWrapper<SocialUserDO>()
.in(SocialUserDO::getUnionId, unionIds)
.in(SocialUserDO::getType, types));
.eq(SocialUserDO::getOpenid, openid));
}
}

View File

@@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.social;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
@@ -25,9 +23,9 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
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.*;
@@ -78,7 +76,6 @@ public class SocialUserServiceImpl implements SocialUserService {
}
socialUser.setType(type).setCode(code).setState(state) // 需要保存 code + state 字段,保证后续可查询
.setOpenid(authUser.getUuid()).setToken(authUser.getToken().getAccessToken()).setRawTokenInfo((toJsonString(authUser.getToken())))
.setUnionId(StrUtil.blankToDefault(authUser.getToken().getUnionId(), authUser.getUuid())) // unionId 识别多个用户
.setNickname(authUser.getNickname()).setAvatar(authUser.getAvatar()).setRawUserInfo(toJsonString(authUser.getRawUserInfo()));
if (socialUser.getId() == null) {
socialUserMapper.insert(socialUser);
@@ -96,9 +93,7 @@ public class SocialUserServiceImpl implements SocialUserService {
return Collections.emptyList();
}
// 获得社交用户
Set<Integer> platforms = CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getPlatform);
return socialUserMapper.selectListByUnionIdAndType(CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getUnionId),
SocialTypeEnum.getTypes(platforms));
return socialUserMapper.selectBatchIds(convertSet(socialUserBinds, SocialUserBindDO::getSocialUserId));
}
@Override
@@ -108,19 +103,17 @@ public class SocialUserServiceImpl implements SocialUserService {
SocialUserDO socialUser = authSocialUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState());
Assert.notNull(socialUser, "社交用户不能为空");
// 如果 unionId 之前绑定过,需要进行解绑
Integer platform = SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform();
socialUserBindMapper.deleteByUserTypeAndPlatformAndUnionId(reqDTO.getUserType(), platform,
socialUser.getUnionId());
// 社交用户可能之前绑定过别的用户,需要进行解绑
socialUserBindMapper.deleteByUserTypeAndSocialUserId(reqDTO.getUserType(), socialUser.getId());
// 如果 userId 之前绑定过该 type 的其它账号,需要进行解绑
socialUserBindMapper.deleteByUserTypeAndUserIdAndUnionId(reqDTO.getUserType(), reqDTO.getUserId(),
socialUser.getUnionId());
// 用户可能之前已经绑定过该社交类型,需要进行解绑
socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(reqDTO.getUserType(), reqDTO.getUserId(),
socialUser.getType());
// 绑定当前登录的社交用户
SocialUserBindDO socialUserBind = SocialUserBindDO.builder()
.userId(reqDTO.getUserId()).userType(reqDTO.getUserType())
.platform(platform).unionId(socialUser.getUnionId()).build();
.socialUserId(socialUser.getId()).socialType(socialUser.getType()).build();
socialUserBindMapper.insert(socialUserBind);
}
@@ -133,8 +126,7 @@ public class SocialUserServiceImpl implements SocialUserService {
}
// 获得对应的社交绑定关系
socialUserBindMapper.deleteByUserTypeAndUserIdAndPlatformAndUnionId(userType, userId,
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(userType, userId, socialUser.getType());
}
@Override
@@ -144,8 +136,8 @@ public class SocialUserServiceImpl implements SocialUserService {
Assert.notNull(socialUser, "社交用户不能为空");
// 如果未绑定的社交用户,则无法自动登录,进行报错
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndPlatformAndUnionId(userType,
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType,
socialUser.getId());
if (socialUserBind == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}