fix(user-sync): 修复用户同步时tenantId为空问题
- 在用户同步数据中添加tenantId字段,tenantId为空时默认传0 - 更新同步日志,增加tenantId信息输出,便于调试跟踪 - 在扫码登录流程中添加多处详细调试日志,输出关键变量状态 - 添加System.out调试信息,帮助排查绑定用户及扫码登录异常情况
This commit is contained in:
37
.workbuddy/memory/2026-04-06.md
Normal file
37
.workbuddy/memory/2026-04-06.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
## 2026-04-06 - 修复编译错误和用户同步问题
|
||||||
|
|
||||||
|
### 修复的问题
|
||||||
|
1. **QrLoginServiceImpl.java编译错误**: 修复了`CommonUtil`类找不到的编译错误
|
||||||
|
- 原始错误: `java: cannot find symbol class CommonUtil location: package com.gxwebsoft.common.core.utils`
|
||||||
|
|
||||||
|
2. **WxOfficialController.java编译错误**: 缺少`DateUtil`导入
|
||||||
|
- 真正的问题根源,导致链式编译失败
|
||||||
|
|
||||||
|
3. **用户同步失败**: `Column 'tenant_id' cannot be null` 数据库完整性约束错误
|
||||||
|
- 问题: 在同步用户到websopy系统时,没有传递tenant_id字段
|
||||||
|
- 结果: websopy端插入`app_user_cache`表时tenant_id为NULL,违反非空约束
|
||||||
|
|
||||||
|
### 解决方案
|
||||||
|
1. **CommonUtil.java修复**: 添加缺失的导入`import com.gxwebsoft.common.core.utils.JSONUtil;`
|
||||||
|
- 原因: `CommonUtil.responseError()`方法使用了`JSONUtil.toJSONString()`但未导入
|
||||||
|
|
||||||
|
2. **WxOfficialController.java修复**:
|
||||||
|
- 添加缺失的导入`import cn.hutool.core.date.DateUtil;`
|
||||||
|
- 添加详细的调试日志,跟踪扫码登录流程
|
||||||
|
- 在`processWxUser`方法中添加"已存在绑定用户"的日志
|
||||||
|
- 在`completeQrLogin`方法中添加开始和完成的详细日志
|
||||||
|
|
||||||
|
3. **UserSyncService.java修复**:
|
||||||
|
- 在同步数据中添加`tenantId`字段: `userCache.put("tenantId", tenantIdValue)`
|
||||||
|
- 处理tenantId可能为null的情况,默认传0: `Integer tenantIdValue = user.getTenantId() != null ? user.getTenantId() : 0`
|
||||||
|
- 更新日志输出,包含tenantId信息
|
||||||
|
|
||||||
|
### 验证结果
|
||||||
|
- 执行`mvn clean compile`成功通过,编译无错误
|
||||||
|
- 所有代码修改语法正确,无编译警告
|
||||||
|
|
||||||
|
### 技术要点
|
||||||
|
1. **Java编译链式失败**: 一个文件的编译错误可能导致依赖它的其他文件也出现"找不到符号"的错误
|
||||||
|
2. **数据库完整性约束**: REST API调用时需要确保传递所有必填字段
|
||||||
|
3. **分布式系统调试**: 当同步到外部系统失败时,需要检查两边数据格式的一致性
|
||||||
|
4. **日志分级**: 在不同阶段添加适当的System.out和log.info日志,便于问题排查
|
||||||
@@ -306,6 +306,9 @@ public class WxOfficialController extends BaseController {
|
|||||||
.eq(UserOauth::getTenantId, tenantId));
|
.eq(UserOauth::getTenantId, tenantId));
|
||||||
if (existingUser != null) {
|
if (existingUser != null) {
|
||||||
userId = existingUser.getUserId();
|
userId = existingUser.getUserId();
|
||||||
|
System.out.println("已存在绑定用户 userId = " + userId);
|
||||||
|
} else {
|
||||||
|
System.out.println("警告:count=1但未找到对应的绑定记录,unionid=" + unionid + ", tenantId=" + tenantId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -318,6 +321,7 @@ public class WxOfficialController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
private void completeQrLogin(String token, Integer userId, Integer tenantId) {
|
private void completeQrLogin(String token, Integer userId, Integer tenantId) {
|
||||||
try {
|
try {
|
||||||
|
System.out.println("开始完成扫码登录: token=" + token + ", userId=" + userId + ", tenantId=" + tenantId);
|
||||||
String redisKey = "qr-login:token:" + token;
|
String redisKey = "qr-login:token:" + token;
|
||||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||||
if (qrLoginData == null) {
|
if (qrLoginData == null) {
|
||||||
@@ -352,9 +356,14 @@ public class WxOfficialController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
redisUtil.set(redisKey, qrLoginData, ttlSeconds, TimeUnit.SECONDS);
|
redisUtil.set(redisKey, qrLoginData, ttlSeconds, TimeUnit.SECONDS);
|
||||||
log.info("扫码登录状态已更新,token={}, userId={}, status={}", token, userId, qrLoginData.getStatus());
|
log.info("扫码登录状态已更新,token={}, userId={}, status={}, needBindPhone={}, message={}, ttlSeconds={}",
|
||||||
|
token, userId, qrLoginData.getStatus(), qrLoginData.getNeedBindPhone(),
|
||||||
|
qrLoginData.getMessage(), ttlSeconds);
|
||||||
|
System.out.println("扫码登录完成,token=" + token + ", userId=" + userId +
|
||||||
|
", status=" + qrLoginData.getStatus() + ", message=" + qrLoginData.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("完成扫码登录失败", e);
|
log.error("完成扫码登录失败", e);
|
||||||
|
System.out.println("完成扫码登录异常: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,12 +64,15 @@ public class UserSyncService {
|
|||||||
userCache.put("phone", user.getPhone());
|
userCache.put("phone", user.getPhone());
|
||||||
userCache.put("status", user.getStatus());
|
userCache.put("status", user.getStatus());
|
||||||
userCache.put("updateTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
userCache.put("updateTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||||
|
// tenantId 可能为 null,但在 websopy 端是必填字段,至少传 0
|
||||||
|
Integer tenantIdValue = user.getTenantId() != null ? user.getTenantId() : 0;
|
||||||
|
userCache.put("tenantId", tenantIdValue);
|
||||||
|
|
||||||
String url = websopyBaseUrl + "/api/app/user-sync/single";
|
String url = websopyBaseUrl + "/api/app/user-sync/single";
|
||||||
String body = userCache.toJSONString();
|
String body = userCache.toJSONString();
|
||||||
|
|
||||||
log.info("同步用户到 websopy: userId={}, username={}, nickname={}, phone={}, url={}",
|
log.info("同步用户到 websopy: userId={}, username={}, nickname={}, phone={}, tenantId={}, url={}",
|
||||||
user.getUserId(), user.getUsername(), user.getNickname(), user.getPhone(), url);
|
user.getUserId(), user.getUsername(), user.getNickname(), user.getPhone(), user.getTenantId(), url);
|
||||||
log.debug("同步用户请求体: {}", body);
|
log.debug("同步用户请求体: {}", body);
|
||||||
|
|
||||||
// 发送 HTTP POST 请求
|
// 发送 HTTP POST 请求
|
||||||
|
|||||||
Reference in New Issue
Block a user