diff --git a/common/js/ai-service.js b/common/js/ai-service.js index 7e3c6f2..3fe8d6f 100644 --- a/common/js/ai-service.js +++ b/common/js/ai-service.js @@ -11,14 +11,16 @@ export default { async sendMessage(message, options = {}) { try { // 获取AI配置 - const aiConfig = store.getters.globalAIAgentConfig + const aiConfig = store.getters.globalAIKefuConfig + + // const new_conversationId = await this.generateConversationId() // 构建Dify API请求参数 const params = { - url: '/api/ai/chat', // 后端代理接口 + url: '/api/kefu/chat', // 后端代理接口 data: { message: message, - conversation_id: options.conversationId || this.generateConversationId(), + // conversation_id: options.conversationId ?? new_conversationId, user_id: store.state.memberInfo?.id || 'anonymous', stream: options.stream || false, // 是否流式响应 // Dify API参数 @@ -32,15 +34,6 @@ export default { } } - // 如果有Dify配置,添加API密钥 - if (aiConfig?.difyApiKey) { - params.header['Authorization'] = `Bearer ${aiConfig.difyApiKey}` - } - - if (aiConfig?.difyBaseUrl) { - params.header['X-Dify-Url'] = aiConfig.difyBaseUrl - } - // 发送请求 const response = await http.sendRequest({ ...params, @@ -63,20 +56,8 @@ export default { */ async sendStreamMessage(message, onChunk, onComplete) { try { - const aiConfig = store.getters.globalAIAgentConfig - - // 检查配置 - if (!aiConfig?.difyBaseUrl || !aiConfig?.difyApiKey) { - throw new Error('未配置Dify服务') - } - - // 创建WebSocket连接或使用Server-Sent Events - if (aiConfig?.difyWsUrl) { - return this.connectWebSocket(message, onChunk, onComplete) - } else { // 使用HTTP流式请求 return this.sendHttpStream(message, onChunk, onComplete) - } } catch (error) { console.error('流式消息发送失败:', error) @@ -89,7 +70,7 @@ export default { */ connectWebSocket(message, onChunk, onComplete) { return new Promise((resolve, reject) => { - const aiConfig = store.getters.globalAIAgentConfig + const aiConfig = store.getters.globalAIKefuConfig const wsUrl = aiConfig.difyWsUrl if (!wsUrl) { @@ -181,10 +162,10 @@ export default { * HTTP流式请求 */ async sendHttpStream(message, onChunk, onComplete) { - const aiConfig = store.getters.globalAIAgentConfig + const aiConfig = store.getters.globalAIKefuConfig const params = { - url: '/api/ai/chat-stream', + url: '/api/kefu/chat-stream', data: { message: message, conversation_id: this.generateConversationId(), @@ -203,11 +184,10 @@ export default { // 使用fetch API进行流式请求(H5环境) // #ifdef H5 try { - const response = await fetch(`${aiConfig.difyBaseUrl}/chat-messages`, { + const response = await fetch(`/api/kefu/chat-messages`, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${aiConfig.difyApiKey}` }, body: JSON.stringify({ inputs: {}, @@ -286,7 +266,7 @@ export default { if (response.code === 0 || response.success) { return { success: true, - content: response.data?.answer || response.data?.content || response.data, + content: response.data?.answer || response.data?.content || response.data?.reply|| response.data, conversationId: response.data?.conversation_id, messageId: response.data?.message_id } @@ -298,8 +278,27 @@ export default { /** * 生成会话ID */ - generateConversationId() { - return 'conv_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9) + async generateConversationId() { + // 构建Dify API请求参数 + 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 response = await http.sendRequest({ + ...params, + async: false // 使用Promise方式 + }) + + return this.handleResponse(response, options) }, /** @@ -307,24 +306,17 @@ export default { */ async getServiceStatus() { try { - const aiConfig = store.getters.globalAIAgentConfig - - if (!aiConfig?.difyBaseUrl || !aiConfig?.difyApiKey) { - return { - available: false, - reason: '未配置Dify服务' - } - } - // 简单的健康检查 const response = await http.sendRequest({ - url: '/api/ai/health', + url: '/api/kefu/health', async: false }) + + const available = response?.data?.status === 'healthy'; return { - available: response.success, - reason: response.success ? '服务正常' : '服务异常' + available, + reason: available ? '服务正常' : '服务异常' } } catch (error) { @@ -341,7 +333,7 @@ export default { async clearConversation(conversationId) { try { const response = await http.sendRequest({ - url: '/api/ai/clear-conversation', + url: '/api/kefu/clear-conversation', data: { conversation_id: conversationId }, async: false }) diff --git a/common/js/config.js b/common/js/config.js index dfa98ec..6b27eff 100644 --- a/common/js/config.js +++ b/common/js/config.js @@ -14,18 +14,18 @@ try { // 调试版本,配置说明 const devCfg = { // 商户ID - uniacid: 460, //825 + uniacid: 1, //825 //api请求地址 - baseUrl: 'https://xcx30.5g-quickapp.com/', + baseUrl: 'https://dev.aigc-quickapp.com/', // baseUrl: 'http://localhost:8010/', // 图片域名 - imgDomain: 'https://xcx30.5g-quickapp.com/', + imgDomain: 'https://dev.aigc-quickapp.com/', //imgDomain: 'http://localhost:8010/', // H5端域名 - h5Domain: 'https://xcx30.5g-quickapp.com/', + h5Domain: 'https://dev.aigc-quickapp.com/', // h5Domain: 'http://localhost:8010/', // // api请求地址 diff --git a/components/ai-chat-message/ai-chat-message.vue b/components/ai-chat-message/ai-chat-message.vue index 1ea4cde..ec75728 100644 --- a/components/ai-chat-message/ai-chat-message.vue +++ b/components/ai-chat-message/ai-chat-message.vue @@ -313,7 +313,7 @@ export default { // 是否启用流式响应 enableStreaming: { type: Boolean, - default: true + default: false }, // 流式响应速度(字符/秒) streamSpeed: { diff --git a/components/hover-nav/hover-nav.vue b/components/hover-nav/hover-nav.vue index 64fbeac..f4b4f4a 100644 --- a/components/hover-nav/hover-nav.vue +++ b/components/hover-nav/hover-nav.vue @@ -5,7 +5,7 @@ - 🤖 + {{ unreadCount > 99 ? '99+' : unreadCount }} @@ -73,17 +73,17 @@ }, computed: { ...mapGetters([ - 'globalAIAgentConfig', + 'globalAIKefuConfig', 'aiUnreadCount' ]), aiAgentimg() { - return this.globalAIAgentConfig?.icon || this.$util.getDefaultImage().aiAgent || '' // AI智能助手的头像 + return this.globalAIKefuConfig?.icon || this.$util.getDefaultImage().aiAgent || '' // AI智能助手的头像 }, unreadCount() { return this.aiUnreadCount }, enableAIChat() { - return this.globalAIAgentConfig?.enable || true // 是否开启AI智能助手 + return this.globalAIKefuConfig?.enable || true // 是否开启AI智能助手 }, }, methods: { diff --git a/pages_tool/ai-chat/index.vue b/pages_tool/ai-chat/index.vue index 65dfbeb..39b3658 100644 --- a/pages_tool/ai-chat/index.vue +++ b/pages_tool/ai-chat/index.vue @@ -89,15 +89,15 @@ export default { computed: { ...mapGetters([ - 'globalAIAgentConfig' + 'globalAIKefuConfig' ]), /// ---- 关于AI客服的配置,支持远程配置 ---- userAvatar() { - return this.globalAIAgentConfig?.userAvatar || '/static/images/user-avatar.png' + return this.globalAIKefuConfig?.userAvatar || '/static/images/user-avatar.png' }, aiAvatar() { - return this.globalAIAgentConfig?.aiAvatar || '/static/images/ai-avatar.png' + return this.globalAIKefuConfig?.aiAvatar || '/static/images/ai-avatar.png' }, /// ---- others ---- containerHeight() { diff --git a/store/index.js b/store/index.js index ec95fe8..046671d 100644 --- a/store/index.js +++ b/store/index.js @@ -60,7 +60,7 @@ const store = new Vuex.Store({ cartChange: 0, bottomNavHidden: false, // 底部导航是否隐藏,true:隐藏,false:显示 aiUnreadCount: 10, // AI未读消息数量 - globalAIAgentConfig: null, // AI客服配置 + globalAIKefuConfig: null, // AI客服配置 globalStoreConfig: null, // 门店配置 globalStoreInfo: null, // 门店信息 defaultStoreInfo: null, // 默认门店 @@ -140,9 +140,9 @@ const store = new Vuex.Store({ setBottomNavHidden(state, value) { state.bottomNavHidden = value; }, - setGlobalAIAgentConfig(state, value) { - state.globalAIAgentConfig = value; - uni.setStorageSync('globalAIAgentConfig', value); // 初始化数据调用 + setglobalAIKefuConfig(state, value) { + state.globalAIKefuConfig = value; + uni.setStorageSync('globalAIKefuConfig', value); // 初始化数据调用 }, setGlobalStoreConfig(state, value) { state.globalStoreConfig = value; @@ -208,7 +208,7 @@ const store = new Vuex.Store({ }, getters: { // AI智能助手配置 - globalAIAgentConfig: state => state.globalAIAgentConfig, + globalAIKefuConfig: state => state.globalAIKefuConfig, // AI未读消息数量 aiUnreadCount: state => state.aiUnreadCount, }, @@ -237,7 +237,7 @@ const store = new Vuex.Store({ this.commit('setMapConfig', data.map_config); - this.commit('setGlobalAIAgentConfig', data.ai_agent_config); + this.commit('setglobalAIKefuConfig', data.ai_agent_config); this.commit('setGlobalStoreConfig', data.store_config);