diff --git a/App.vue b/App.vue index 09f2e4e..e638aa6 100644 --- a/App.vue +++ b/App.vue @@ -266,7 +266,6 @@ imgUrl: this.$util.img(share_data.imgUrl) }, res => { - console.log(res); } ); } diff --git a/common/js/ai-service.js b/common/js/ai-service.js index f88c7a2..9bda135 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); @@ -16,12 +27,19 @@ try { } export default { + /** + * 获取uniacid + * @returns {number} uniacid + */ + getUniacid() { + return uni.getStorageSync('uniacid') || Config.uniacid; // 从缓存中获取uniacid,或使用默认uniacid + }, + /** * 发送普通消息(blocking 模式) */ async sendMessage(message, options = {}) { try { - const aiConfig = store.getters.globalAIKefuConfig; const params = { url: '/api/kefu/chat', data: { @@ -31,16 +49,14 @@ 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' } }; - console.log('Sending blocking request to:', params.url); - console.log('Request data:', params.data); - const response = await http.sendRequest({ ...params, async: false @@ -52,7 +68,14 @@ export default { } return result; } catch (error) { - console.error('Dify API请求失败:', error); + console.error('[AI Service] Dify API请求失败,详细错误:', error); + console.error('[AI Service] 错误堆栈:', error.stack); + // 打印更多信息帮助调试 + console.error('[AI Service] store 状态:', { + memberInfo: store.state.memberInfo, + uniacid: this.getUniacid(), + token: store.state.token + }); throw new Error('AI服务暂时不可用,请稍后重试'); } }, @@ -63,21 +86,23 @@ export default { async sendStreamMessage(message, onChunk, onComplete) { // #ifdef MP-WEIXIN return new Promise((resolve, reject) => { - const socketTask = wx.connectSocket({ - url: 'wss://dev.aigc-quickapp.com/ws/aikefu', - header: {} - }); + const match = Config.baseUrl.match(/^(?:https?:\/\/|wss?:\/\/)([^\/\s:]+)/); + const domain = match ? match[1] : ''; + const wssUrl = `wss://${domain}/ws/aikefu`; + const socketTask = wx.connectSocket({ + url: wssUrl, + header: {} + }); let content = ''; let conversationId = ''; let isAuthenticated = false; socketTask.onOpen(() => { - console.log('WebSocket 连接成功,开始认证...'); socketTask.send({ data: JSON.stringify({ action: 'auth', - uniacid: store.state.uniacid || '1', + uniacid: this.getUniacid(), token: store.state.token || 'test_token', user_id: store.state.memberInfo?.id || 'anonymous' }) @@ -87,18 +112,17 @@ export default { socketTask.onMessage((res) => { try { const data = JSON.parse(res.data); - console.log('收到 WebSocket 消息:', data); if (data.type === 'auth_success') { - console.log('认证成功,发送聊天消息...'); isAuthenticated = true; socketTask.send({ data: JSON.stringify({ action: 'chat', - uniacid: store.state.uniacid || '1', + uniacid: this.getUniacid(), query: message, user_id: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' + conversation_id: this.getConversationId() || '', + ...getDomainParams() }) }); } else if (data.type === 'auth_failed') { @@ -110,20 +134,20 @@ export default { } // 处理流式消息块 - else if (data.type === 'message' || data.event === 'message') { + else if (data.event === 'message') { const text = data.answer || data.content || data.text || ''; content += text; if (onChunk) onChunk(text); } // 处理流结束 - else if (data.event === 'message_end' || data.type === 'message_end') { + else if (data.event === 'message_end') { conversationId = data.conversation_id || ''; if (conversationId) { this.setConversationId(conversationId); } if (onComplete) { - onComplete({ content, conversation_id: conversationId }); + onComplete({ content, conversation_id: conversationId }); } resolve({ content, conversation_id: conversationId }); socketTask.close(); @@ -131,7 +155,6 @@ export default { // 可选:处理 done else if (data.type === 'done') { - console.log('对话完成:', data); } } catch (e) { @@ -147,7 +170,6 @@ export default { }); socketTask.onClose(() => { - console.log('WebSocket 连接已关闭'); }); const timeout = setTimeout(() => { @@ -175,12 +197,14 @@ export default { 'Accept': 'text/event-stream', }, body: JSON.stringify({ - uniacid: store.state.uniacid || '1', + uniacid: this.getUniacid(), + token: store.state.token || 'test_token', inputs: {}, query: message, response_mode: 'streaming', user_id: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' + conversation_id: this.getConversationId() || '', + ...getDomainParams() }) }); @@ -314,17 +338,19 @@ 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: this.getUniacid(), + token: store.state.token || 'test_token', + user_id: store.state.memberInfo?.id || 'anonymous', + member_id: store.state.memberInfo?.id || 'anonymous', + ...getDomainParams() + }, + header: { + 'Content-Type': 'application/json' + } + }; const response = await http.sendRequest({ ...params, async: false @@ -363,7 +389,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; @@ -382,13 +411,14 @@ export default { throw new Error('会话ID(conversation_id)是必填参数'); } const requestData = { - uniacid: params.uniacid || store.state.uniacid || '1', + uniacid: params.uniacid || this.getUniacid(), conversation_id: params.conversation_id, user_id: params.user_id || store.state.memberInfo?.id || 'anonymous', limit: params.limit || 20, offset: params.offset || 0, - member_id: params.member_id || store.state.memberInfo?.id || '', - token: params.token || '' + member_id: params.member_id || store.state.memberInfo?.id || 'anonymous', + token: params.token || '', + ...getDomainParams() }; const response = await http.sendRequest({ url: '/api/kefu/getHistory', @@ -427,13 +457,17 @@ export default { if (window.currentEventSource) { window.currentEventSource.close(); } - const params = new URLSearchParams({ - uniacid: store.state.uniacid || '1', - user_id: store.state.memberInfo?.id || '123456', + const paramsObj = { + uniacid: this.getUniacid(), + user_id: store.state.memberInfo?.id || 'anonymous', + member_id: store.state.memberInfo?.id || 'anonymous', + token: store.state.token || 'test_token', 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); diff --git a/common/js/config.js b/common/js/config.js index ec8d65b..06779c6 100644 --- a/common/js/config.js +++ b/common/js/config.js @@ -59,7 +59,6 @@ if (process?.env?.NODE_ENV === 'development') { // 如果本地配置文件不存在或加载失败,保持使用默认配置 // 只在开发模式下输出提示信息 // #ifdef WEB - console.log('本地配置文件 .local.config.js 不存在,使用默认配置'); // #endif } } diff --git a/common/js/customer-service.js b/common/js/customer-service.js index cd68ed5..288770f 100644 --- a/common/js/customer-service.js +++ b/common/js/customer-service.js @@ -2,6 +2,8 @@ * 客服统一处理服务 * 整合各种客服方式,提供统一的调用接口 */ +import IMAGE_CONFIG from './image-config.js'; + export class CustomerService { constructor(vueInstance, externalConfig = null) { this.vm = vueInstance; @@ -30,7 +32,6 @@ export class CustomerService { // 优先级:外部传入 > vuex store > 空对象 const servicerConfig = this.externalConfig || this.vm.$store.state.servicerConfig || {}; - console.log(`【实时客服配置】`, servicerConfig); let platformConfig = null; @@ -165,8 +166,6 @@ export class CustomerService { } const config = this.getPlatformConfig(); - console.log('【当前客服配置】', config); - console.log('【客服类型】', config.type); const { niushop = {}, sendMessageTitle = '', sendMessagePath = '', sendMessageImg = '' } = options; @@ -178,31 +177,24 @@ export class CustomerService { // 核心路由:根据 type 决定行为 switch (config.type) { case 'aikefu': - console.log('【跳转 AI 客服】目标路径: /pages_tool/ai-chat/index'); this.openDifyService(); break; case 'wxwork': - console.log('【跳转企业微信客服】'); this.openWxworkService(false, config, options); break; case 'third': - console.log('【跳转第三方客服】'); this.openThirdService(config); break; case 'miniprogram': - console.log('【跳转第三方小程序客服】'); this.openThirdService(config); break; case 'niushop': - console.log('【跳转牛商客服】'); this.openNiushopService(niushop); break; case 'weapp': - console.log('【跳转微信官方客服】'); this.openWeappService(config, options); break; case 'aliapp': - console.log('【跳转支付宝客服】'); this.openAliappService(config); break; default: @@ -223,7 +215,7 @@ export class CustomerService { wx.navigateToMiniProgram({ appId: 'wxeb490c6f9b154ef9', path: `pages/contacts/externalContactDetail?url=${encodeURIComponent(wxworkConfig.contact_url)}`, - success: () => console.log('跳转企业微信成功'), + success: () => {}, fail: (err) => { console.error('跳转企业微信失败:', err); this.fallbackToOriginalService(); @@ -260,24 +252,20 @@ export class CustomerService { } openThirdService(config) { - console.log('【第三方客服配置】', config); - console.log('【配置字段】', Object.keys(config)); // 支持多种可能的字段名 const miniAppId = config.mini_app_id || config.miniAppId || config.appid || config.appId || config.app_id; const miniAppPath = config.mini_app_path || config.miniAppPath || config.path || config.page_path || ''; - console.log('【解析后的小程序配置】AppID:', miniAppId, 'Path:', miniAppPath); + // 优先处理第三方微信小程序客服 if (miniAppId) { - console.log('【跳转第三方小程序】AppID:', miniAppId, 'Path:', miniAppPath); // #ifdef MP-WEIXIN wx.navigateToMiniProgram({ appId: miniAppId, path: miniAppPath, success: () => { - console.log('【跳转第三方小程序成功】'); }, fail: (err) => { console.error('【跳转第三方小程序失败】', err); @@ -293,7 +281,6 @@ export class CustomerService { // 处理第三方链接客服 if (config.third_url) { - console.log('【跳转第三方链接】', config.third_url); // #ifdef H5 window.location.href = config.third_url; // #endif @@ -324,7 +311,7 @@ export class CustomerService { // 此方法仅用于自定义跳转(如 useOfficial: false) if (config.useOfficial !== false) { // 不做任何事,应由