1
This commit is contained in:
@@ -1,15 +1,20 @@
|
||||
package com.ruoyi.jarvis.service;
|
||||
|
||||
import com.ruoyi.jarvis.domain.dto.WeComChatSession;
|
||||
import com.ruoyi.jarvis.wecom.WeComConvention;
|
||||
|
||||
/**
|
||||
* 企微多轮会话(Redis JSON,键前缀与旧版 JDUtil「interaction_state」思路一致:interaction_state:wecom:{userId})
|
||||
* 企微多轮会话(Redis JSON)。键由 {@link WeComConvention#sessionRedisKey(String)} 生成,
|
||||
* 参数 wecomUserId 必须为明文 XML 中的 <b>FromUserName</b>(成员 UserID),与 wxSend 转发字段一致。
|
||||
*/
|
||||
public interface IWeComChatSessionService {
|
||||
|
||||
/** @param wecomUserId 成员 UserID,同 FromUserName */
|
||||
WeComChatSession get(String wecomUserId);
|
||||
|
||||
/** @param wecomUserId 成员 UserID,同 FromUserName */
|
||||
void put(String wecomUserId, WeComChatSession session);
|
||||
|
||||
/** @param wecomUserId 成员 UserID,同 FromUserName */
|
||||
void delete(String wecomUserId);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.ruoyi.jarvis.service.impl;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.jarvis.domain.dto.WeComChatSession;
|
||||
import com.ruoyi.jarvis.service.IWeComChatSessionService;
|
||||
import com.ruoyi.jarvis.wecom.WeComConvention;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -23,9 +24,6 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WeComChatSessionServiceImpl.class);
|
||||
|
||||
/** 与 JDUtil 的 interaction_state 命名风格一致,避免与个人微信会话键冲突加 wecom 命名空间 */
|
||||
public static final String REDIS_PREFIX = "interaction_state:wecom:";
|
||||
|
||||
private static final DateTimeFormatter FMT = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
|
||||
@Resource
|
||||
@@ -42,7 +40,7 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
if (!StringUtils.hasText(wecomUserId)) {
|
||||
return null;
|
||||
}
|
||||
String json = stringRedisTemplate.opsForValue().get(REDIS_PREFIX + wecomUserId.trim());
|
||||
String json = stringRedisTemplate.opsForValue().get(WeComConvention.sessionRedisKey(wecomUserId.trim()));
|
||||
if (!StringUtils.hasText(json)) {
|
||||
return null;
|
||||
}
|
||||
@@ -53,7 +51,7 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
}
|
||||
LocalDateTime last = LocalDateTime.parse(s.getLastInteractionTime(), FMT);
|
||||
if (ChronoUnit.MINUTES.between(last, LocalDateTime.now()) > idleTimeoutMinutes) {
|
||||
stringRedisTemplate.delete(REDIS_PREFIX + wecomUserId.trim());
|
||||
stringRedisTemplate.delete(WeComConvention.sessionRedisKey(wecomUserId.trim()));
|
||||
return null;
|
||||
}
|
||||
return s;
|
||||
@@ -70,7 +68,7 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
}
|
||||
session.touch();
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
REDIS_PREFIX + wecomUserId.trim(),
|
||||
WeComConvention.sessionRedisKey(wecomUserId.trim()),
|
||||
JSON.toJSONString(session),
|
||||
sessionTtlMinutes,
|
||||
TimeUnit.MINUTES);
|
||||
@@ -81,7 +79,7 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
if (!StringUtils.hasText(wecomUserId)) {
|
||||
return;
|
||||
}
|
||||
stringRedisTemplate.delete(REDIS_PREFIX + wecomUserId.trim());
|
||||
stringRedisTemplate.delete(WeComConvention.sessionRedisKey(wecomUserId.trim()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +88,7 @@ public class WeComChatSessionServiceImpl implements IWeComChatSessionService {
|
||||
@Scheduled(fixedDelayString = "${jarvis.wecom.session-sweep-ms:60000}")
|
||||
public void sweepIdleSessions() {
|
||||
try {
|
||||
Set<String> keys = stringRedisTemplate.keys(REDIS_PREFIX + "*");
|
||||
Set<String> keys = stringRedisTemplate.keys(WeComConvention.SESSION_REDIS_KEY_PREFIX + "*");
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* LinPingFan:全部指令;其他人员:须 super_admin.wxid=企微UserID,且仅「京*」指令 + 京东分享物流链接流程。
|
||||
* 多轮录入使用 Redis 会话({@link WeComChatSession},键 interaction_state:wecom:*),与旧版「开通礼金」interaction_state 机制一致。
|
||||
* LinPingFan:全部指令;其他人员:须在超级管理员中识别为本人(wxid=企微 UserID,**或** 企微 UserID 出现在 touser 逗号分隔列表中),且仅「京*」指令 + 京东分享物流链接流程。
|
||||
* 多轮会话使用 Redis({@link WeComChatSession},键 interaction_state:wecom:{FromUserName}),与旧版「开通礼金」interaction_state 思路一致。
|
||||
*/
|
||||
@Service
|
||||
public class WeComInboundServiceImpl implements IWeComInboundService {
|
||||
@@ -52,7 +52,7 @@ public class WeComInboundServiceImpl implements IWeComInboundService {
|
||||
SuperAdmin row = superAdminService.selectSuperAdminByWecomUserId(from);
|
||||
boolean isSuper = WE_COM_SUPER_USER_ID.equals(from);
|
||||
if (!isSuper && row == null) {
|
||||
return "无权限:请在后台「超级管理员」中维护企微 UserID(wxid 字段)";
|
||||
return "无权限:请在后台「超级管理员」将您的企微 UserID 填到 wxid,或加入该行的 touser(逗号分隔)";
|
||||
}
|
||||
|
||||
WeComChatSession session = weComChatSessionService.get(from);
|
||||
|
||||
Reference in New Issue
Block a user