Browse Source

新建对话优化

gcz 2 weeks ago
parent
commit
7b6c7e9ed9
1 changed files with 14 additions and 7 deletions
  1. 14 7
      src/views/AIChat.vue

+ 14 - 7
src/views/AIChat.vue

@@ -98,7 +98,7 @@
 
 <script setup>
 import { ref, onMounted, nextTick, watch } from 'vue'
-import { useRoute } from 'vue-router'
+import { useRoute, useRouter } from 'vue-router'
 import userAvatar from '@/assets/user-avatar.png'
 import assistantAvatar from '@/assets/assistant-avatar.png'
 import { post,get } from '@/utils/request'
@@ -110,6 +110,7 @@ import { useUserStore } from '../stores/user'
 
 const userStore = useUserStore()
 const route = useRoute()
+const router = useRouter()
 const messagesRef = ref(null)
 const inputMessage = ref('')
 const loading = ref(false)
@@ -527,17 +528,23 @@ const getHistoryChat = async (historyId) => {
 
 // 初始化对话
 const initChat = async () => {
-  messages.value = []
-  await getKnowledgeList() // 获取最新的知识库列表
-  chatId.value = ''
-  sessionId.value = ''
-  isChating.value = false
+  messages.value = []  // 清除所有消息
+  chatId.value = ''    // 清除对话 ID
+  sessionId.value = '' // 清除会话 ID
+  isChating.value = false  // 关闭对话状态
+  inputMessage.value = ''  // 清空输入框
+  loading.value = false    // 重置加载状态
+  await getKnowledgeList() // 重新获取知识库列表
 }
 
 // 监听路由参数变化
 watch(() => route.query.newChat, async (newVal) => {
-  if(newVal) {
+  if(newVal === 'true') {  // 确保检查的是字符串 'true'
     await initChat()
+    // 清除 URL 中的 newChat 参数,防止重复触发
+    router.replace({
+      query: { ...route.query, newChat: undefined }
+    })
   }
 }, { immediate: true })