This commit is contained in:
van
2026-04-01 16:39:03 +08:00
parent 921c8a2374
commit 9ae74c999e
3 changed files with 19 additions and 10 deletions

View File

@@ -35,7 +35,10 @@ public class WeComChatSession {
this.lastInteractionTime = LocalDateTime.now().format(FMT);
}
public boolean isLogisticsWaitRemark() {
/**
* 是否处于「已收物流链、待备注」步骤。方法名避免使用 isXxx防止 Fastjson 序列化 Redis JSON 时混入多余布尔字段。
*/
public boolean matchLogisticsWaitRemark() {
return SCENE_JD_LOGISTICS_SHARE.equals(scene) && STEP_WAIT_REMARK.equals(step)
&& logisticsUrl != null && !logisticsUrl.isEmpty();
}

View File

@@ -40,23 +40,29 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
if (!StringUtils.hasText(wecomUserId)) {
return null;
}
String json = stringRedisTemplate.opsForValue().get(WeComConvention.sessionRedisKey(wecomUserId.trim()));
String key = WeComConvention.sessionRedisKey(wecomUserId.trim());
String json = stringRedisTemplate.opsForValue().get(key);
if (!StringUtils.hasText(json)) {
log.debug("企微会话 Redis 未命中 key={}", key);
return null;
}
try {
WeComChatSession s = JSON.parseObject(json, WeComChatSession.class);
if (s == null || !StringUtils.hasText(s.getLastInteractionTime())) {
log.info("企微会话 Redis 命中但缺字段 userId={} key={}", wecomUserId, WeComConvention.sessionRedisKey(wecomUserId.trim()));
return null;
}
LocalDateTime last = LocalDateTime.parse(s.getLastInteractionTime(), FMT);
if (ChronoUnit.MINUTES.between(last, LocalDateTime.now()) > idleTimeoutMinutes) {
stringRedisTemplate.delete(WeComConvention.sessionRedisKey(wecomUserId.trim()));
log.info("企微会话空闲超时已删 userId={} key={}", wecomUserId, WeComConvention.sessionRedisKey(wecomUserId.trim()));
return null;
}
log.debug("企微会话读取成功 userId={} scene={} step={}", wecomUserId, s.getScene(), s.getStep());
return s;
} catch (Exception e) {
log.warn("解析企微会话失败 userId={}", wecomUserId, e);
log.warn("解析企微会话失败 userId={} key={} jsonSnippet={}", wecomUserId, WeComConvention.sessionRedisKey(wecomUserId.trim()),
json != null && json.length() > 120 ? json.substring(0, 120) + "..." : json, e);
return null;
}
}
@@ -67,11 +73,10 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
return;
}
session.touch();
stringRedisTemplate.opsForValue().set(
WeComConvention.sessionRedisKey(wecomUserId.trim()),
JSON.toJSONString(session),
sessionTtlMinutes,
TimeUnit.MINUTES);
String key = WeComConvention.sessionRedisKey(wecomUserId.trim());
String payload = JSON.toJSONString(session);
stringRedisTemplate.opsForValue().set(key, payload, sessionTtlMinutes, TimeUnit.MINUTES);
log.info("企微会话已写入 Redis key={} ttlMin={} bytes={}", key, sessionTtlMinutes, payload.length());
}
@Override

View File

@@ -56,12 +56,12 @@ public class WeComInboundServiceImpl implements IWeComInboundService {
}
WeComChatSession session = weComChatSessionService.get(from);
if (session != null && !session.isLogisticsWaitRemark()) {
if (session != null && !session.matchLogisticsWaitRemark()) {
weComChatSessionService.delete(from);
session = null;
}
if (session != null && session.isLogisticsWaitRemark()) {
if (session != null && session.matchLogisticsWaitRemark()) {
String t = content.trim();
if ("取消".equals(t) || "取消录入".equals(t)) {
weComChatSessionService.delete(from);
@@ -91,6 +91,7 @@ public class WeComInboundServiceImpl implements IWeComInboundService {
String url = extractJd3cnUrl(content);
if (url != null) {
weComChatSessionService.put(from, WeComChatSession.startLogisticsWaitRemark(url));
log.info("企微物流多轮会话已创建 user={} url={}", from, url);
return "收到物流链接 " + url + " ,请输入备注信息";
}
}