From 386cb79e1eb6466dcaf7fadc53c67ee59b4901bb Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 6 Oct 2025 01:13:37 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=90=9BFix:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=8D=E5=8F=8A=E6=97=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/ChatPanel.vue | 51 ++++++++++++++++++++----- frontend/src/components/ConfigPanel.vue | 8 +++- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/frontend/src/components/ChatPanel.vue b/frontend/src/components/ChatPanel.vue index ffb60c8..eadc70d 100644 --- a/frontend/src/components/ChatPanel.vue +++ b/frontend/src/components/ChatPanel.vue @@ -99,7 +99,6 @@ const getPlatformName = (platformId) => { } // 发送消息 -// TODO 需要将用户配置(端点、密钥)和用户消息发送给后端统一处理 const sendMessage = async () => { if (!canSend.value) return @@ -126,12 +125,8 @@ const sendMessage = async () => { throw new Error('未找到厂商配置') } - // TODO 这里无需解密,应该交由后端解密 - // 解密密钥 - const apiKey = decrypt(config.apiKey) - - // 调用API - const result = await callAIPlatform(selectedPlatform.value, apiKey, config.endpoint, userInput.value) + // 将配置信息和用户消息发送给后端处理 + const result = await callAIPlatform(selectedPlatform.value, config.apiKey, config.endpoint, userInput.value) // 显示响应 responseMessage.value = result @@ -144,14 +139,50 @@ const sendMessage = async () => { } // 调用AI平台API -// TODO 此处由后端统一处理 +// TODO 这里需要调用实际后端API接口 const callAIPlatform = async (platformId, apiKey, endpoint, message) => { - // TODO 待完善 + try { + // 发送请求到后端API + const response = await fetch('/api/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + platformId, + apiKey, + endpoint, + message + }) + }) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const data = await response.json() + return data.response + } catch (error) { + console.error('调用AI平台API失败:', error) + throw new Error('调用AI平台API失败: ' + error.message) + } } // 初始化 onMounted(() => { loadConfiguredPlatforms() + + // 监听配置保存事件,以便刷新数据 + const handleConfigSaved = () => { + loadConfiguredPlatforms() + } + + window.addEventListener('config-saved', handleConfigSaved) + + // 组件卸载时移除事件监听器 + onUnmounted(() => { + window.removeEventListener('config-saved', handleConfigSaved) + }) }) @@ -248,4 +279,4 @@ onMounted(() => { .info-message p { margin: 0; } - \ No newline at end of file + diff --git a/frontend/src/components/ConfigPanel.vue b/frontend/src/components/ConfigPanel.vue index 173ae26..b22b6c2 100644 --- a/frontend/src/components/ConfigPanel.vue +++ b/frontend/src/components/ConfigPanel.vue @@ -183,6 +183,12 @@ const saveConfig = () => { resetForm() showValidation('配置保存成功', 'success') + + // TODO: 保存后应该使得 ChatPanel 组件刷新,使得能够选择厂商 + // 触发全局事件,通知 ChatPanel 刷新配置 + if (typeof window !== 'undefined') { + window.dispatchEvent(new CustomEvent('config-saved', { detail: { platformId: selectedPlatform.value } })) + } } catch (error) { console.error('保存配置失败:', error) showValidation('保存配置失败,请重试', 'error') @@ -362,4 +368,4 @@ onMounted(() => { .remove-btn:hover { background-color: #c82333; } - \ No newline at end of file +