From 46bdff3734577f65314066b8c5de3de60e5aaad4 Mon Sep 17 00:00:00 2001 From: dongzp <975303544@qq.com> Date: Wed, 10 Jun 2026 17:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=88=B7=E6=96=B0=E6=8C=89=E9=92=AE?= =?UTF-8?q?=EF=BC=8C=E5=88=87=E6=8D=A2tab=E8=87=AA=E5=8A=A8=E5=88=B7?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin.html | 81 ++++++++++++++++++++++++++++++++++++++----- src/admin-handlers.ts | 26 +++++++++----- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/admin.html b/admin.html index 82cf41b..0e90fb3 100644 --- a/admin.html +++ b/admin.html @@ -76,6 +76,7 @@ 路由配置(routes)
+ 刷新 新增路由
@@ -142,6 +143,7 @@ + 刷新 新增 Mock 文件 分组管理 @@ -193,6 +195,7 @@ 接口列表
+ 刷新 新增接口 批量导入
@@ -229,6 +232,9 @@ +
+ 刷新 +
@@ -559,6 +565,10 @@ data: function () { return { activeMainTab: "routes", + routesRefreshing: false, + mocksRefreshing: false, + apiRefreshing: false, + configRefreshing: false, apiList: [], apiPagination: { currentPage: 1, pageSize: 10 }, apiDialog: { @@ -660,6 +670,12 @@ }; }, watch: { + activeMainTab: function (tab) { + if (tab === "routes") { this.loadRoutes(); } + else if (tab === "mocks") { this.loadMockFiles(); this.loadMockGroups(); } + else if (tab === "api") { this.loadApiList(); } + else if (tab === "basic") { this.loadConfig(); } + }, routeSearch: function () { this.routePagination.currentPage = 1; }, mockFileSearch: function () { this.mockFilePagination.currentPage = 1; }, mockGroupFilter: function () { this.mockFilePagination.currentPage = 1; }, @@ -670,8 +686,64 @@ await this.loadMockFiles(); await this.loadMockGroups(); await this.loadConfig(); + await this.loadRoutes(); }, methods: { + loadRoutes: async function () { + try { + var resp = await fetch("/__routes"); + var data = await resp.json(); + if (!resp.ok || data.success === false) { + throw new Error(data.error || "加载路由失败"); + } + this.form.routes = this.toRouteArray( + data.routes || {}, + data.routeStatuses || {}, + data.routeEnabledMap || {}, + data.routeApiNameMap || {}, + ); + this.routePagination.currentPage = 1; + } catch (err) { + this.$message.error("加载路由失败: " + err.message); + } + }, + refreshRoutes: async function () { + this.routesRefreshing = true; + try { + await this.loadRoutes(); + this.$message.success("路由配置已刷新"); + } finally { + this.routesRefreshing = false; + } + }, + refreshMocks: async function () { + this.mocksRefreshing = true; + try { + await this.loadMockFiles(); + await this.loadMockGroups(); + this.$message.success("Mock 数据已刷新"); + } finally { + this.mocksRefreshing = false; + } + }, + refreshApiList: async function () { + this.apiRefreshing = true; + try { + await this.loadApiList(); + this.$message.success("接口列表已刷新"); + } finally { + this.apiRefreshing = false; + } + }, + refreshConfig: async function () { + this.configRefreshing = true; + try { + await this.loadConfig(); + this.$message.success("基础配置已刷新"); + } finally { + this.configRefreshing = false; + } + }, getPreviewText: function (content) { var text = String(content || "").replace(/\s+/g, " ").trim(); if (!text) return "(空文件)"; @@ -974,13 +1046,6 @@ var resp = await fetch("/__config"); var data = await resp.json(); this.form.config = Object.assign({}, this.form.config, data.config || {}); - this.form.routes = this.toRouteArray( - data.routes || {}, - data.routeStatuses || {}, - data.routeEnabledMap || {}, - data.routeApiNameMap || {}, - ); - this.routePagination.currentPage = 1; } catch (err) { this.$message.error("加载配置失败: " + err.message); } @@ -1051,7 +1116,7 @@ ); this.routeDialog.visible = false; await this.loadMockFiles(); - await this.loadConfig(); + await this.loadRoutes(); } catch (err) { this.$message.error("创建失败: " + err.message); } diff --git a/src/admin-handlers.ts b/src/admin-handlers.ts index c7e717e..01722c6 100644 --- a/src/admin-handlers.ts +++ b/src/admin-handlers.ts @@ -135,13 +135,8 @@ async function handleConfig( clientRes.end( JSON.stringify( { - routes: state.rawRoutes, - routeStatuses: state.rawRouteStatuses, - routeEnabledMap: state.routeEnabledMap, - routeApiNameMap: state.routeApiNameMap, config: state.config, timestamp: new Date().toISOString(), - totalRoutes: Object.keys(state.rawRoutes).length, }, null, 2, @@ -162,16 +157,31 @@ async function handleRoutes( clientReq: http.IncomingMessage, clientRes: http.ServerResponse, ): Promise { - if (clientReq.method !== "POST") { + if (clientReq.method !== "GET" && clientReq.method !== "POST") { clientRes.writeHead(405, { "Content-Type": "application/json", - Allow: "POST", + Allow: "GET, POST", }); clientRes.end( JSON.stringify({ success: false, error: "Method Not Allowed", - allow: ["POST"], + allow: ["GET, POST"], + }), + ); + return; + } + + if (clientReq.method === "GET") { + clientRes.writeHead(200, { "Content-Type": "application/json" }); + clientRes.end( + JSON.stringify({ + success: true, + routes: state.rawRoutes, + routeStatuses: state.rawRouteStatuses, + routeEnabledMap: state.routeEnabledMap, + routeApiNameMap: state.routeApiNameMap, + totalRoutes: Object.keys(state.rawRoutes).length, }), ); return;