From e541a529d4d114c13d330511c8ac19cd78f0bbbb Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Mon, 10 Aug 2026 18:43:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20API=20=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E5=9C=B0=E5=9D=80=E9=85=8D=E7=BD=AE=E5=92=8C=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=B5=8B=E8=AF=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dashboard/src/lib/api.ts | 41 +++++++++++++ dashboard/src/pages/Home.tsx | 9 ++- dashboard/src/pages/Settings.tsx | 100 +++++++++++++++++++++++++++++-- 3 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 dashboard/src/lib/api.ts diff --git a/dashboard/src/lib/api.ts b/dashboard/src/lib/api.ts new file mode 100644 index 0000000..bce2d1b --- /dev/null +++ b/dashboard/src/lib/api.ts @@ -0,0 +1,41 @@ +const STORAGE_KEY = 'workstation.apiBaseUrl' + +function envApiBaseUrl(): string { + return import.meta.env.VITE_API_BASE_URL ?? '' +} + +export function normalizeApiBaseUrl(url: string): string { + return url.trim().replace(/\/+$/, '') +} + +export function getApiBaseUrl(): string { + try { + const saved = localStorage.getItem(STORAGE_KEY) + if (saved !== null && saved !== '') return normalizeApiBaseUrl(saved) + } catch { + // localStorage 不可用时退回环境变量 + } + return envApiBaseUrl() +} + +export function setApiBaseUrl(url: string): void { + const normalized = normalizeApiBaseUrl(url) + try { + if (normalized === '') { + localStorage.removeItem(STORAGE_KEY) + } else { + localStorage.setItem(STORAGE_KEY, normalized) + } + } catch { + // 忽略写入失败 + } +} + +export function resolveApiUrl(path: string): string { + const base = normalizeApiBaseUrl(getApiBaseUrl()) + return `${base}${path}` +} + +export function apiFetch(path: string, init?: RequestInit): Promise { + return fetch(resolveApiUrl(path), init) +} diff --git a/dashboard/src/pages/Home.tsx b/dashboard/src/pages/Home.tsx index b9b0e56..4ed3402 100644 --- a/dashboard/src/pages/Home.tsx +++ b/dashboard/src/pages/Home.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' +import { apiFetch } from '../lib/api' interface CpuStats { model: string @@ -25,8 +26,6 @@ interface SystemStats { os: OsStats } -const API_BASE = import.meta.env.VITE_API_BASE_URL ?? '' - function formatUptime(seconds: number) { const d = Math.floor(seconds / 86400) const h = Math.floor((seconds % 86400) / 3600) @@ -81,7 +80,7 @@ export default function Home() { const [error, setError] = useState(false) useEffect(() => { - fetch(`${API_BASE}/api/system/stats`) + apiFetch('/api/system/stats') .then((res) => { if (!res.ok) throw new Error('Failed to fetch stats') return res.json() @@ -111,7 +110,7 @@ export default function Home() {

系统状态

{error - ? '无法获取系统信息,请确认后端服务已启动' + ? '无法获取系统信息,请确认后端服务已启动,或在「设置」中配置服务器 API 地址' : stats ? '实时运行状态一览' : '正在获取系统信息…'} @@ -193,7 +192,7 @@ export default function Home() { ) : (

- {error ? '无法获取系统信息,请确认后端服务已启动' : '正在获取系统信息…'} + {error ? '无法获取系统信息,请确认后端服务已启动,或在「设置」中配置服务器 API 地址' : '正在获取系统信息…'}

)} diff --git a/dashboard/src/pages/Settings.tsx b/dashboard/src/pages/Settings.tsx index 629488f..73e0cd0 100644 --- a/dashboard/src/pages/Settings.tsx +++ b/dashboard/src/pages/Settings.tsx @@ -1,10 +1,100 @@ +import { useState } from 'react' +import { getApiBaseUrl, normalizeApiBaseUrl, resolveApiUrl, setApiBaseUrl } from '../lib/api' + +type TestStatus = 'idle' | 'testing' | 'ok' | 'fail' + export default function Settings() { + const [value, setValue] = useState(getApiBaseUrl()) + const [saved, setSaved] = useState(getApiBaseUrl()) + const [status, setStatus] = useState('idle') + const [message, setMessage] = useState('') + + const dirty = normalizeApiBaseUrl(value) !== saved + + function handleSave() { + setApiBaseUrl(value) + setSaved(getApiBaseUrl()) + setStatus('idle') + setMessage('') + } + + async function handleTest() { + setStatus('testing') + setMessage('') + const base = normalizeApiBaseUrl(value) + const oldBase = getApiBaseUrl() + if (base !== oldBase) setApiBaseUrl(base) + try { + const res = await fetch(resolveApiUrl('/api/system/stats')) + if (!res.ok) throw new Error(`HTTP ${res.status}`) + const data = await res.json() + const label = (data.os?.name as string | undefined) ?? '后端' + setStatus('ok') + setMessage(`${label} 连接正常`) + } catch { + setStatus('fail') + setMessage('连接失败,请检查地址是否正确以及后端服务是否已启动') + } finally { + if (base !== oldBase) setApiBaseUrl(oldBase) + } + } + return ( -
-
🚧
-

设置

-

系统偏好与账号配置

-

页面建设中,敬请期待

+
+

设置

+

系统偏好与账号配置

+ +
+

服务器 API 地址

+

+ 配置前端连接后端服务的 API 基础地址,留空则使用环境变量或本地代理 +

+ + + +

+ 当前生效地址:{saved === '' ? '默认(本地代理)' : saved} +

+ + {message && ( +

+ {message} +

+ )} + +
+ + +
+
) }