refactor(ai-service): 提取域名配置参数以减少重复代码

This commit is contained in:
2026-04-30 16:39:55 +08:00
parent 8d5c543a4f
commit 8e5561c2a7

View File

@@ -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);