diff --git a/common/js/ai-service.js b/common/js/ai-service.js index f76becc..1880d24 100644 --- a/common/js/ai-service.js +++ b/common/js/ai-service.js @@ -344,15 +344,21 @@ export default { if (jsonStr && jsonStr !== '[DONE]') { const data = JSON.parse(jsonStr); if (data.event === 'message') { - const text = data.answer || data.text || ''; + var text = data.answer || data.text || ''; + // 兼容嵌套 data.data 格式 + if (!text && data.data) { + text = data.data.content || data.data.answer || ''; + } content += text; if (onChunk) onChunk(text); } - if (data.conversation_id) { - conversationId = data.conversation_id; + // 兼容两种层级:conversation_id 可能在顶层或 data.data 中 + var cid = data.conversation_id || (data.data && data.data.conversation_id) || ''; + if (cid) { + conversationId = cid; } - if (data.event === 'message_end') { - // 可提前结束 + if (data.event === 'message_end' || data.event === 'done') { + console.log('[AI SSE] 收到完成信号:', data.event, 'conversationId:', cid); } } } catch (e) { @@ -369,12 +375,16 @@ export default { if (jsonStr) { const data = JSON.parse(jsonStr); if (data.event === 'message') { - const text = data.answer || ''; + var text = data.answer || data.text || ''; + if (!text && data.data) { + text = data.data.content || data.data.answer || ''; + } content += text; if (onChunk) onChunk(text); } - if (data.conversation_id) { - conversationId = data.conversation_id; + var cid = data.conversation_id || (data.data && data.data.conversation_id) || ''; + if (cid) { + conversationId = cid; } } } catch (e) { @@ -382,6 +392,7 @@ export default { } } + console.log('[AI SSE] 流完成, conversationId:', conversationId, 'content长度:', content.length); if (onComplete) { onComplete({ content, conversation_id: conversationId }); } diff --git a/pages_tool/ai-chat/ai-chat-message.vue b/pages_tool/ai-chat/ai-chat-message.vue index ef2ff5f..41dc5e9 100644 --- a/pages_tool/ai-chat/ai-chat-message.vue +++ b/pages_tool/ai-chat/ai-chat-message.vue @@ -570,6 +570,8 @@ export default { // 初始加载时滚底(仅一次) this.$nextTick(() => { this.scrollToBottom() + // 确保 H5 在组件挂载后也尝试加载历史(兜底) + this.loadHistoryIfNeeded() }) }, watch: { diff --git a/pages_tool/ai-chat/index.vue b/pages_tool/ai-chat/index.vue index e076f9c..fb5f4a0 100644 --- a/pages_tool/ai-chat/index.vue +++ b/pages_tool/ai-chat/index.vue @@ -148,17 +148,23 @@ export default { onShow() { console.log('[AI Chat Page] onShow 触发'); - // 监听键盘高度变化 - uni.onKeyboardHeightChange((res) => { - this.keyboardHeight = res.height; - if (this.$refs.chat && this.$refs.chat.updateKeyboardHeight) { - this.$refs.chat.updateKeyboardHeight(res.height); + // 监听键盘高度变化(H5 可能不支持,try-catch 保护) + try { + uni.onKeyboardHeightChange((res) => { + this.keyboardHeight = res.height; + if (this.$refs.chat && this.$refs.chat.updateKeyboardHeight) { + this.$refs.chat.updateKeyboardHeight(res.height); + } + }); + } catch (e) { + console.warn('onKeyboardHeightChange 不可用:', e.message); + } + // 每次页面显示时尝试加载历史记录(H5可能组件尚未创建,用 nextTick 确保 ref 就绪) + this.$nextTick(() => { + if (this.$refs.chat && this.$refs.chat.loadHistoryIfNeeded) { + this.$refs.chat.loadHistoryIfNeeded(); } }); - // 每次页面显示时尝试加载历史记录 - if (this.$refs.chat && this.$refs.chat.loadHistoryIfNeeded) { - this.$refs.chat.loadHistoryIfNeeded(); - } }, onHide() {