From 5240505a2e5b9a4820805caa17a0ac215ff7ae9d Mon Sep 17 00:00:00 2001 From: "qichi.liang" Date: Wed, 20 May 2026 18:27:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=81=A2=E5=A4=8D=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=97=A0?= =?UTF-8?q?=E9=9C=80=E6=AF=8F=E6=AC=A1=E6=89=8B=E5=8A=A8=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit InitConfig 组件在应用挂载时从 localStorage 读取保存的 API Key/模式,自动调用 PUT /api/config 恢复后端设置 --- frontend/src/components/InitConfig.tsx | 25 +++++++++++++++++++++++++ frontend/src/main.tsx | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 frontend/src/components/InitConfig.tsx diff --git a/frontend/src/components/InitConfig.tsx b/frontend/src/components/InitConfig.tsx new file mode 100644 index 0000000..22766d7 --- /dev/null +++ b/frontend/src/components/InitConfig.tsx @@ -0,0 +1,25 @@ +import { useEffect } from 'react' + +export default function InitConfig() { + useEffect(() => { + const openaiKey = localStorage.getItem('openai-key') + const deepseekKey = localStorage.getItem('deepseek-key') + const openaiBaseUrl = localStorage.getItem('openai-base-url') + const deepseekBaseUrl = localStorage.getItem('deepseek-base-url') + const aiMock = localStorage.getItem('ai-mock') ?? 'true' + + const body: Record = { aiMock } + if (openaiKey) body.openaiKey = openaiKey + if (deepseekKey) body.deepseekKey = deepseekKey + if (openaiBaseUrl) body.openaiBaseUrl = openaiBaseUrl + if (deepseekBaseUrl) body.deepseekBaseUrl = deepseekBaseUrl + + fetch('/api/config', { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }).catch(() => {}) + }, []) + + return null +} diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index aa14b89..b6446c2 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -3,6 +3,7 @@ import { createRoot } from 'react-dom/client' import { RouterProvider } from 'react-router-dom' import { QueryClientProvider } from '@tanstack/react-query' import { ErrorBoundary } from './components/ErrorBoundary' +import InitConfig from './components/InitConfig' import { router } from './router' import { queryClient } from './lib/queryClient' import './index.css' @@ -11,6 +12,7 @@ createRoot(document.getElementById('root')!).render( +