From 3018f2ff6677e1611bcda513187a824d6866a7c2 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Wed, 14 Jan 2026 20:02:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=85=8D=E7=BD=AE=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/admin/page.tsx | 37 ++++++++++++++++++---- src/app/api/admin/reload/route.ts | 51 +++++++++++++++++++++++++++++++ src/lib/config.ts | 5 +++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/app/api/admin/reload/route.ts diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index 4b3e5f0..790938d 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -10438,6 +10438,23 @@ function AdminPageClient() { }); }; + // 新增: 重载配置处理函数 + const handleReloadConfig = async () => { + await withLoading('reloadConfig', async () => { + try { + const response = await fetch(`/api/admin/reload`); + if (!response.ok) { + throw new Error(`重载失败: ${response.status}`); + } + showSuccess('重载成功,配置缓存已清除!', showAlert); + await fetchConfig(); + } catch (err) { + showError(err instanceof Error ? err.message : '重载失败', showAlert); + throw err; + } + }); + }; + if (loading) { return ( @@ -10475,12 +10492,20 @@ function AdminPageClient() { 管理员设置 {config && role === 'owner' && ( - + <> + + + )} diff --git a/src/app/api/admin/reload/route.ts b/src/app/api/admin/reload/route.ts new file mode 100644 index 0000000..8f19da3 --- /dev/null +++ b/src/app/api/admin/reload/route.ts @@ -0,0 +1,51 @@ +/* eslint-disable no-console */ + +import { NextRequest, NextResponse } from 'next/server'; + +import { getAuthInfoFromCookie } from '@/lib/auth'; +import { clearConfigCache } from '@/lib/config'; + +export const runtime = 'nodejs'; + +export async function GET(request: NextRequest) { + const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage'; + if (storageType === 'localstorage') { + return NextResponse.json( + { + error: '不支持本地存储进行管理员配置', + }, + { status: 400 } + ); + } + + const authInfo = getAuthInfoFromCookie(request); + if (!authInfo || !authInfo.username) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); + } + const username = authInfo.username; + + if (username !== process.env.USERNAME) { + return NextResponse.json({ error: '仅支持站长重载配置' }, { status: 401 }); + } + + try { + await clearConfigCache(); + + return NextResponse.json( + { ok: true }, + { + headers: { + 'Cache-Control': 'no-store', + }, + } + ); + } catch (error) { + return NextResponse.json( + { + error: '重载配置失败', + details: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/lib/config.ts b/src/lib/config.ts index 0cb0eb1..a6a99d6 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -610,4 +610,9 @@ export async function getAvailableApiSites(user?: string): Promise { export async function setCachedConfig(config: AdminConfig) { cachedConfig = config; +} + +export async function clearConfigCache() { + cachedConfig = null as any; + configInitPromise = null; } \ No newline at end of file