From 22726ca92b495af95ec6460c2c4c3322b0b4ee24 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Tue, 20 Jan 2026 11:12:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E6=B3=A8=E5=86=8C=E8=B4=A6=E6=88=B7=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/service/impl/WechatAuthStrategy.java | 94 ++++--------------- 1 file changed, 18 insertions(+), 76 deletions(-) diff --git a/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java b/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java index 9c40bf7..dbf794f 100644 --- a/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java +++ b/intc-admin/src/main/java/com/intc/web/service/impl/WechatAuthStrategy.java @@ -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.ValidatorUtils; 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.tenant.helper.TenantHelper; 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.bo.SysUserBo; import com.intc.system.domain.vo.SysClientVo; @@ -33,9 +31,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; 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 SysUserMapper userMapper; private final ISysUserService userService; - private final AquUserMapper aquUserMapper; - private final IAquUserService aquUserService; @Override public LoginVo login(String body, SysClientVo client) { @@ -85,12 +78,13 @@ public class WechatAuthStrategy implements IAuthStrategy { throw new ServiceException("获取手机号失败,请重新授权"); } - SysUserVo sysUser = loadUserByPhone(mobilePhone); + // 3. 根据手机号加载或创建系统用户(传入aquUser以保持ID一致) + SysUserVo sysUser = loadUserByPhone(mobilePhone, aquUser); log.info("找到对应的系统用户: sysUserId={}, userName={}", sysUser.getUserId(), sysUser.getUserName()); - // 3. 使用系统用户构建 LoginUser(包含完整的权限信息) + // 4. 使用系统用户构建 LoginUser(包含完整的权限信息) return loginService.buildLoginUser(sysUser); }); @@ -125,9 +119,10 @@ public class WechatAuthStrategy implements IAuthStrategy { * 根据手机号加载系统用户,如果不存在则自动创建 * * @param phone 手机号 + * @param aquUser 已存在的鱼测云用户(用于保持ID一致) * @return 系统用户信息 */ - private SysUserVo loadUserByPhone(String phone) { + private SysUserVo loadUserByPhone(String phone, AquUser aquUser) { SysUserVo user = userMapper.selectVoOne( new LambdaQueryWrapper() .eq(SysUser::getPhonenumber, phone) @@ -136,7 +131,7 @@ public class WechatAuthStrategy implements IAuthStrategy { // 如果用户不存在,创建新用户 if (ObjectUtil.isNull(user)) { log.info("系统中不存在该手机号的用户,自动创建新用户: phone={}", phone); - user = createNewUser(phone); + user = createNewUser(phone, aquUser); } // 检查用户状态 @@ -152,12 +147,15 @@ public class WechatAuthStrategy implements IAuthStrategy { * 创建新的系统用户 * * @param phone 手机号 + * @param aquUser 已存在的鱼测云用户(用于保持ID一致) * @return 新创建的用户信息 */ - private SysUserVo createNewUser(String phone) { + private SysUserVo createNewUser(String phone, AquUser aquUser) { try { // 构建新用户对象 SysUserBo newUser = new SysUserBo(); + // 使用AquUser的ID作为系统用户ID,保持一致 + newUser.setUserId(aquUser.getId()); // 使用手机号作为用户名 newUser.setUserName(phone); // 使用手机号作为昵称 @@ -178,11 +176,13 @@ public class WechatAuthStrategy implements IAuthStrategy { newUser.setCreateBy(0L); newUser.setUpdateBy(0L); - // 调用用户服务创建用户 - boolean success = userService.registerUser(newUser, LoginHelper.getTenantId()); + // 直接插入用户(不使用registerUser,因为ID已经指定) + SysUser sysUser = MapstructUtils.convert(newUser, SysUser.class); + sysUser.setTenantId(LoginHelper.getTenantId()); + int rows = userMapper.insert(sysUser); - if (!success) { - log.error("创建新用户失败: phone={}", phone); + if (rows <= 0) { + log.error("创建新用户失败: phone={}, aquUserId={}", phone, aquUser.getId()); throw new ServiceException("创建用户失败,请联系管理员"); } @@ -197,12 +197,9 @@ public class WechatAuthStrategy implements IAuthStrategy { throw new ServiceException("创建用户失败,请联系管理员"); } - log.info("成功创建系统用户: phone={}, userId={}, userName={}", + log.info("成功创建系统用户: phone={}, userId={}, userName={}, 与AquUser ID保持一致", phone, createdUser.getUserId(), createdUser.getUserName()); - // 创建对应的鱼测云用户(AquUser) - createAquUser(createdUser); - return createdUser; } catch (Exception e) { @@ -210,59 +207,4 @@ public class WechatAuthStrategy implements IAuthStrategy { 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 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); - // 不抛异常,不影响登录流程 - } - } }