添加自定义主题支持

This commit is contained in:
mtvpls
2025-12-12 18:06:34 +08:00
parent 4ff7c1caf3
commit 15d86e3252
7 changed files with 918 additions and 1 deletions
+111
View File
@@ -0,0 +1,111 @@
/* eslint-disable @typescript-eslint/no-explicit-any,no-console */
import { NextRequest, NextResponse } from 'next/server';
import { getAuthInfoFromCookie } from '@/lib/auth';
import { getConfig } from '@/lib/config';
import { db } from '@/lib/db';
export const runtime = 'nodejs';
export async function POST(request: NextRequest) {
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
if (storageType === 'localstorage') {
return NextResponse.json(
{
error: '不支持本地存储进行管理员配置',
},
{ status: 400 }
);
}
try {
const body = await request.json();
const authInfo = getAuthInfoFromCookie(request);
if (!authInfo || !authInfo.username) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const username = authInfo.username;
const {
enableBuiltInTheme,
builtInTheme,
customCSS,
enableCache,
cacheMinutes,
} = body as {
enableBuiltInTheme: boolean;
builtInTheme: string;
customCSS: string;
enableCache: boolean;
cacheMinutes: number;
};
// 参数校验
if (
typeof enableBuiltInTheme !== 'boolean' ||
typeof builtInTheme !== 'string' ||
typeof customCSS !== 'string' ||
typeof enableCache !== 'boolean' ||
typeof cacheMinutes !== 'number'
) {
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
}
const adminConfig = await getConfig();
// 权限校验
if (username !== process.env.USERNAME) {
// 管理员
const user = adminConfig.UserConfig.Users.find(
(u) => u.username === username
);
if (!user || user.role !== 'admin' || user.banned) {
return NextResponse.json({ error: '权限不足' }, { status: 401 });
}
}
// 获取当前版本号,如果CSS有变化则递增
const currentVersion = adminConfig.ThemeConfig?.cacheVersion || 0;
const currentCSS = enableBuiltInTheme
? adminConfig.ThemeConfig?.builtInTheme
: adminConfig.ThemeConfig?.customCSS;
const newCSS = enableBuiltInTheme ? builtInTheme : customCSS;
const cssChanged = currentCSS !== newCSS;
// 更新主题配置
adminConfig.ThemeConfig = {
enableBuiltInTheme,
builtInTheme,
customCSS,
enableCache,
cacheMinutes,
cacheVersion: cssChanged ? currentVersion + 1 : currentVersion,
};
// 写入数据库
await db.saveAdminConfig(adminConfig);
return NextResponse.json(
{
ok: true,
cacheVersion: adminConfig.ThemeConfig.cacheVersion,
},
{
headers: {
'Cache-Control': 'no-store',
},
}
);
} catch (error) {
console.error('更新主题配置失败:', error);
return NextResponse.json(
{
error: '更新主题配置失败',
details: (error as Error).message,
},
{ status: 500 }
);
}
}
+74
View File
@@ -0,0 +1,74 @@
/* eslint-disable @typescript-eslint/no-explicit-any,no-console */
import { NextRequest, NextResponse } from 'next/server';
import { getConfig } from '@/lib/config';
import { getThemeCSS } from '@/styles/themes';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
try {
const adminConfig = await getConfig();
const themeConfig = adminConfig.ThemeConfig;
// 如果没有配置主题,返回空CSS
if (!themeConfig) {
return new NextResponse('', {
headers: {
'Content-Type': 'text/css',
'Cache-Control': 'no-store',
},
});
}
let css = '';
// 如果启用了内置主题,使用内置主题CSS
if (themeConfig.enableBuiltInTheme) {
css = getThemeCSS(themeConfig.builtInTheme as any);
} else {
// 否则使用自定义CSS
css = themeConfig.customCSS || '';
}
// 设置缓存控制
const cacheMinutes = themeConfig.cacheMinutes || 1440; // 默认1天(1440分钟)
const maxAge = cacheMinutes * 60; // 转换为秒
const staleWhileRevalidate = maxAge * 7; // 过期后7倍时间内可使用旧版本
const cacheControl = themeConfig.enableCache
? `public, max-age=${maxAge}, stale-while-revalidate=${staleWhileRevalidate}`
: 'no-store';
// 添加版本号到ETag
const etag = `"${themeConfig.cacheVersion}"`;
// 检查客户端缓存
const ifNoneMatch = request.headers.get('if-none-match');
if (ifNoneMatch === etag && themeConfig.enableCache) {
return new NextResponse(null, {
status: 304,
headers: {
'Cache-Control': cacheControl,
ETag: etag,
},
});
}
return new NextResponse(css, {
headers: {
'Content-Type': 'text/css; charset=utf-8',
'Cache-Control': cacheControl,
ETag: etag,
},
});
} catch (error) {
console.error('获取主题CSS失败:', error);
return new NextResponse('', {
headers: {
'Content-Type': 'text/css',
'Cache-Control': 'no-store',
},
});
}
}