chore:微信小程序真机智能客服正常
This commit is contained in:
@@ -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 路由
|
||||
|
||||
Reference in New Issue
Block a user