chore:微信小程序真机智能客服正常

This commit is contained in:
2026-09-12 09:20:51 +08:00
parent d15724dd2f
commit 429487b1d1
3 changed files with 55 additions and 17 deletions

View File

@@ -367,8 +367,10 @@ export default {
try { try {
await ensureWebSocketConnected(); await ensureWebSocketConnected();
} catch (e) { } catch (e) {
if (onComplete) onComplete({ error: e.message }); // WebSocket 不可用(真机常见: socket 合法域名未配置 / 后端 WS 未部署)
throw e; // 自动降级为 HTTP 阻塞式请求, 复用与 H5 相同的后端接口
console.warn('[AI Service] MP WebSocket 不可用,降级为 HTTP blocking 请求:', e && e.message);
return this._fallbackToHttp(message, onChunk, onComplete);
} }
wsMessageContent = ''; wsMessageContent = '';
@@ -380,21 +382,52 @@ export default {
wsMessageResolve = resolve; wsMessageResolve = resolve;
wsMessageReject = reject; wsMessageReject = reject;
wsSocketTask.send({ try {
data: JSON.stringify({ wsSocketTask.send({
action: 'chat', data: JSON.stringify({
agent_id: getAgentId(), action: 'chat',
uniacid: this.getUniacid(), agent_id: getAgentId(),
query: message, uniacid: this.getUniacid(),
user_id: getUserId(), query: message,
conversation_id: this.getConversationId() || '', user_id: getUserId(),
...getDomainParams() conversation_id: this.getConversationId() || '',
}) ...getDomainParams()
}); })
});
} catch (sendErr) {
console.warn('[AI Service] MP WebSocket send 失败,降级为 HTTP blocking 请求:', sendErr && sendErr.message);
// 用降级结果决定该 promise 的最终状态, 避免同时弹出错误气泡
this._fallbackToHttp(message, onChunk, onComplete)
.then((res) => resolve(res))
.catch((err) => reject(err));
}
}); });
// #endif // #endif
}, },
/**
* [MP 降级] WebSocket 不可用时, 改用 HTTP 阻塞式请求完成一次完整回复
* 只要「request 合法域名」已配置即可工作, 不依赖 socket 合法域名
*/
async _fallbackToHttp(message, onChunk, onComplete) {
try {
const result = await this.sendMessage(message);
const content = result && result.content ? result.content : '';
if (content) {
if (onChunk) onChunk(content);
if (onComplete) onComplete({ content, conversation_id: result.conversationId });
return { content, conversation_id: result.conversationId };
}
const emptyErr = new Error(lang.lang('aiService.serviceUnavailable'));
if (onComplete) onComplete({ error: emptyErr.message });
throw emptyErr;
} catch (err) {
console.error('[AI Service] MP HTTP 降级请求也失败:', err);
if (onComplete) onComplete({ error: (err && err.message) || lang.lang('aiService.serviceUnavailable') });
throw err;
}
},
/** /**
* HTTP 流式请求(仅 H5 使用) * HTTP 流式请求(仅 H5 使用)
* PR-7: URL 按 agent_id 路由 * PR-7: URL 按 agent_id 路由

View File

@@ -2625,7 +2625,7 @@
}, },
"ai-chat": { "ai-chat": {
"index": { "index": {
"title": "AI智能客服", "title": "AI智能",
"cancel": "取消", "cancel": "取消",
"save": "保存", "save": "保存",
"send": "发送", "send": "发送",

View File

@@ -3046,12 +3046,12 @@ _handleStreamError(msg) {
} }
} }
/* 气泡: 浅灰背景 + 方形圆角, 内容在气泡内左对齐(中文阅读体验更好) */ /* 气泡: 浅灰背景 + 轻微圆角, 内容在气泡内左对齐(中文阅读体验更好) */
.message-bubble { .message-bubble {
background: #f5f5f5 !important; background: #f5f5f5 !important;
color: #1f1f1f !important; color: #1f1f1f !important;
border: none !important; border: none !important;
border-radius: 0 !important; border-radius: 16rpx !important;
box-shadow: none !important; box-shadow: none !important;
padding: 20rpx 30rpx !important; // 加大内边距,让气泡更"大" padding: 20rpx 30rpx !important; // 加大内边距,让气泡更"大"
display: inline-block; display: inline-block;
@@ -3076,7 +3076,7 @@ _handleStreamError(msg) {
/* #ifdef H5 */ /* #ifdef H5 */
/* H5 用户消息气泡加大内边距,文字大小保持不变 */ /* H5 用户消息气泡加大内边距,文字大小保持不变 */
.user-message .message-bubble { .user-message .message-bubble {
padding: 24rpx 40rpx !important; padding: 36rpx 60rpx !important;
} }
/* #endif */ /* #endif */
@@ -3682,6 +3682,11 @@ _handleStreamError(msg) {
} }
} }
} }
/* 用户消息气泡: 保持浅灰气泡 + 加大内边距(文字大小不变), 覆盖上面的 padding:0 */
.user-message .message-content .message-bubble {
padding: 36rpx 60rpx !important;
}
} }
} }
} }