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

187 lines
5.5 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.

import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { isSuperAdmin, canAddSuperAdmin, getSuperAdminCount } from '@/lib/department-permission';
/**
* GET - 获取所有超级管理员列表
*/
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const superAdmins = await prisma.user.findMany({
where: { isSuperAdmin: true },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
createdAt: true,
},
orderBy: { createdAt: 'asc' },
});
const count = await getSuperAdminCount();
const canAdd = await canAddSuperAdmin();
return NextResponse.json({
superAdmins,
count,
maxCount: 4,
canAdd,
});
} catch (error) {
console.error('[Super Admins] GET Error:', error);
return NextResponse.json({ error: '获取超级管理员列表失败' }, { status: 500 });
}
}
/**
* POST - 添加超级管理员
*/
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const { userId: targetUserId } = await request.json();
if (!targetUserId) {
return NextResponse.json({ error: '用户ID不能为空' }, { status: 400 });
}
// 检查是否可以添加(最多4个)
if (!(await canAddSuperAdmin())) {
return NextResponse.json({ error: '超级管理员数量已达上限(最多4个)' }, { status: 400 });
}
// 检查目标用户是否存在
const targetUser = await prisma.user.findUnique({
where: { id: targetUserId },
});
if (!targetUser) {
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
}
// 检查是否已经是超级管理员
if (targetUser.isSuperAdmin) {
return NextResponse.json({ error: '该用户已经是超级管理员' }, { status: 400 });
}
// 检查是否是管理员角色(ADMIN或SYSTEM_ADMIN)
const { isSystemAdmin: isSystemAdminCheck } = await import('@/lib/department-permission');
if (!isSystemAdminCheck(targetUser.role)) {
return NextResponse.json({ error: '只有系统管理员才能被设置为超级管理员' }, { status: 400 });
}
// 设置为超级管理员
const updatedUser = await prisma.user.update({
where: { id: targetUserId },
data: { isSuperAdmin: true },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
isSuperAdmin: true,
createdAt: true,
},
});
return NextResponse.json({
message: '超级管理员添加成功',
user: updatedUser,
});
} catch (error) {
console.error('[Super Admins] POST Error:', error);
return NextResponse.json({ error: '添加超级管理员失败' }, { status: 500 });
}
}
/**
* DELETE - 移除超级管理员
*/
export async function DELETE(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const { searchParams } = new URL(request.url);
const targetUserId = searchParams.get('userId');
if (!targetUserId) {
return NextResponse.json({ error: '用户ID不能为空' }, { status: 400 });
}
// 不能移除自己
if (targetUserId === userId) {
return NextResponse.json({ error: '不能移除自己的超级管理员权限' }, { status: 400 });
}
// 检查目标用户是否存在
const targetUser = await prisma.user.findUnique({
where: { id: targetUserId },
});
if (!targetUser) {
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
}
// 检查是否是超级管理员
if (!targetUser.isSuperAdmin) {
return NextResponse.json({ error: '该用户不是超级管理员' }, { status: 400 });
}
// 检查移除后是否至少还有1个超级管理员
const count = await getSuperAdminCount();
if (count <= 1) {
return NextResponse.json({ error: '至少需要保留1个超级管理员' }, { status: 400 });
}
// 移除超级管理员权限
const updatedUser = await prisma.user.update({
where: { id: targetUserId },
data: { isSuperAdmin: false },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
isSuperAdmin: true,
createdAt: true,
},
});
return NextResponse.json({
message: '超级管理员权限已移除',
user: updatedUser,
});
} catch (error) {
console.error('[Super Admins] DELETE Error:', error);
return NextResponse.json({ error: '移除超级管理员失败' }, { status: 500 });
}
}