fix: 微信小程序登录,自测bug修复。
This commit is contained in:
@@ -121,25 +121,45 @@ public class WxLoginServiceImpl implements WxLoginService {
|
|||||||
log.info("开始处理用户登录: mobilePhone={}, openId={}", mobilePhone, sessionInfo.getOpenid());
|
log.info("开始处理用户登录: mobilePhone={}, openId={}", mobilePhone, sessionInfo.getOpenid());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 查询用户是否存在
|
// 1. 优先按 unionId 查询用户(同一个微信账号可能换过手机号)
|
||||||
AquUser user = aquUserMapper.selectOne(
|
AquUser user = null;
|
||||||
|
String unionId = sessionInfo.getUnionid();
|
||||||
|
String openId = sessionInfo.getOpenid();
|
||||||
|
|
||||||
|
if (unionId != null && !unionId.isEmpty()) {
|
||||||
|
user = aquUserMapper.selectOne(
|
||||||
|
new LambdaQueryWrapper<AquUser>()
|
||||||
|
.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<AquUser>()
|
new LambdaQueryWrapper<AquUser>()
|
||||||
.eq(AquUser::getMobilePhone, mobilePhone)
|
.eq(AquUser::getMobilePhone, mobilePhone)
|
||||||
);
|
);
|
||||||
|
if (user != null) {
|
||||||
|
log.info("通过手机号找到已存在用户: userId={}, mobilePhone={}", user.getId(), mobilePhone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<Long> listParentUserId = new ArrayList<>();
|
List<Long> listParentUserId = new ArrayList<>();
|
||||||
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
// 2. 用户不存在,创建新用户
|
// 3. 用户不存在,创建新用户
|
||||||
log.info("用户不存在,开始创建新用户: mobilePhone={}", mobilePhone);
|
log.info("用户不存在,开始创建新用户: mobilePhone={}", mobilePhone);
|
||||||
user = createNewUser(mobilePhone, sessionInfo);
|
user = createNewUser(mobilePhone, sessionInfo);
|
||||||
} else {
|
} else {
|
||||||
log.info("用户已存在: userId={}, mobilePhone={}", user.getId(), mobilePhone);
|
log.info("用户已存在: userId={}, mobilePhone={}", user.getId(), mobilePhone);
|
||||||
|
|
||||||
// 3. 用户已存在,更新微信信息
|
// 4. 用户已存在,更新微信信息(含手机号变更)
|
||||||
updateWxInfo(user, sessionInfo);
|
updateWxInfo(user, sessionInfo, mobilePhone);
|
||||||
|
|
||||||
// 4. 查询父账号关系
|
// 5. 查询父账号关系
|
||||||
try {
|
try {
|
||||||
List<UserRelation> relations = userRelationMapper.selectList(
|
List<UserRelation> relations = userRelationMapper.selectList(
|
||||||
new LambdaQueryWrapper<UserRelation>()
|
new LambdaQueryWrapper<UserRelation>()
|
||||||
@@ -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 {
|
try {
|
||||||
boolean needUpdate = false;
|
boolean needUpdate = false;
|
||||||
|
|
||||||
@@ -234,6 +254,20 @@ public class WxLoginServiceImpl implements WxLoginService {
|
|||||||
needUpdate = true;
|
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) {
|
if (needUpdate) {
|
||||||
user.setUpdateTime(new Date());
|
user.setUpdateTime(new Date());
|
||||||
int rows = aquUserMapper.updateById(user);
|
int rows = aquUserMapper.updateById(user);
|
||||||
|
|||||||
Reference in New Issue
Block a user