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 {
await ensureWebSocketConnected();
} catch (e) {
if (onComplete) onComplete({ error: e.message });
throw e;
// WebSocket 不可用(真机常见: socket 合法域名未配置 / 后端 WS 未部署)
// 自动降级为 HTTP 阻塞式请求, 复用与 H5 相同的后端接口
console.warn('[AI Service] MP WebSocket 不可用,降级为 HTTP blocking 请求:', e && e.message);
return this._fallbackToHttp(message, onChunk, onComplete);
}
wsMessageContent = '';
@@ -380,21 +382,52 @@ export default {
wsMessageResolve = resolve;
wsMessageReject = reject;
wsSocketTask.send({
data: JSON.stringify({
action: 'chat',
agent_id: getAgentId(),
uniacid: this.getUniacid(),
query: message,
user_id: getUserId(),
conversation_id: this.getConversationId() || '',
...getDomainParams()
})
});
try {
wsSocketTask.send({
data: JSON.stringify({
action: 'chat',
agent_id: getAgentId(),
uniacid: this.getUniacid(),
query: message,
user_id: getUserId(),
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
},
/**
* [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 使用)
* PR-7: URL 按 agent_id 路由

View File

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

View File

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