From ae8786f297150daf87b8843000eb18753e5b8e05 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 20 Jun 2025 13:34:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0api=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/dashboard/FeatureCard.jsx | 2 +- .../layout/github/GithubTransitionCard.jsx | 6 ++- src/frontend/src/libs/system/Notification.jsx | 2 +- src/frontend/src/pages/Dashboard.jsx | 49 +++++++++++------- src/frontend/src/services/api.js | 51 +++++++++++++++++++ 5 files changed, 87 insertions(+), 23 deletions(-) diff --git a/src/frontend/src/components/pages/dashboard/FeatureCard.jsx b/src/frontend/src/components/pages/dashboard/FeatureCard.jsx index 5da6c03..20edad6 100644 --- a/src/frontend/src/components/pages/dashboard/FeatureCard.jsx +++ b/src/frontend/src/components/pages/dashboard/FeatureCard.jsx @@ -28,7 +28,7 @@ const FeatureCard = ({ title, description, buttonText, to, disabled }) => ( {description} @@ -132,25 +143,25 @@ const Dashboard = () => { title={'网络扫描'} description={'快速扫描指定子网,发现可用设备,展示设备 IP/MAC 和开放端口信息'} buttonText={'立即扫描'} - to={'/scan'} + to={'/dashboard/scan'} /> diff --git a/src/frontend/src/services/api.js b/src/frontend/src/services/api.js index e69de29..3b051fe 100644 --- a/src/frontend/src/services/api.js +++ b/src/frontend/src/services/api.js @@ -0,0 +1,51 @@ +/** + * 获取config + * @returns {any|null} + */ +export const getConfig = () => { + const cfg = localStorage.getItem('connection-config'); + return cfg ? JSON.parse(cfg) : null; +}; + +/** + * 获取后端url + * @returns {string} + */ +export const getBaseUrl = () => { + const cfg = getConfig(); + return cfg?.backendUrl || ''; +}; + +/** + * fetchUrl + * @param path 路径 + * @param options 选项 + * @returns {Promise} + */ +const fetchWithBase = async (path, options = {}) => { + const base = getBaseUrl(); + const res = await fetch(`${base}${path}`, options); + return res.json(); +}; + +/** + * api模块 + * @type {{test: (function(): Promise<*>), scan: (function(*): Promise<*>), listDevices: (function(): Promise<*>), parseCommand: (function(*): Promise<*>), applyConfig: (function(*, *): Promise<*>)}} + */ +export const api = { + test: () => fetchWithBase('/api/test'), + scan: (subnet) => fetchWithBase(`/api/scan_network?subnet=${encodeURIComponent(subnet)}`), + listDevices: () => fetchWithBase('/api/list_devices'), + parseCommand: (text) => + fetchWithBase('/api/parse_command', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ command: text }), + }), + applyConfig: (switch_ip, config) => + fetchWithBase('/api/apply_config', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ switch_ip, config }), + }), +};