fix 修复 三方账号可以被同一个用户多次绑定问题

This commit is contained in:
疯狂的狮子Li 2024-05-11 19:52:33 +08:00
parent a36a07ae6f
commit ea5d657e31
4 changed files with 23 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.lock.annotation.Lock4j;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUser;
@ -64,20 +65,24 @@ public class SysLoginService {
* 绑定第三方用户 * 绑定第三方用户
* *
* @param authUserData 授权响应实体 * @param authUserData 授权响应实体
* @return 统一响应实体
*/ */
@Lock4j
public void socialRegister(AuthUser authUserData) { public void socialRegister(AuthUser authUserData) {
String authId = authUserData.getSource() + authUserData.getUuid(); String authId = authUserData.getSource() + authUserData.getUuid();
// 第三方用户信息 // 第三方用户信息
SysSocialBo bo = BeanUtil.toBean(authUserData, SysSocialBo.class); SysSocialBo bo = BeanUtil.toBean(authUserData, SysSocialBo.class);
BeanUtil.copyProperties(authUserData.getToken(), bo); BeanUtil.copyProperties(authUserData.getToken(), bo);
bo.setUserId(LoginHelper.getUserId()); Long userId = LoginHelper.getUserId();
bo.setUserId(userId);
bo.setAuthId(authId); bo.setAuthId(authId);
bo.setOpenId(authUserData.getUuid()); bo.setOpenId(authUserData.getUuid());
bo.setUserName(authUserData.getUsername()); bo.setUserName(authUserData.getUsername());
bo.setNickName(authUserData.getNickname()); bo.setNickName(authUserData.getNickname());
// 查询是否已经绑定用户 // 查询是否已经绑定用户
List<SysSocialVo> list = sysSocialService.selectByAuthId(authId); SysSocialBo params = new SysSocialBo();
params.setUserId(userId);
params.setSource(bo.getSource());
List<SysSocialVo> list = sysSocialService.queryList(params);
if (CollUtil.isEmpty(list)) { if (CollUtil.isEmpty(list)) {
// 没有绑定用户, 新增用户信息 // 没有绑定用户, 新增用户信息
sysSocialService.insertByBo(bo); sysSocialService.insertByBo(bo);
@ -85,6 +90,8 @@ public class SysLoginService {
// 更新用户信息 // 更新用户信息
bo.setId(list.get(0).getId()); bo.setId(list.get(0).getId());
sysSocialService.updateByBo(bo); sysSocialService.updateByBo(bo);
// 如果要绑定的平台账号已经被绑定过了 是否抛异常自行决断
// throw new ServiceException("此平台账号已经被绑定!");
} }
} }

View File

@ -29,9 +29,9 @@ public class SysSocialBo extends TenantEntity {
private Long id; private Long id;
/** /**
* 唯一ID * 认证唯一ID
*/ */
@NotBlank(message = "唯一ID不能为空", groups = { AddGroup.class, EditGroup.class }) @NotBlank(message = "认证唯一ID不能为空", groups = { AddGroup.class, EditGroup.class })
private String authId; private String authId;
/** /**
@ -64,7 +64,7 @@ public class SysSocialBo extends TenantEntity {
/** /**
* 用户的 ID * 用户的 ID
*/ */
@NotBlank(message = "用户的 ID不能为空", groups = { AddGroup.class, EditGroup.class }) @NotBlank(message = "用户的ID不能为空", groups = { AddGroup.class, EditGroup.class })
private Long userId; private Long userId;
/** /**

View File

@ -21,7 +21,7 @@ public interface ISysSocialService {
/** /**
* 查询社会化关系列表 * 查询社会化关系列表
*/ */
List<SysSocialVo> queryList(); List<SysSocialVo> queryList(SysSocialBo bo);
/** /**
* 查询社会化关系列表 * 查询社会化关系列表
@ -45,9 +45,7 @@ public interface ISysSocialService {
/** /**
* 根据 authId 查询 SysSocial 表和 SysUser 返回 SysSocialAuthResult 映射的对象 * 根据 authId 查询
* @param authId 认证ID
* @return SysSocial
*/ */
List<SysSocialVo> selectByAuthId(String authId); List<SysSocialVo> selectByAuthId(String authId);

View File

@ -1,8 +1,10 @@
package org.dromara.system.service.impl; package org.dromara.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.SysSocial; import org.dromara.system.domain.SysSocial;
import org.dromara.system.domain.bo.SysSocialBo; import org.dromara.system.domain.bo.SysSocialBo;
import org.dromara.system.domain.vo.SysSocialVo; import org.dromara.system.domain.vo.SysSocialVo;
@ -37,8 +39,12 @@ public class SysSocialServiceImpl implements ISysSocialService {
* 授权列表 * 授权列表
*/ */
@Override @Override
public List<SysSocialVo> queryList() { public List<SysSocialVo> queryList(SysSocialBo bo) {
return baseMapper.selectVoList(); LambdaQueryWrapper<SysSocial> lqw = new LambdaQueryWrapper<SysSocial>()
.eq(ObjectUtil.isNotNull(bo.getUserId()), SysSocial::getUserId, bo.getUserId())
.eq(StringUtils.isNotBlank(bo.getAuthId()), SysSocial::getAuthId, bo.getAuthId())
.eq(StringUtils.isNotBlank(bo.getSource()), SysSocial::getSource, bo.getSource());
return baseMapper.selectVoList(lqw);
} }
@Override @Override