chore:可以看到之前历史对话了,但是获取时会晃动不完整

This commit is contained in:
2025-12-13 16:19:29 +08:00
parent a06ee95482
commit 80428e625f
2 changed files with 166 additions and 42 deletions

View File

@@ -1,7 +1,18 @@
import Config from './config.js'
import http from './http.js'
import store from '@/store/index.js'
let currentConversationId = null;
const CONVERSATION_KEY = 'ai_conversation_id';
// 初始化时从本地读取
try {
const saved = uni.getStorageSync(CONVERSATION_KEY);
if (saved) {
currentConversationId = saved;
}
} catch (e) {
console.warn('读取会话ID失败:', e);
}
export default {
/**
* 发送消息到Dify API
@@ -20,7 +31,8 @@ export default {
inputs: {},
query: message,
response_mode: options.stream ? 'streaming' : 'blocking',
user: store.state.memberInfo?.id || 'anonymous'
user: store.state.memberInfo?.id || 'anonymous',
conversation_id: this.getConversationId() || ''
},
header: {
'Content-Type': 'application/json'
@@ -33,12 +45,17 @@ export default {
...params,
async: false
})
return this.handleResponse(response, options)
} catch (error) {
console.error('Dify API请求失败:', error)
throw new Error('AI服务暂时不可用请稍后重试')
}
},
const result = this.handleResponse(response, options);
if (result.conversationId) {
this.setConversationId(result.conversationId);
}
return result;
} catch (error) {
console.error('Dify API请求失败:', error)
throw new Error('AI服务暂时不可用请稍后重试')
}
},
/**
* 流式消息处理
@@ -296,8 +313,9 @@ export default {
}
});
}
if (onComplete) onComplete(content)
return content
if (onComplete) onComplete({ content, conversation_id: conversationId });
return { content, conversation_id: conversationId };
} catch (error) {
console.error('HTTP流式请求失败:', error)
throw error
@@ -403,6 +421,30 @@ export default {
return false
}
},
getConversationId() {
return currentConversationId;
},
setConversationId(id) {
if (id && id !== currentConversationId) {
currentConversationId = id;
try {
uni.setStorageSync(CONVERSATION_KEY, id);
} catch (e) {
console.error('保存会话ID失败:', e);
}
}
},
clearConversationId() {
currentConversationId = null;
try {
uni.removeStorageSync(CONVERSATION_KEY);
} catch (e) {
console.error('清除会话ID失败:', e);
}
},
/**
* 获取会话历史
*/