From 668146f414a841e50f05b1692daddab1dfb1de16 Mon Sep 17 00:00:00 2001 From: djteang <935037887@qq.com> Date: Wed, 24 Sep 2025 22:12:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fixed:=E8=87=AA=E5=AE=9A=E4=B9=89=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E5=BA=94=E7=94=A8=E6=89=80=E6=9C=89=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 + src/app/api/admin/theme/route.ts | 59 +++---- src/app/api/theme/route.ts | 46 ------ src/app/layout.tsx | 100 +++--------- src/components/GlobalThemeLoader.tsx | 138 ++++++++-------- src/components/ThemeManager.tsx | 59 ++++--- src/lib/config.ts | 13 ++ src/lib/upstash.db.ts | 228 +++++++++++++++++++-------- src/middleware.ts | 2 +- 9 files changed, 333 insertions(+), 315 deletions(-) delete mode 100644 src/app/api/theme/route.ts diff --git a/README.md b/README.md index 91388b3..6fee402 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ services: restart: on-failure ports: - '3000:3000' + - '3001:3001' environment: - USERNAME=admin - PASSWORD=orange @@ -111,6 +112,7 @@ services: restart: on-failure ports: - '3000:3000' + - '3001:3001' environment: - USERNAME=admin - PASSWORD=orange @@ -147,6 +149,7 @@ services: restart: on-failure ports: - '3000:3000' + - '3001:3001' environment: - USERNAME=admin - PASSWORD=orange diff --git a/src/app/api/admin/theme/route.ts b/src/app/api/admin/theme/route.ts index 5130bfa..28b3a6c 100644 --- a/src/app/api/admin/theme/route.ts +++ b/src/app/api/admin/theme/route.ts @@ -3,6 +3,7 @@ import { getAuthInfoFromCookie } from '@/lib/auth'; import { db } from '@/lib/db'; import { AdminConfig } from '@/lib/admin.types'; import { headers, cookies } from 'next/headers'; +import { getConfig, setCachedConfig, clearCachedConfig } from '@/lib/config'; export async function GET() { try { @@ -22,12 +23,8 @@ export async function GET() { return NextResponse.json({ error: '认证信息无效' }, { status: 401 }); } - const config = await db.getAdminConfig(); - const themeConfig = config?.ThemeConfig || { - defaultTheme: 'default' as const, - customCSS: '', - allowUserCustomization: true, - }; + const config = await getConfig(); + const themeConfig = config.ThemeConfig; return NextResponse.json({ success: true, @@ -75,40 +72,7 @@ export async function POST(request: Request) { } // 获取当前配置 - const currentConfig = await db.getAdminConfig(); - - // 如果没有配置,创建一个基础配置 - let baseConfig: AdminConfig; - if (!currentConfig) { - baseConfig = { - ConfigSubscribtion: { - URL: "", - AutoUpdate: false, - LastCheck: "", - }, - ConfigFile: "", - SiteConfig: { - SiteName: "OrangeTV", - Announcement: "", - SearchDownstreamMaxPage: 10, - SiteInterfaceCacheTime: 30, - DoubanProxyType: "direct", - DoubanProxy: "", - DoubanImageProxyType: "direct", - DoubanImageProxy: "", - DisableYellowFilter: false, - FluidSearch: true, - RequireDeviceCode: false, - }, - UserConfig: { - Users: [], - }, - SourceConfig: [], - CustomCategories: [], - }; - } else { - baseConfig = currentConfig; - } + const baseConfig = await getConfig(); // 更新主题配置 const updatedConfig: AdminConfig = { @@ -120,10 +84,23 @@ export async function POST(request: Request) { }, }; - console.log('保存主题配置:', updatedConfig.ThemeConfig); + console.log('=== 保存主题配置 ==='); + console.log('请求参数:', { defaultTheme, customCSS, allowUserCustomization }); + console.log('当前存储类型:', process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage'); + console.log('待保存配置:', updatedConfig.ThemeConfig); + console.log('完整配置对象:', JSON.stringify(updatedConfig, null, 2)); + await db.saveAdminConfig(updatedConfig); console.log('主题配置保存成功'); + // 直接更新缓存,确保缓存与数据库同步 + await setCachedConfig(updatedConfig); + console.log('已更新配置缓存'); + + // 立即验证缓存中的配置 + const cachedConfig = await getConfig(); + console.log('保存后验证缓存中的配置:', cachedConfig.ThemeConfig); + return NextResponse.json({ success: true, message: '主题配置已更新', diff --git a/src/app/api/theme/route.ts b/src/app/api/theme/route.ts deleted file mode 100644 index ff47fe5..0000000 --- a/src/app/api/theme/route.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { NextResponse } from 'next/server'; -import { db } from '@/lib/db'; - -export async function GET() { - try { - console.log('API /theme: 开始获取主题配置...'); - const config = await db.getAdminConfig(); - - console.log('API /theme: 获取到的管理员配置:', { - hasConfig: !!config, - hasThemeConfig: !!(config?.ThemeConfig), - themeConfig: config?.ThemeConfig - }); - - const themeConfig = config?.ThemeConfig || { - defaultTheme: 'default' as const, - customCSS: '', - allowUserCustomization: true, - }; - - console.log('API /theme: 最终返回的主题配置:', themeConfig); - - return NextResponse.json({ - success: true, - data: themeConfig, - }); - } catch (error) { - console.error('API /theme: 获取主题配置失败,返回默认配置:', error); - - const defaultThemeConfig = { - defaultTheme: 'default' as const, - customCSS: '', - allowUserCustomization: true, - }; - - return NextResponse.json( - { - success: true, // 改为 success: true,因为我们提供了有效的默认配置 - data: defaultThemeConfig, - fallback: true, // 标记这是备用配置 - error: error instanceof Error ? error.message : '未知错误' - }, - { status: 200 } - ); - } -} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 455031f..5ed01a7 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -6,7 +6,6 @@ import { Inter } from 'next/font/google'; import './globals.css'; import { getConfig } from '@/lib/config'; -import { db } from '@/lib/db'; import { GlobalErrorIndicator } from '../components/GlobalErrorIndicator'; import { SiteProvider } from '../components/SiteProvider'; @@ -44,33 +43,6 @@ export default async function RootLayout({ }) { const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage'; - // 预先获取主题配置 - let themeConfig = { - defaultTheme: 'default', - customCSS: '', - allowUserCustomization: true - }; - - try { - if (storageType !== 'localstorage') { - const adminConfig = await db.getAdminConfig(); - if (adminConfig?.ThemeConfig) { - themeConfig = { - defaultTheme: adminConfig.ThemeConfig.defaultTheme || 'default', - customCSS: adminConfig.ThemeConfig.customCSS || '', - allowUserCustomization: adminConfig.ThemeConfig.allowUserCustomization !== false, - }; - console.log('服务端获取主题配置成功:', themeConfig); - } else { - console.log('服务端配置中没有主题配置,使用默认配置'); - } - } else { - console.log('LocalStorage模式,使用默认主题配置'); - } - } catch (error) { - console.error('服务端获取主题配置失败,使用默认配置:', error); - } - let siteName = process.env.NEXT_PUBLIC_SITE_NAME || 'OrangeTV'; let announcement = process.env.ANNOUNCEMENT || @@ -122,7 +94,6 @@ export default async function RootLayout({ CUSTOM_CATEGORIES: customCategories, FLUID_SEARCH: fluidSearch, REQUIRE_DEVICE_CODE: requireDeviceCode, - THEME_CONFIG: themeConfig, }; return ( @@ -141,84 +112,59 @@ export default async function RootLayout({ }} /> - {/* 主题初始化脚本 - 内联执行,确保在生产环境中立即应用主题 */} + {/* 立即从缓存应用主题,避免闪烁 */} {/* eslint-disable-next-line @next/next/no-sync-scripts */}