From ca7d179d1b86fda0330b559daff7da435f1d15c7 Mon Sep 17 00:00:00 2001 From: ingkele <598792849@qq.com> Date: Fri, 9 Oct 2026 13:41:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0AI=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E5=BC=B9=E7=AA=97=E7=BB=84=E4=BB=B6=EF=BC=88?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E6=89=A3=E5=AD=90API=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/ai-chat/ai-chat.vue | 220 ++++++++++++++++++ components/right-bar/right-bar.vue | 7 +- .../router/service/client/ai/pub/chat.js | 53 +++++ 3 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 components/ai-chat/ai-chat.vue create mode 100644 uniCloud-tcb/cloudfunctions/router/service/client/ai/pub/chat.js diff --git a/components/ai-chat/ai-chat.vue b/components/ai-chat/ai-chat.vue new file mode 100644 index 0000000..058a656 --- /dev/null +++ b/components/ai-chat/ai-chat.vue @@ -0,0 +1,220 @@ + + + + + + \ No newline at end of file diff --git a/components/right-bar/right-bar.vue b/components/right-bar/right-bar.vue index 354353b..92f7af0 100644 --- a/components/right-bar/right-bar.vue +++ b/components/right-bar/right-bar.vue @@ -23,12 +23,12 @@
  • + @click="$refs.aiChat.toggle()" >
    - 在线客服 +

      AI

    + 客服
  • @@ -66,6 +66,7 @@ + diff --git a/uniCloud-tcb/cloudfunctions/router/service/client/ai/pub/chat.js b/uniCloud-tcb/cloudfunctions/router/service/client/ai/pub/chat.js new file mode 100644 index 0000000..c52f46f --- /dev/null +++ b/uniCloud-tcb/cloudfunctions/router/service/client/ai/pub/chat.js @@ -0,0 +1,53 @@ +module.exports = { + main: async (event) => { + let { data = {}, userInfo } = event; + + const COZE_TOKEN = 'sat_OiXlI4Da5Q99fxmul6pNfExmA6hR898UbxBDwrvjZ638APxRSlFnXbz6zyFedKNG'; + const BOT_ID = '7680345813062680618'; + + // 使用流式接口,直接从 SSE 事件中提取回答 + const res = await uniCloud.httpclient.request('https://api.coze.cn/v3/chat', { + method: 'POST', + dataType: 'text', + contentType: 'json', + timeout: 120000, + headers: { 'Authorization': 'Bearer ' + COZE_TOKEN }, + data: { + bot_id: BOT_ID, + user_id: (userInfo && userInfo.uid) || 'anonymous', + stream: true, + auto_save_history: true, + additional_messages: [{ + role: 'user', + content: data.message, + content_type: 'text' + }] + } + }); + + // 从 SSE 文本中提取 type=answer 的内容 + let answer = '暂时无法回复,请拨打 400-686-5559 咨询'; + let conversationId = ''; + const text = res.data || ''; + + const lines = text.split('\n'); + for (const line of lines) { + if (!line.startsWith('data:')) continue; + const d = line.slice(5).trim(); + if (d === '[DONE]' || d === '"[DONE]"') continue; + try { + const p = JSON.parse(d); + if (p.conversation_id) conversationId = p.conversation_id; + if (p.type === 'answer' && p.content) { + answer = p.content; + } + } catch (e) { /* 跳过非 JSON 行 */ } + } + + return { + code: 0, + msg: 'ok', + data: { answer, conversation_id: conversationId } + }; + } +};