diff --git a/common/js/ai-service.js b/common/js/ai-service.js index f099346..a0ebb86 100644 --- a/common/js/ai-service.js +++ b/common/js/ai-service.js @@ -5,6 +5,17 @@ import store from '@/store/index.js' let currentConversationId = null; const CONVERSATION_KEY = 'ai_conversation_id'; +/** + * 获取域名配置参数(避免重复代码) + */ +function getDomainParams() { + return { + imgDomain: Config.imgDomain, + h5Domain: Config.h5Domain, + baseUrl: Config.baseUrl + }; +} + // 初始化时从本地读取会话ID try { const saved = uni.getStorageSync(CONVERSATION_KEY); @@ -31,7 +42,8 @@ export default { query: message, response_mode: 'blocking', // 强制 blocking user: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' + conversation_id: this.getConversationId() || '', + ...getDomainParams() }, header: { 'Content-Type': 'application/json' @@ -102,7 +114,8 @@ export default { uniacid: store.state.uniacid || '1', query: message, user_id: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' + conversation_id: this.getConversationId() || '', + ...getDomainParams() }) }); } else if (data.type === 'auth_failed') { @@ -184,7 +197,8 @@ export default { query: message, response_mode: 'streaming', user_id: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' + conversation_id: this.getConversationId() || '', + ...getDomainParams() }) }); @@ -318,17 +332,18 @@ export default { * 生成新会话(如需) */ async generateConversationId() { - const params = { - url: '/api/kefu/createConversation', - data: { - uniacid: store.state.uniacid, - user_id: store.state.memberInfo?.id || 'anonymous', - member_id: store.state.memberInfo?.id || '', - }, - header: { - 'Content-Type': 'application/json' - } - }; + const params = { + url: '/api/kefu/createConversation', + data: { + uniacid: store.state.uniacid, + user_id: store.state.memberInfo?.id || 'anonymous', + member_id: store.state.memberInfo?.id || '', + ...getDomainParams() + }, + header: { + 'Content-Type': 'application/json' + } + }; const response = await http.sendRequest({ ...params, async: false @@ -367,7 +382,10 @@ export default { try { const response = await http.sendRequest({ url: '/api/kefu/clear-conversation', - data: { conversation_id: conversationId }, + data: { + conversation_id: conversationId, + ...getDomainParams() + }, async: false }); return response.success; @@ -392,7 +410,8 @@ export default { limit: params.limit || 20, offset: params.offset || 0, member_id: params.member_id || store.state.memberInfo?.id || '', - token: params.token || '' + token: params.token || '', + ...getDomainParams() }; const response = await http.sendRequest({ url: '/api/kefu/getHistory', @@ -431,13 +450,15 @@ export default { if (window.currentEventSource) { window.currentEventSource.close(); } - const params = new URLSearchParams({ + const paramsObj = { uniacid: store.state.uniacid || '1', user_id: store.state.memberInfo?.id || '123456', query: message, conversation_id: conversationId || '', - stream: 'true' - }); + stream: 'true', + ...getDomainParams() + }; + const params = new URLSearchParams(paramsObj); const url = `${Config.baseUrl}/api/kefu/chat?${params.toString()}`; try { const eventSource = new EventSource(url);