Browse Source

解决新建会话页面出现空白

gcz 2 weeks ago
parent
commit
a54043da81
2 changed files with 33 additions and 5 deletions
  1. 8 1
      src/layout/index.vue
  2. 25 4
      src/views/AIChat.vue

+ 8 - 1
src/layout/index.vue

@@ -204,7 +204,14 @@ const hidePhoneNumber = (phone) => {
 
 const handleNewChat = () => {
   // router.push('/ai-chat')
-  window.location.href = '/ai-chat'
+  router.push({
+    path: '/ai-chat',
+    query: {
+      historyId: null,
+      newChat: true
+    }
+  })
+  // window.location.href = '/ai-chat'
 }
 </script>
 

+ 25 - 4
src/views/AIChat.vue

@@ -97,7 +97,7 @@
 </template>
 
 <script setup>
-import { ref, onMounted, nextTick } from 'vue'
+import { ref, onMounted, nextTick, watch } from 'vue'
 import { useRoute } from 'vue-router'
 import userAvatar from '@/assets/user-avatar.png'
 import assistantAvatar from '@/assets/assistant-avatar.png'
@@ -425,12 +425,16 @@ const getHistoryChat = async (historyId) => {
 
           // 按照 data: 分割字符串
           const responses = answerContent.split('data:')
+          console.log('responses',responses);
           
           // 遍历所有分割后的响应
           for (const response of responses) {
+            console.log('response==========',response);
             if (response.trim() && !response.includes('"data": true')) {
+              console.log('response.trim()',response.trim());
               try {
                 const jsonData = JSON.parse(response.trim())
+                console.log('jsonData',jsonData);
                 if (jsonData.data && jsonData.data.answer) {
                   formattedAnswer += jsonData.data.answer
                 }
@@ -474,12 +478,29 @@ const getHistoryChat = async (historyId) => {
   }
 }
 
-onMounted(() => {
+// 初始化对话
+const initChat = async () => {
+  messages.value = []
+  await getKnowledgeList() // 获取最新的知识库列表
+  chatId.value = ''
+  sessionId.value = ''
+  isChating.value = false
+}
+
+// 监听路由参数变化
+watch(() => route.query.newChat, async (newVal) => {
+  if(newVal) {
+    await initChat()
+  }
+}, { immediate: true })
+
+onMounted(async () => {
   const historyId = route.query.historyId
   if (historyId) {
-    getHistoryChat(historyId)
+    await getHistoryChat(historyId)
+  } else {
+    await getKnowledgeList() // 如果不是历史记录,需要获取知识库列表
   }
-  getKnowledgeList()
   scrollToBottom()
 })
 </script>