From 3162d6a9f2fec79033fd557963ff9e5bbb13cd19 Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Wed, 15 Apr 2026 09:08:10 +0800 Subject: [PATCH] =?UTF-8?q?chore=EF=BC=9AH5=E7=9A=84=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E8=81=8A=E5=A4=A9=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages_tool/ai-chat/ai-chat-message.vue | 52 +++++++++++++------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/pages_tool/ai-chat/ai-chat-message.vue b/pages_tool/ai-chat/ai-chat-message.vue index 95ab7fe..bf88263 100644 --- a/pages_tool/ai-chat/ai-chat-message.vue +++ b/pages_tool/ai-chat/ai-chat-message.vue @@ -496,13 +496,13 @@ export default { }, watch: { messages: { - handler() { - this.$nextTick(() => { - // 仅当不是加载历史时,执行滚底 - if (!this.isLoadingHistory) { + handler(newVal, oldVal) { + // 仅在新增消息时触发滚动,流式响应时由手动控制 + if (!this.isLoadingHistory && !this.isStreaming && newVal.length !== oldVal.length) { + this.$nextTick(() => { this.scrollToBottom() - } - }) + }) + } }, deep: true } @@ -901,6 +901,11 @@ async loadChatHistoryIfExist() { this.messages = this.messages.filter(msg => msg.type !== 'loading'); this.shouldScrollToBottom = true; this.messages.push(streamMessage); + + // 添加消息后立即滚动一次 + this.$nextTick(() => { + this.scrollToBottom(); + }); try { // #ifdef H5 @@ -908,10 +913,8 @@ async loadChatHistoryIfExist() { await aiService.sendHttpStream( userMessage, (chunk) => { - // 实时更新内容 + // 实时更新内容(不频繁滚动) streamMessage.content += chunk; - this.$forceUpdate(); // 强制更新视图 - this.scrollToBottom(); // 滚动到底部 }, (completeResult) => { // 流结束回调 @@ -930,30 +933,31 @@ async loadChatHistoryIfExist() { return; } - - // 保存会话 ID if (convId) { this.currentConversationId = convId; aiService.setConversationId(convId); this.saveConversationIdToLocal(convId); } + + // 流结束后再滚动到底部 + this.$nextTick(() => { + this.scrollToBottom(); + }); // 触发事件 this.$emit('ai-response', streamMessage); } ); // #endif - + // #ifdef MP-WEIXIN // ===== 微信小程序: 使用 WebSocket ===== await aiService.sendStreamMessage( userMessage, (chunk) => { - // 实时更新内容 + // 实时更新内容(不频繁滚动) streamMessage.content += chunk; - this.$forceUpdate(); - this.scrollToBottom(); // 滚动到底部 }, (completeResult) => { // 流结束回调 @@ -968,14 +972,17 @@ async loadChatHistoryIfExist() { return; } - - // 保存会话 ID if (convId) { this.currentConversationId = convId; aiService.setConversationId(convId); this.saveConversationIdToLocal(convId); } + + // 流结束后再滚动到底部 + this.$nextTick(() => { + this.scrollToBottom(); + }); // 触发事件 this.$emit('ai-response', streamMessage); @@ -1080,23 +1087,18 @@ async loadChatHistoryIfExist() { const lastMessage = filteredMessages[filteredMessages.length - 1]; const lastIndex = this.messages.indexOf(lastMessage); this.scrollIntoViewId = `msg-${lastMessage.id || lastMessage.timestamp}-${lastIndex}`; - this.$nextTick(() => { - setTimeout(() => { - this.scrollIntoViewId = null; - }, 100); - }); + // 保持 scrollIntoViewId 不清除,避免频繁切换 } } // #endif // #ifdef MP-WEIXIN - // 先重置为0,再设置大值,确保视图更新 - this.scrollTop = 0; + // 直接设置大值滚动到底部,不要重置为0 this.$nextTick(() => { this.scrollTop = 9999999; }); // #endif - }, 50); + }, 100); }, // 加载更多历史消息