From 962ea124fab25610403ca952d38cb84649f178fc Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Wed, 8 Apr 2026 09:14:57 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E9=80=80=E5=87=BA=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=90=8E=EF=BC=8C=E5=9C=A8=E8=BF=9B=E5=85=A5?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E9=A1=B5=E9=9D=A2=E5=90=8E=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=9A=84=E6=B0=94=E6=B3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages_tool/ai-chat/ai-chat-message.vue | 76 ++++++++++++++++++++------ 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/pages_tool/ai-chat/ai-chat-message.vue b/pages_tool/ai-chat/ai-chat-message.vue index 6dd3a12..8fd8677 100644 --- a/pages_tool/ai-chat/ai-chat-message.vue +++ b/pages_tool/ai-chat/ai-chat-message.vue @@ -15,7 +15,7 @@ @@ -418,22 +418,50 @@ export default { isFetchingHistory: false } }, - created() { - this.messages = [...this.initialMessages] - this.messageId = this.messages.length - this.initAudioContext() - // 初始化用户头像(缓存优先) - this.initUserAvatarData() - // 初始化用户昵称(缓存优先) - this.initUserNicknameData() - - // 👇 关键:在这里加载历史记录,只执行一次 - this.loadChatHistoryIfExist().then(() => { - this.historyOffset = this.messages.length; - }); - - this.currentConversationId = aiService.getConversationId(); - }, + onShow() { + // 如果不是 AI 客服,立即退出并跳转 + if (customerServiceType !== 'ai') { + uni.showToast({ title: '当前客服类型不支持此页面', icon: 'none' }); + setTimeout(() => { + uni.navigateBack({ delta: 1 }); // 或 redirectTo 首页 + }, 1000); + return; // ⚠️ 关键:阻止后续初始化 + } + // 优先读取本地缓存的会话 ID + const localConvId = this.getConversationIdFromLocal(); + if (localConvId) { + this.currentConversationId = localConvId; + aiService.setConversationId(localConvId); + } else { + this.currentConversationId = aiService.getConversationId(); + } + // 加载初始历史后,同步historyOffset + this.loadChatHistoryIfExist().then(() => { + // 初始历史条数作为偏移量 + this.historyOffset = this.messages.length; + }); + // 移除:this.historyOffset = 0;(避免覆盖初始历史的偏移量) + }, + // 新增:组件销毁时清除防抖定时器 + beforeDestroy() { + if (this.scrollTimer) clearTimeout(this.scrollTimer); + }, + created() { + this.messages = [...this.initialMessages] + this.messageId = this.messages.length + this.initAudioContext() + // 初始化用户头像(缓存优先) + this.initUserAvatarData() + // 初始化用户昵称(缓存优先) + this.initUserNicknameData() + this.currentConversationId = aiService.getConversationId(); + }, + mounted() { + // 初始加载时滚底(仅一次) + this.$nextTick(() => { + this.scrollToBottom() + }) + }, watch: { messages: { handler() { @@ -536,7 +564,7 @@ async loadChatHistoryIfExist() { { id: 2, text: '没帮助', type: 'dislike' } ] : [] }; - }); + }).filter(msg => msg.content.trim() !== ''); // 过滤掉内容为空的消息 } catch (error) { console.error('加载历史记录失败:', error); // 只需调用服务层的清除方法,它会处理本地存储 @@ -865,6 +893,12 @@ async loadChatHistoryIfExist() { // 标记流式结束 streamMessage.isStreaming = false; + // 检查内容是否为空,如果为空则移除该消息 + if (!streamMessage.content || streamMessage.content.trim() === '') { + this.messages = this.messages.filter(msg => msg.id !== streamMessage.id); + return; + } + // 添加操作按钮 streamMessage.actions = [ { id: 1, text: '有帮助', type: 'like' }, @@ -900,6 +934,12 @@ async loadChatHistoryIfExist() { // 标记流式结束 streamMessage.isStreaming = false; + // 检查内容是否为空,如果为空则移除该消息 + if (!streamMessage.content || streamMessage.content.trim() === '') { + this.messages = this.messages.filter(msg => msg.id !== streamMessage.id); + return; + } + // 添加操作按钮 streamMessage.actions = [ { id: 1, text: '有帮助', type: 'like' },