更新安全配置项目

This commit is contained in:
2026-04-13 22:10:43 +08:00
parent 8e245076fe
commit 70d3020035
8 changed files with 43 additions and 284 deletions
+32 -50
View File
@@ -1,61 +1,43 @@
import { NextResponse } from "next/server";
import { getConfig, saveDBConfig, saveAIConfig, saveExportConfig, type DBConfig, type AIConfig, type ExportConfig } from "@/lib/config";
import {NextResponse} from "next/server";
import {getAllConfig, saveAIConfig, saveExportConfig} from "@/lib/config";
// GET /api/config - 获取完整配置
export async function GET() {
try {
const config = getConfig();
return NextResponse.json({
success: true,
data: config,
});
} catch (error) {
return NextResponse.json(
{ success: false, error: "读取配置失败" },
{ status: 500 }
);
}
try {
const config = getAllConfig();
return NextResponse.json({
success: true,
data: config,
});
} catch (error) {
return NextResponse.json(
{success: false, error: "读取配置失败"},
{status: 500}
);
}
}
// POST /api/config - 保存配置
// Body 格式: { db?: DBConfig, ai?: AIConfig, export?: ExportConfig }
// Body 格式: {ai?: AIConfig, export?: ExportConfig }
export async function POST(request: Request) {
try {
const body = await request.json();
const { db, ai, export: exportConfig } = body;
try {
const body = await request.json();
const {ai, export: exportConfig} = body;
if (ai) {
saveAIConfig(ai);
}
if (db) {
saveDBConfig(db);
}
if (ai) {
saveAIConfig(ai);
}
if (exportConfig) {
saveExportConfig(exportConfig);
}
// 如果同时发送了完整配置
if (!db && !ai && !exportConfig && body.db === undefined && body.ai === undefined && body.export === undefined) {
// 可能是旧的扁平格式,尝试兼容
if (body.apiKey !== undefined || body.baseUrl !== undefined || body.model !== undefined) {
saveAIConfig({
apiKey: body.apiKey,
baseUrl: body.baseUrl,
model: body.model,
if (exportConfig) {
saveExportConfig(exportConfig);
}
return NextResponse.json({
success: true,
message: "配置已保存",
});
}
} catch (error) {
return NextResponse.json(
{success: false, error: "保存失败"},
{status: 500}
);
}
return NextResponse.json({
success: true,
message: "配置已保存",
});
} catch (error) {
return NextResponse.json(
{ success: false, error: "保存失败" },
{ status: 500 }
);
}
}