Ver código fonte

1、修复智能问答缓存key加入角色id

liyanbo 3 meses atrás
pai
commit
e78decfe46

+ 37 - 11
src/components/ai/text/TextToText.vue

@@ -92,8 +92,8 @@ import DefaultMessage from "@/components/DefaultMessage/index.vue";
 const props = defineProps({
   //根据需求传输
   isCourse: { type: Boolean, default: false},
-  cacheDataKey: { type: String, default: localStorage.getItem('token') + "_ai_textToText" },
-  cacheDataHistoryKey: { type: String, default: localStorage.getItem('token') + "_ai_textToText_history" },
+  cacheDataKey: { type: String, default: null },
+  cacheDataHistoryKey: { type: String, default: null },
   preDialogueList: { type: Array, default: () => []},
   replySupplement: { type: String, default: ''},
 
@@ -103,6 +103,33 @@ const props = defineProps({
   personIntroduce: { type: String},
 })
 
+// 计算带personId的缓存键
+const computedCacheDataKey = computed(() => {
+  let cacheKey = props.cacheDataKey;
+  if (!cacheKey) {
+    cacheKey = localStorage.getItem('token') + "_ai_textToText";
+  }
+  if (props.personId) {
+    cacheKey += `_${props.personId}`;
+  }
+  return cacheKey;
+});
+
+// 计算带personId的历史记录缓存键
+const computedCacheDataHistoryKey = computed(() => {
+  let historyKey = props.cacheDataHistoryKey;
+  if (!historyKey) {
+    historyKey = localStorage.getItem('token') + "_ai_textToText";
+  }
+  if (props.personId) {
+    historyKey += `_${props.personId}`;
+  }
+  if (!props.cacheDataHistoryKey){
+    historyKey += "_history";
+  }
+  return historyKey;
+});
+
 // 定义emits
 const emits = defineEmits(['saveProgress'])
 
@@ -195,8 +222,7 @@ const getConversation = async (id) => {
   // )
 
   // 取缓存历史记录
-  let cacheDataHistoryStr = localStorage.getItem(props.cacheDataHistoryKey);
-  console.log("cacheDataHistoryStr", cacheDataHistoryStr);
+  let cacheDataHistoryStr = localStorage.getItem(computedCacheDataHistoryKey.value);
   if (cacheDataHistoryStr) {
     cacheDataHistoryList.value = JSON.parse(cacheDataHistoryStr) || [];
     activeMessageList.value.push(...cacheDataHistoryList.value);
@@ -204,7 +230,7 @@ const getConversation = async (id) => {
     // 加入预置对话
     activeMessageList.value.push(...props.preDialogueList);
     cacheDataHistoryList.value.push(...props.preDialogueList);
-    localStorage.setItem(props.cacheDataHistoryKey, JSON.stringify(cacheDataHistoryList.value));
+    localStorage.setItem(computedCacheDataHistoryKey.value, JSON.stringify(cacheDataHistoryList.value));
   }
 };
 
@@ -305,7 +331,7 @@ const doSendMessage = async (content) => {
     content: content,
     createTime: new Date(),
   });
-  localStorage.setItem(props.cacheDataHistoryKey, JSON.stringify(cacheDataHistoryList.value));
+  localStorage.setItem(computedCacheDataHistoryKey.value, JSON.stringify(cacheDataHistoryList.value));
 
 
   // 执行发送
@@ -548,7 +574,7 @@ const textRoll = async () => {
             content: receiveMessageFullText.value,
             createTime: new Date(),
           });
-          localStorage.setItem(props.cacheDataHistoryKey, JSON.stringify(cacheDataHistoryList.value));
+          localStorage.setItem(computedCacheDataHistoryKey.value, JSON.stringify(cacheDataHistoryList.value));
         }
 
         // 不是对话中可以结束
@@ -602,17 +628,17 @@ onMounted(async () => {
   selectedImage.value = personImage.value;
 
   //创建对话
-  let cacheDataKeyData = localStorage.getItem(props.cacheDataKey);
+  let cacheDataKeyData = localStorage.getItem(computedCacheDataKey.value);
   if (cacheDataKeyData) {
     activeConversationId.value = cacheDataKeyData;
   }else{
     // 智能问答
     CreateDialogue({ roleId: personId.value })
         .then((res) => {
-          console.log("创建会话:", res);
+          console.log("创建会话:", res.data);
           activeConversationId.value = res.data;
           //将会话存入会话缓存
-          localStorage.setItem(props.cacheDataKey, activeConversationId.value)
+          localStorage.setItem(computedCacheDataKey.value, activeConversationId.value)
         })
         .catch((error) => {
           console.error("请求出错:", error);
@@ -905,4 +931,4 @@ defineExpose({
   cursor: pointer;
   box-shadow: 0 0px 2px rgba(0, 0, 0, 0.3);
 }
-</style>
+</style>

+ 1 - 1
src/components/videopage/DialogComponents.vue

@@ -287,7 +287,7 @@ const createAiChart = async () => {
   // 智能问答
   await CreateDialogue({ roleId: xZAiData.value.id })
     .then(res => {
-      console.log("创建会话:", res);
+      console.log("创建会话:", res.data);
       activeConversationId.value = res.data
     })
     .catch(error => {

+ 0 - 1
src/utils/loginUtils.js

@@ -388,7 +388,6 @@ const removeLocalStorage = () => {
 }
 
 const removeLocalStorageKey = (remKey) => {
-  console.log('removeLocalStorageKey---', remKey)
   if (remKey) {
     for (let i = 0; i < localStorage.length; i++) {
       const key = localStorage.key(i);