From 158a0190b5a7b93c0d31541c1e350e5221519fb1 Mon Sep 17 00:00:00 2001 From: ageer <ageerle@163.com> Date: 星期五, 16 五月 2025 23:52:43 +0800 Subject: [PATCH] feat: 优化会话管理查询逻辑 --- ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/ChatSessionController.java | 7 +++ ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/knowledge/KnowledgeInfoServiceImpl.java | 10 +++-- ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java | 71 +++++++++-------------------------- ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/domain/bo/ChatSessionBo.java | 3 - ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/service/impl/ChatMessageServiceImpl.java | 10 +++- 5 files changed, 38 insertions(+), 63 deletions(-) diff --git a/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/domain/bo/ChatSessionBo.java b/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/domain/bo/ChatSessionBo.java index a6b23ba..a334ae6 100644 --- a/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/domain/bo/ChatSessionBo.java +++ b/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/domain/bo/ChatSessionBo.java @@ -29,13 +29,11 @@ /** * 鐢ㄦ埛id */ - @NotNull(message = "鐢ㄦ埛id涓嶈兘涓虹┖", groups = { AddGroup.class, EditGroup.class }) private Long userId; /** * 浼氳瘽鏍囬 */ - @NotBlank(message = "浼氳瘽鏍囬涓嶈兘涓虹┖", groups = { AddGroup.class, EditGroup.class }) private String sessionTitle; /** @@ -47,7 +45,6 @@ /** * 澶囨敞 */ - @NotBlank(message = "澶囨敞涓嶈兘涓虹┖", groups = { AddGroup.class, EditGroup.class }) private String remark; diff --git a/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/service/impl/ChatMessageServiceImpl.java b/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/service/impl/ChatMessageServiceImpl.java index 32c473d..087f58f 100644 --- a/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/service/impl/ChatMessageServiceImpl.java +++ b/ruoyi-modules-api/ruoyi-chat-api/src/main/java/org/ruoyi/service/impl/ChatMessageServiceImpl.java @@ -2,6 +2,7 @@ import org.ruoyi.common.core.utils.MapstructUtils; import org.ruoyi.common.core.utils.StringUtils; +import org.ruoyi.common.satoken.utils.LoginHelper; import org.ruoyi.core.page.TableDataInfo; import org.ruoyi.core.page.PageQuery; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -45,6 +46,10 @@ */ @Override public TableDataInfo<ChatMessageVo> queryPageList(ChatMessageBo bo, PageQuery pageQuery) { + if(!LoginHelper.isLogin()){ + return TableDataInfo.build(); + } + bo.setUserId(LoginHelper.getUserId()); LambdaQueryWrapper<ChatMessage> lqw = buildQueryWrapper(bo); Page<ChatMessageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw); return TableDataInfo.build(result); @@ -64,9 +69,8 @@ LambdaQueryWrapper<ChatMessage> lqw = Wrappers.lambdaQuery(); lqw.eq(bo.getUserId() != null, ChatMessage::getUserId, bo.getUserId()); lqw.eq(StringUtils.isNotBlank(bo.getContent()), ChatMessage::getContent, bo.getContent()); - lqw.eq(StringUtils.isNotBlank(bo.getRole()), ChatMessage::getRole, bo.getRole()); - lqw.eq(bo.getDeductCost() != null, ChatMessage::getDeductCost, bo.getDeductCost()); - lqw.eq(bo.getTotalTokens() != null, ChatMessage::getTotalTokens, bo.getTotalTokens()); + lqw.eq(bo.getSessionId() != null, ChatMessage::getSessionId, bo.getSessionId()); + lqw.like(StringUtils.isNotBlank(bo.getRole()), ChatMessage::getRole, bo.getRole()); lqw.like(StringUtils.isNotBlank(bo.getModelName()), ChatMessage::getModelName, bo.getModelName()); return lqw; } diff --git a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/ChatSessionController.java b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/ChatSessionController.java index 4c5e7dc..955bf5f 100644 --- a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/ChatSessionController.java +++ b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/controller/chat/ChatSessionController.java @@ -8,6 +8,7 @@ import cn.dev33.satoken.annotation.SaCheckPermission; import org.ruoyi.common.excel.utils.ExcelUtil; import org.ruoyi.common.idempotent.annotation.RepeatSubmit; +import org.ruoyi.common.satoken.utils.LoginHelper; import org.ruoyi.core.page.TableDataInfo; import org.ruoyi.domain.bo.ChatSessionBo; import org.ruoyi.domain.vo.ChatSessionVo; @@ -42,6 +43,12 @@ @SaCheckPermission("system:session:list") @GetMapping("/list") public TableDataInfo<ChatSessionVo> list(ChatSessionBo bo, PageQuery pageQuery) { + if(!LoginHelper.isLogin()){ + // 濡傛灉鐢ㄦ埛娌℃湁鐧诲綍,杩斿洖绌轰細璇濆垪琛� + return TableDataInfo.build(); + } + // 榛樿鏌ヨ褰撳墠鐢ㄦ埛浼氳瘽 + bo.setUserId(LoginHelper.getUserId()); return chatSessionService.queryPageList(bo, pageQuery); } diff --git a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java index 1387230..5c1e5ea 100644 --- a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java +++ b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java @@ -1,19 +1,14 @@ package org.ruoyi.chat.service.chat.impl; -import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.collection.CollectionUtil; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.google.protobuf.ServiceException; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import okhttp3.ResponseBody; -import org.ruoyi.chat.enums.ChatModeType; import org.ruoyi.chat.factory.ChatServiceFactory; import org.ruoyi.chat.service.chat.IChatCostService; import org.ruoyi.chat.service.chat.IChatService; import org.ruoyi.chat.service.chat.ISseService; -import org.ruoyi.chat.util.IpUtil; import org.ruoyi.chat.util.SSEUtil; import org.ruoyi.common.chat.config.LocalCache; import org.ruoyi.common.chat.entity.Tts.TextToSpeech; @@ -26,15 +21,14 @@ import org.ruoyi.common.core.utils.StringUtils; import org.ruoyi.common.core.utils.file.FileUtils; import org.ruoyi.common.core.utils.file.MimeTypeUtils; -import org.ruoyi.common.redis.utils.RedisUtils; import org.ruoyi.domain.bo.ChatSessionBo; import org.ruoyi.domain.bo.QueryVectorBo; import org.ruoyi.domain.vo.ChatModelVo; import org.ruoyi.domain.vo.KnowledgeInfoVo; -import org.ruoyi.service.IKnowledgeInfoService; -import org.ruoyi.service.VectorStoreService; import org.ruoyi.service.IChatModelService; import org.ruoyi.service.IChatSessionService; +import org.ruoyi.service.IKnowledgeInfoService; +import org.ruoyi.service.VectorStoreService; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; @@ -49,10 +43,12 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.time.Duration; import java.util.ArrayList; import java.util.List; +/** + * @author ageer + */ @Service @Slf4j @RequiredArgsConstructor @@ -81,24 +77,20 @@ try { // 鏋勫缓娑堟伅鍒楄〃 buildChatMessageList(chatRequest); - if (!StpUtil.isLogin()) { - // 鏈櫥褰曠敤鎴烽檺鍒跺璇濇鏁� - checkUnauthenticatedUserChatLimit(request); - }else { - LocalCache.CACHE.put("userId", chatCostService.getUserId()); - chatRequest.setUserId(chatCostService.getUserId()); - // 淇濆瓨浼氳瘽淇℃伅 - if(chatRequest.getSessionId()==null){ - ChatSessionBo chatSessionBo = new ChatSessionBo(); - chatSessionBo.setUserId(chatCostService.getUserId()); - chatSessionBo.setSessionTitle(getFirst10Characters(chatRequest.getPrompt())); - chatSessionBo.setSessionContent(chatRequest.getPrompt()); - chatSessionService.insertByBo(chatSessionBo); - chatRequest.setSessionId(chatSessionBo.getId()); - } - // 淇濆瓨娑堟伅璁板綍 骞舵墸闄よ垂鐢� - chatCostService.deductToken(chatRequest); + + LocalCache.CACHE.put("userId", chatCostService.getUserId()); + chatRequest.setUserId(chatCostService.getUserId()); + // 淇濆瓨浼氳瘽淇℃伅 + if(chatRequest.getSessionId()==null){ + ChatSessionBo chatSessionBo = new ChatSessionBo(); + chatSessionBo.setUserId(chatCostService.getUserId()); + chatSessionBo.setSessionTitle(getFirst10Characters(chatRequest.getPrompt())); + chatSessionBo.setSessionContent(chatRequest.getPrompt()); + chatSessionService.insertByBo(chatSessionBo); + chatRequest.setSessionId(chatSessionBo.getId()); } + // 淇濆瓨娑堟伅璁板綍 骞舵墸闄よ垂鐢� + chatCostService.deductToken(chatRequest); // 鏍规嵁妯″瀷鍒嗙被璋冪敤涓嶅悓鐨勫鐞嗛�昏緫 IChatService chatService = chatServiceFactory.getChatService(chatModelVo.getCategory()); chatService.chat(chatRequest, sseEmitter); @@ -124,33 +116,6 @@ // 濡傛灉闀垮害涓嶈冻10锛岃繑鍥炴暣涓瓧绗︿覆 return str; } - } - - /** - * 妫�鏌ユ湭鐧诲綍鐢ㄦ埛鏄惁瓒呰繃褰撴棩瀵硅瘽娆℃暟闄愬埗 - * - * @param request 褰撳墠璇锋眰 - * @throws ServiceException 濡傛灉褰撴棩鍏嶈垂娆℃暟宸茬敤瀹� - */ - public void checkUnauthenticatedUserChatLimit(HttpServletRequest request) throws ServiceException { - - String clientIp = IpUtil.getClientIp(request); - // 璁垮姣忓ぉ榛樿鍙兘瀵硅瘽5娆� - int timeWindowInSeconds = 5; - String redisKey = "clientIp:" + clientIp; - int count = 0; - // 妫�鏌edis涓殑瀵硅瘽娆℃暟 - if (RedisUtils.getCacheObject(redisKey) == null) { - // 缂撳瓨鏈夋晥鏃堕棿1澶� - RedisUtils.setCacheObject(redisKey, count, Duration.ofSeconds(86400)); - } else { - count = RedisUtils.getCacheObject(redisKey); - if (count >= timeWindowInSeconds) { - throw new ServiceException("褰撴棩鍏嶈垂娆℃暟宸茬敤瀹�"); - } - count++; - RedisUtils.setCacheObject(redisKey, count); - } } /** diff --git a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/knowledge/KnowledgeInfoServiceImpl.java b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/knowledge/KnowledgeInfoServiceImpl.java index c34b4af..89b6863 100644 --- a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/knowledge/KnowledgeInfoServiceImpl.java +++ b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/knowledge/KnowledgeInfoServiceImpl.java @@ -330,10 +330,11 @@ } } } + /** * 绗竴姝� 瀹氭椂 鎷嗚ВPDF鏂囦欢涓殑鍥剧墖 */ - @Scheduled(fixedDelay = 15000) // 姣�3绉掓墽琛屼竴娆� + //@Scheduled(fixedDelay = 15000) // 姣�3绉掓墽琛屼竴娆� public void dealKnowledgeAttachPic() throws Exception { //澶勭悊 鎷嗚ВPDF鏂囦欢涓殑鍥剧墖鐨勮褰� List<KnowledgeAttach> knowledgeAttaches = attachMapper.selectList( @@ -349,10 +350,11 @@ } } } + /** * 绗簩姝� 瀹氭椂 瑙f瀽鍥剧墖鍐呭 */ - @Scheduled(fixedDelay = 15000) + //@Scheduled(fixedDelay = 15000) public void dealKnowledgeAttachPicAnys() throws Exception { //鑾峰彇鏈鐞嗙殑鍥剧墖璁板綍 List<KnowledgeAttachPic> knowledgeAttachPics = picMapper.selectList( @@ -369,7 +371,7 @@ /** * 绗笁姝� 瀹氭椂 澶勭悊 闄勪欢涓婁紶鍚庝笂浼犲悜閲忔暟鎹簱 */ - @Scheduled(fixedDelay = 30000) // 姣�3绉掓墽琛屼竴娆� + //@Scheduled(fixedDelay = 30000) // 姣�3绉掓墽琛屼竴娆� public void dealKnowledgeAttachVector() throws Exception { //澶勭悊 闇�瑕佷笂浼犲悜閲忔暟鎹簱鐨勮褰� List<KnowledgeAttach> knowledgeAttaches = attachMapper.selectList( @@ -388,7 +390,7 @@ /** * 绗洓姝� 瀹氭椂 澶勭悊 澶辫触鏁版嵁 */ - @Scheduled(fixedDelay = 30 * 60 * 1000) + //@Scheduled(fixedDelay = 30 * 60 * 1000) public void dealKnowledge40Status() throws Exception { //鎷嗚ВPDF澶辫触 閲嶆柊璁剧疆鐘舵�� attachMapper.update(new LambdaUpdateWrapper<KnowledgeAttach>() -- Gitblit v1.9.3