From 3490aaa7a559cb9ace128a2bb88e756f39c408c0 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Sat, 4 Apr 2026 00:07:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E7=99=BB=E5=BD=95=EF=BC=8C=E8=87=AA=E6=B5=8Bbug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/WxLoginServiceImpl.java | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/intc-modules/intc-weixin/src/main/java/com/intc/weixin/service/impl/WxLoginServiceImpl.java b/intc-modules/intc-weixin/src/main/java/com/intc/weixin/service/impl/WxLoginServiceImpl.java index 1274fbb..1064aa2 100644 --- a/intc-modules/intc-weixin/src/main/java/com/intc/weixin/service/impl/WxLoginServiceImpl.java +++ b/intc-modules/intc-weixin/src/main/java/com/intc/weixin/service/impl/WxLoginServiceImpl.java @@ -121,25 +121,45 @@ public class WxLoginServiceImpl implements WxLoginService { log.info("开始处理用户登录: mobilePhone={}, openId={}", mobilePhone, sessionInfo.getOpenid()); try { - // 1. 查询用户是否存在 - AquUser user = aquUserMapper.selectOne( - new LambdaQueryWrapper() - .eq(AquUser::getMobilePhone, mobilePhone) - ); + // 1. 优先按 unionId 查询用户(同一个微信账号可能换过手机号) + AquUser user = null; + String unionId = sessionInfo.getUnionid(); + String openId = sessionInfo.getOpenid(); + + if (unionId != null && !unionId.isEmpty()) { + user = aquUserMapper.selectOne( + new LambdaQueryWrapper() + .eq(AquUser::getWxUnionId, unionId) + ); + if (user != null) { + log.info("通过unionId找到已存在用户: userId={}, mobilePhone={}", user.getId(), user.getMobilePhone()); + } + } + + // 2. 若 unionId 未找到,再按手机号查询 + if (user == null) { + user = aquUserMapper.selectOne( + new LambdaQueryWrapper() + .eq(AquUser::getMobilePhone, mobilePhone) + ); + if (user != null) { + log.info("通过手机号找到已存在用户: userId={}, mobilePhone={}", user.getId(), mobilePhone); + } + } List listParentUserId = new ArrayList<>(); if (user == null) { - // 2. 用户不存在,创建新用户 + // 3. 用户不存在,创建新用户 log.info("用户不存在,开始创建新用户: mobilePhone={}", mobilePhone); user = createNewUser(mobilePhone, sessionInfo); } else { log.info("用户已存在: userId={}, mobilePhone={}", user.getId(), mobilePhone); - // 3. 用户已存在,更新微信信息 - updateWxInfo(user, sessionInfo); + // 4. 用户已存在,更新微信信息(含手机号变更) + updateWxInfo(user, sessionInfo, mobilePhone); - // 4. 查询父账号关系 + // 5. 查询父账号关系 try { List relations = userRelationMapper.selectList( new LambdaQueryWrapper() @@ -213,9 +233,9 @@ public class WxLoginServiceImpl implements WxLoginService { } /** - * 更新微信信息 + * 更新微信信息(不覆盖用户在App内修改的手机号) */ - private void updateWxInfo(AquUser user, WxSessionInfoVo sessionInfo) { + private void updateWxInfo(AquUser user, WxSessionInfoVo sessionInfo, String newMobilePhone) { try { boolean needUpdate = false; @@ -234,6 +254,20 @@ public class WxLoginServiceImpl implements WxLoginService { needUpdate = true; } + // 手机号修改策略: + // 只有当数据库中手机号为空时,才用微信返回的号进行填充。 + // 如果数据库已有手机号,则不覆盖(尊重用户在App内主动修改的号码)。 + if (newMobilePhone != null && (user.getMobilePhone() == null || user.getMobilePhone().isEmpty())) { + log.info("用户手机号为空,填充微信手机号: userId={}, newPhone={}", + user.getId(), newMobilePhone); + user.setMobilePhone(newMobilePhone); + needUpdate = true; + } else if (newMobilePhone != null && !newMobilePhone.equals(user.getMobilePhone())) { + // 手机号不一致,仅记录日志,不自动覆盖 + log.info("微信返回手机号[{}]与数据库手机号[{}]不一致,保持数据库号码不变: userId={}", + newMobilePhone, user.getMobilePhone(), user.getId()); + } + if (needUpdate) { user.setUpdateTime(new Date()); int rows = aquUserMapper.updateById(user);