123 lines
4.3 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: 'https://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
loginLogoUrl: 'https://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?.user || !(session.user as any).id) {
return NextResponse.json(
{ error: '未登录' },
{ status: 401 }
);
}
const userRole = (session.user as any).role;
if (!isSystemAdmin(userRole)) {
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 || 'https://s3.1080p.hk/shuzhou/2025/05/18/8ebadf0a7cc34eaea00e8619b8efb827.png',
loginLogoUrl: loginLogoUrl || 'https://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 }
);
}
}