From cef2b6e02e694779458eb47f4d33bf0884962bd6 Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Tue, 7 Apr 2026 15:10:26 +0800 Subject: [PATCH] =?UTF-8?q?chore=EF=BC=9A=E5=AE=A2=E6=9C=8D=E5=9B=9E?= =?UTF-8?q?=E7=AD=94=E4=B8=8D=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .local.config.js | 2 +- common/js/ai-service.js | 6 +- pages_tool/ai-chat/ai-chat-message.vue | 139 +++++++++++-------------- 3 files changed, 63 insertions(+), 84 deletions(-) diff --git a/.local.config.js b/.local.config.js index af9a195..f3c4732 100644 --- a/.local.config.js +++ b/.local.config.js @@ -46,6 +46,6 @@ const localDevConfig = ({ uniacid: 2847, domain: 'https://xcx6.aigc-quickapp.com/', }, -})['2847']; // 选择要使用的环境配置 +})['1']; // 选择要使用的环境配置 export default localDevConfig; \ No newline at end of file diff --git a/common/js/ai-service.js b/common/js/ai-service.js index f88c7a2..e2f05ae 100644 --- a/common/js/ai-service.js +++ b/common/js/ai-service.js @@ -110,20 +110,20 @@ export default { } // 处理流式消息块 - else if (data.type === 'message' || data.event === 'message') { + else if (data.event === 'message') { const text = data.answer || data.content || data.text || ''; content += text; if (onChunk) onChunk(text); } // 处理流结束 - else if (data.event === 'message_end' || data.type === 'message_end') { + else if (data.event === 'message_end') { conversationId = data.conversation_id || ''; if (conversationId) { this.setConversationId(conversationId); } if (onComplete) { - onComplete({ content, conversation_id: conversationId }); + onComplete({ content, conversation_id: conversationId }); } resolve({ content, conversation_id: conversationId }); socketTask.close(); diff --git a/pages_tool/ai-chat/ai-chat-message.vue b/pages_tool/ai-chat/ai-chat-message.vue index d1eba61..dabe25a 100644 --- a/pages_tool/ai-chat/ai-chat-message.vue +++ b/pages_tool/ai-chat/ai-chat-message.vue @@ -418,50 +418,22 @@ export default { isFetchingHistory: false } }, - 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() - }) - }, + 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(); + }, watch: { messages: { handler() { @@ -530,42 +502,49 @@ export default { } }, // 👇 新增:加载历史记录 - 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; - } - } - }, +// 👇 修正:加载历史记录 +async loadChatHistoryIfExist() { + const convId = aiService.getConversationId(); + if (convId) { + try { + // ✅ 调用正确的方法,并传入会话ID + const response = await aiService.getConversationHistory({ + conversation_id: convId + }); + + if (!response.success || !Array.isArray(response.messages)) { + console.warn('历史记录为空或格式无效'); + return; + } + + // 注意:getConversationHistory 返回的数据结构与 getChatHistory 预期的不同 + // 需要根据实际返回的 response.data.messages 来映射 + this.messages = response.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 * 1000, // 后端时间戳通常是秒,前端需要毫秒 + actions: msg.role !== 'user' ? [ + { id: 1, text: '有帮助', type: 'like' }, + { id: 2, text: '没帮助', type: 'dislike' } + ] : [] + }; + }); + } catch (error) { + console.error('加载历史记录失败:', error); + // 只需调用服务层的清除方法,它会处理本地存储 + aiService.clearConversationId(); + this.currentConversationId = null; + } + } +}, // 新增:初始化用户昵称(支持缓存持久化) initUserNicknameData() { // 1. 优先读取本地缓存的昵称