From 8d84306747f1888afc0c343c47c4d7dbe9d3c3a7 Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Tue, 16 Dec 2025 11:32:40 +0800 Subject: [PATCH] =?UTF-8?q?chore=EF=BC=9A=E5=BE=80=E4=B8=8A=E7=BF=BB?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=EF=BC=8C=E4=B8=8D=E5=9C=A8?= =?UTF-8?q?=E4=B9=B1=E7=AA=9C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai-chat-message/ai-chat-message.vue | 105 ++++++++++-------- 1 file changed, 59 insertions(+), 46 deletions(-) diff --git a/components/ai-chat-message/ai-chat-message.vue b/components/ai-chat-message/ai-chat-message.vue index ff4f0c1..aaa70be 100644 --- a/components/ai-chat-message/ai-chat-message.vue +++ b/components/ai-chat-message/ai-chat-message.vue @@ -438,7 +438,7 @@ export default { isAIServiceAvailable: true, aiServiceError: null, isLoadingHistory: false, // 新增:标记历史加载状态 - + shouldScrollToBottom: false } }, onShow() { @@ -464,16 +464,24 @@ export default { this.initUserNicknameData() this.currentConversationId = aiService.getConversationId(); }, - mounted() { - this.scrollToBottom() - }, + mounted() { + // 初始加载时滚底(仅一次) + this.$nextTick(() => { + this.scrollToBottom() + }) + }, watch: { - messages: { - handler() { - this.$nextTick(() => { - this.scrollToBottom() - }) - }, + messages: { + handler() { + this.$nextTick(() => { + // 仅当不是加载历史、且需要滚底时,才执行滚底 + if (!this.isLoadingHistory && this.shouldScrollToBottom) { + this.scrollToBottom() + // 滚底后重置状态,避免重复触发 + this.shouldScrollToBottom = false + } + }) + }, deep: true } }, @@ -519,38 +527,42 @@ export default { } }, // 👇 新增:加载历史记录 - async loadChatHistoryIfExist() { - const convId = aiService.getConversationId(); - if (convId) { - try { - const history = await aiService.getConversationHistory({ - conversation_id: convId, - uniacid: this.$store.state.uniacid, - user_id: this.$store.state.memberInfo?.id || 'anonymous' - }); - - if (history.success && history.messages?.length > 0) { - // 格式化消息 - this.messages = history.messages.map(msg => ({ - id: msg.message_id || (Date.now() + Math.random() * 10000), - role: msg.role === 'user' ? 'user' : 'assistant', - content: msg.content || msg.answer || msg.text || '', - timestamp: msg.created_at, - actions: msg.role !== 'user' ? [ - { id: 1, text: '有帮助', type: 'like' }, - { id: 2, text: '没帮助', type: 'dislike' } - ] : [] - })); - } - } catch (error) { - console.error('加载历史记录失败:', error); - // 可选:清除无效ID - aiService.clearConversationId(); - this.clearConversationIdFromLocal(); - this.currentConversationId = null; - } - } - }, + async loadChatHistoryIfExist() { + const convId = aiService.getConversationId(); + if (convId) { + try { + const history = await aiService.getChatHistory(); + if (!history || !Array.isArray(history.messages)) { + console.warn('历史记录为空或格式无效'); + return; + } + + this.messages = history.messages.map(msg => { + let content = ''; + if (msg.role === 'user') { + content = msg.query || (typeof msg.inputs === 'string' ? msg.inputs : JSON.stringify(msg.inputs || '')) || ''; + } else { + content = msg.answer || ''; + } + return { + id: msg.message_id || (Date.now() + Math.random() * 10000), + role: msg.role === 'user' ? 'user' : 'assistant', + content: content, + timestamp: msg.created_at, + actions: msg.role !== 'user' ? [ + { id: 1, text: '有帮助', type: 'like' }, + { id: 2, text: '没帮助', type: 'dislike' } + ] : [] + }; + }); + } catch (error) { + console.error('加载历史记录失败:', error); + aiService.clearConversationId(); + this.clearConversationIdFromLocal(); + this.currentConversationId = null; + } + } + }, // 新增:初始化用户昵称(支持缓存持久化) initUserNicknameData() { // 1. 优先读取本地缓存的昵称 @@ -688,7 +700,7 @@ export default { content: this.inputText.trim(), timestamp: Date.now() } - + this.shouldScrollToBottom = true this.messages.push(userMessage) this.inputText = '' @@ -700,7 +712,7 @@ export default { content: '', timestamp: Date.now() } - + this.shouldScrollToBottom = true this.messages.push(loadingMessage) try { @@ -736,7 +748,7 @@ export default { { id: 1, text: '重试', type: 'retry' } ] } - + this.shouldScrollToBottom = true this.messages.push(errorMessage) this.$emit('ai-response', errorMessage) } @@ -775,7 +787,7 @@ export default { { id: 2, text: '没帮助', type: 'dislike' } ] } - + this.shouldScrollToBottom = true this.messages.push(aiMessage) this.$emit('ai-response', aiMessage) }, @@ -794,6 +806,7 @@ export default { // 移除加载状态,添加流式消息 this.messages = this.messages.filter(msg => msg.type !== 'loading') + this.shouldScrollToBottom = true this.messages.push(streamMessage) // 开始流式响应