fix: 微信登录,注册账户修改。
This commit is contained in:
@@ -12,12 +12,10 @@ import com.intc.common.core.exception.user.UserException;
|
|||||||
import com.intc.common.core.utils.StringUtils;
|
import com.intc.common.core.utils.StringUtils;
|
||||||
import com.intc.common.core.utils.ValidatorUtils;
|
import com.intc.common.core.utils.ValidatorUtils;
|
||||||
import com.intc.common.json.utils.JsonUtils;
|
import com.intc.common.json.utils.JsonUtils;
|
||||||
|
import com.intc.common.core.utils.MapstructUtils;
|
||||||
import com.intc.common.satoken.utils.LoginHelper;
|
import com.intc.common.satoken.utils.LoginHelper;
|
||||||
import com.intc.common.tenant.helper.TenantHelper;
|
import com.intc.common.tenant.helper.TenantHelper;
|
||||||
import com.intc.fishery.domain.AquUser;
|
import com.intc.fishery.domain.AquUser;
|
||||||
import com.intc.fishery.domain.bo.AquUserBo;
|
|
||||||
import com.intc.fishery.mapper.AquUserMapper;
|
|
||||||
import com.intc.fishery.service.IAquUserService;
|
|
||||||
import com.intc.system.domain.SysUser;
|
import com.intc.system.domain.SysUser;
|
||||||
import com.intc.system.domain.bo.SysUserBo;
|
import com.intc.system.domain.bo.SysUserBo;
|
||||||
import com.intc.system.domain.vo.SysClientVo;
|
import com.intc.system.domain.vo.SysClientVo;
|
||||||
@@ -33,9 +31,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信小程序认证策略
|
* 微信小程序认证策略
|
||||||
*
|
*
|
||||||
@@ -50,8 +45,6 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
private final SysLoginService loginService;
|
private final SysLoginService loginService;
|
||||||
private final SysUserMapper userMapper;
|
private final SysUserMapper userMapper;
|
||||||
private final ISysUserService userService;
|
private final ISysUserService userService;
|
||||||
private final AquUserMapper aquUserMapper;
|
|
||||||
private final IAquUserService aquUserService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LoginVo login(String body, SysClientVo client) {
|
public LoginVo login(String body, SysClientVo client) {
|
||||||
@@ -85,12 +78,13 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
throw new ServiceException("获取手机号失败,请重新授权");
|
throw new ServiceException("获取手机号失败,请重新授权");
|
||||||
}
|
}
|
||||||
|
|
||||||
SysUserVo sysUser = loadUserByPhone(mobilePhone);
|
// 3. 根据手机号加载或创建系统用户(传入aquUser以保持ID一致)
|
||||||
|
SysUserVo sysUser = loadUserByPhone(mobilePhone, aquUser);
|
||||||
|
|
||||||
log.info("找到对应的系统用户: sysUserId={}, userName={}",
|
log.info("找到对应的系统用户: sysUserId={}, userName={}",
|
||||||
sysUser.getUserId(), sysUser.getUserName());
|
sysUser.getUserId(), sysUser.getUserName());
|
||||||
|
|
||||||
// 3. 使用系统用户构建 LoginUser(包含完整的权限信息)
|
// 4. 使用系统用户构建 LoginUser(包含完整的权限信息)
|
||||||
return loginService.buildLoginUser(sysUser);
|
return loginService.buildLoginUser(sysUser);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -125,9 +119,10 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
* 根据手机号加载系统用户,如果不存在则自动创建
|
* 根据手机号加载系统用户,如果不存在则自动创建
|
||||||
*
|
*
|
||||||
* @param phone 手机号
|
* @param phone 手机号
|
||||||
|
* @param aquUser 已存在的鱼测云用户(用于保持ID一致)
|
||||||
* @return 系统用户信息
|
* @return 系统用户信息
|
||||||
*/
|
*/
|
||||||
private SysUserVo loadUserByPhone(String phone) {
|
private SysUserVo loadUserByPhone(String phone, AquUser aquUser) {
|
||||||
SysUserVo user = userMapper.selectVoOne(
|
SysUserVo user = userMapper.selectVoOne(
|
||||||
new LambdaQueryWrapper<SysUser>()
|
new LambdaQueryWrapper<SysUser>()
|
||||||
.eq(SysUser::getPhonenumber, phone)
|
.eq(SysUser::getPhonenumber, phone)
|
||||||
@@ -136,7 +131,7 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
// 如果用户不存在,创建新用户
|
// 如果用户不存在,创建新用户
|
||||||
if (ObjectUtil.isNull(user)) {
|
if (ObjectUtil.isNull(user)) {
|
||||||
log.info("系统中不存在该手机号的用户,自动创建新用户: phone={}", phone);
|
log.info("系统中不存在该手机号的用户,自动创建新用户: phone={}", phone);
|
||||||
user = createNewUser(phone);
|
user = createNewUser(phone, aquUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查用户状态
|
// 检查用户状态
|
||||||
@@ -152,12 +147,15 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
* 创建新的系统用户
|
* 创建新的系统用户
|
||||||
*
|
*
|
||||||
* @param phone 手机号
|
* @param phone 手机号
|
||||||
|
* @param aquUser 已存在的鱼测云用户(用于保持ID一致)
|
||||||
* @return 新创建的用户信息
|
* @return 新创建的用户信息
|
||||||
*/
|
*/
|
||||||
private SysUserVo createNewUser(String phone) {
|
private SysUserVo createNewUser(String phone, AquUser aquUser) {
|
||||||
try {
|
try {
|
||||||
// 构建新用户对象
|
// 构建新用户对象
|
||||||
SysUserBo newUser = new SysUserBo();
|
SysUserBo newUser = new SysUserBo();
|
||||||
|
// 使用AquUser的ID作为系统用户ID,保持一致
|
||||||
|
newUser.setUserId(aquUser.getId());
|
||||||
// 使用手机号作为用户名
|
// 使用手机号作为用户名
|
||||||
newUser.setUserName(phone);
|
newUser.setUserName(phone);
|
||||||
// 使用手机号作为昵称
|
// 使用手机号作为昵称
|
||||||
@@ -178,11 +176,13 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
newUser.setCreateBy(0L);
|
newUser.setCreateBy(0L);
|
||||||
newUser.setUpdateBy(0L);
|
newUser.setUpdateBy(0L);
|
||||||
|
|
||||||
// 调用用户服务创建用户
|
// 直接插入用户(不使用registerUser,因为ID已经指定)
|
||||||
boolean success = userService.registerUser(newUser, LoginHelper.getTenantId());
|
SysUser sysUser = MapstructUtils.convert(newUser, SysUser.class);
|
||||||
|
sysUser.setTenantId(LoginHelper.getTenantId());
|
||||||
|
int rows = userMapper.insert(sysUser);
|
||||||
|
|
||||||
if (!success) {
|
if (rows <= 0) {
|
||||||
log.error("创建新用户失败: phone={}", phone);
|
log.error("创建新用户失败: phone={}, aquUserId={}", phone, aquUser.getId());
|
||||||
throw new ServiceException("创建用户失败,请联系管理员");
|
throw new ServiceException("创建用户失败,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,12 +197,9 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
throw new ServiceException("创建用户失败,请联系管理员");
|
throw new ServiceException("创建用户失败,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("成功创建系统用户: phone={}, userId={}, userName={}",
|
log.info("成功创建系统用户: phone={}, userId={}, userName={}, 与AquUser ID保持一致",
|
||||||
phone, createdUser.getUserId(), createdUser.getUserName());
|
phone, createdUser.getUserId(), createdUser.getUserName());
|
||||||
|
|
||||||
// 创建对应的鱼测云用户(AquUser)
|
|
||||||
createAquUser(createdUser);
|
|
||||||
|
|
||||||
return createdUser;
|
return createdUser;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -210,59 +207,4 @@ public class WechatAuthStrategy implements IAuthStrategy {
|
|||||||
throw new ServiceException("创建用户失败: " + e.getMessage());
|
throw new ServiceException("创建用户失败: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建鱼测云用户(AquUser)
|
|
||||||
*
|
|
||||||
* @param sysUser 系统用户信息
|
|
||||||
*/
|
|
||||||
private void createAquUser(SysUserVo sysUser) {
|
|
||||||
try {
|
|
||||||
// 检查是否已存在鱼测云用户
|
|
||||||
AquUser existingAquUser = aquUserMapper.selectById(sysUser.getUserId());
|
|
||||||
if (existingAquUser != null) {
|
|
||||||
log.info("鱼测云用户已存在,跳过创建: userId={}", sysUser.getUserId());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建报警电话列表(包含用户手机号)
|
|
||||||
List<String> warnPhoneList = new ArrayList<>();
|
|
||||||
if (StringUtils.isNotBlank(sysUser.getPhonenumber())) {
|
|
||||||
warnPhoneList.add(sysUser.getPhonenumber());
|
|
||||||
}
|
|
||||||
String warnPhoneJson = JsonUtils.toJsonString(warnPhoneList);
|
|
||||||
|
|
||||||
// 创建鱼测云用户对象
|
|
||||||
AquUserBo aquUserBo = new AquUserBo();
|
|
||||||
// 使用系统用户ID作为鱼测云用户ID,保持一致
|
|
||||||
aquUserBo.setId(sysUser.getUserId());
|
|
||||||
// 使用系统用户的昵称作为用户名
|
|
||||||
aquUserBo.setUserName(sysUser.getNickName());
|
|
||||||
// 设置手机号
|
|
||||||
aquUserBo.setMobilePhone(sysUser.getPhonenumber());
|
|
||||||
// 设置报警电话列表
|
|
||||||
aquUserBo.setWarnPhoneJson(warnPhoneJson);
|
|
||||||
// 非管理员
|
|
||||||
aquUserBo.setIsManager(0L);
|
|
||||||
// 没有大屏权限
|
|
||||||
aquUserBo.setHasScreen(0L);
|
|
||||||
// 备注
|
|
||||||
aquUserBo.setRemark("微信登录自动创建");
|
|
||||||
|
|
||||||
// 调用服务创建鱼测云用户
|
|
||||||
boolean success = aquUserService.insertByBo(aquUserBo);
|
|
||||||
|
|
||||||
if (success) {
|
|
||||||
log.info("成功创建鱼测云用户: userId={}, userName={}, phone={}",
|
|
||||||
sysUser.getUserId(), aquUserBo.getUserName(), aquUserBo.getMobilePhone());
|
|
||||||
} else {
|
|
||||||
log.error("创建鱼测云用户失败: userId={}", sysUser.getUserId());
|
|
||||||
// 不抛异常,不影响登录流程
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("创建鱼测云用户异常: userId={}, error={}", sysUser.getUserId(), e.getMessage(), e);
|
|
||||||
// 不抛异常,不影响登录流程
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user