chenpei ea9076df45 feat: 实现统一的下载错误处理系统并修复产品全案数据库Schema
- 创建统一的下载错误处理模块 (lib/download-errors.ts)
- 实现13种错误类型分类和中文错误消息
- 后端统一使用 createDownloadError 创建标准化错误响应
- 前端统一使用 parseDownloadError 解析错误并显示
- 修复正式系统数据库Schema,添加产品全案相关字段和表
- 执行数据库迁移:添加 task_no, task_status, task_fetched_at 字段
- 创建 material_assets, api_keys, api_call_logs 表
2026-01-05 17:52:42 +08:00

116 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 系统设置API
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { isSystemAdmin } from '@/lib/department-permission';
import { proxyHttpUrl } from '@/lib/url-rewrite';
// GET - 获取系统设置
export async function GET() {
try {
let settings = await prisma.systemSettings.findFirst();
// 如果没有设置,创建默认设置
if (!settings) {
settings = await prisma.systemSettings.create({
data: {
headerBgColor: '#1f2937',
headerLogoUrl: 'http://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
loginLogoUrl: 'http://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
siteName: '由壹点3D素材',
},
});
}
// 将 HTTP URL 转换为通过代理的 URL(解决混合内容问题)
const response = {
...settings,
headerLogoUrl: proxyHttpUrl(settings.headerLogoUrl) || settings.headerLogoUrl,
loginLogoUrl: proxyHttpUrl(settings.loginLogoUrl) || settings.loginLogoUrl,
};
return NextResponse.json(response);
} catch (error) {
console.error('Get system settings error:', error);
return NextResponse.json(
{ error: '获取系统设置失败' },
{ status: 500 }
);
}
}
// PUT - 更新系统设置(仅管理员)
export async function PUT(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session || !isSystemAdmin(session.user.role)) {
return NextResponse.json(
{ error: '无权限' },
{ status: 403 }
);
}
const {
headerBgColor,
siteName,
headerLogoUrl,
loginLogoUrl,
s3Endpoint,
s3AccessKey,
s3SecretKey,
s3Bucket,
s3Region,
s3PublicUrl,
maintenanceEnabled,
maintenanceMessage,
maintenanceStartTime,
maintenanceEndTime,
} = await request.json();
let settings = await prisma.systemSettings.findFirst();
if (settings) {
// 更新现有设置
const updateData: any = {};
if (headerBgColor !== undefined) updateData.headerBgColor = headerBgColor;
if (siteName !== undefined) updateData.siteName = siteName;
if (headerLogoUrl !== undefined) updateData.headerLogoUrl = headerLogoUrl;
if (loginLogoUrl !== undefined) updateData.loginLogoUrl = loginLogoUrl;
if (s3Endpoint !== undefined) updateData.s3Endpoint = s3Endpoint;
if (s3AccessKey !== undefined) updateData.s3AccessKey = s3AccessKey;
if (s3SecretKey !== undefined) updateData.s3SecretKey = s3SecretKey;
if (s3Bucket !== undefined) updateData.s3Bucket = s3Bucket;
if (s3Region !== undefined) updateData.s3Region = s3Region;
if (s3PublicUrl !== undefined) updateData.s3PublicUrl = s3PublicUrl;
if (maintenanceEnabled !== undefined) updateData.maintenanceEnabled = maintenanceEnabled;
if (maintenanceMessage !== undefined) updateData.maintenanceMessage = maintenanceMessage;
if (maintenanceStartTime !== undefined) updateData.maintenanceStartTime = maintenanceStartTime ? new Date(maintenanceStartTime) : null;
if (maintenanceEndTime !== undefined) updateData.maintenanceEndTime = maintenanceEndTime ? new Date(maintenanceEndTime) : null;
settings = await prisma.systemSettings.update({
where: { id: settings.id },
data: updateData,
});
} else {
// 创建新设置
settings = await prisma.systemSettings.create({
data: {
headerBgColor: headerBgColor || '#1f2937',
headerLogoUrl: headerLogoUrl || 'http://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
loginLogoUrl: loginLogoUrl || 'http://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
siteName: siteName || '由壹点3D素材',
},
});
}
return NextResponse.json(settings);
} catch (error) {
console.error('Update system settings error:', error);
return NextResponse.json(
{ error: '更新系统设置失败' },
{ status: 500 }
);
}
}