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' },