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

74 lines
1.9 KiB
TypeScript

// 切换用户下载权限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';
// POST - 切换用户下载权限(仅管理员)
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session || !isSystemAdmin(session.user.role)) {
return NextResponse.json(
{ error: '无权限' },
{ status: 403 }
);
}
const { userId } = await request.json();
if (!userId) {
return NextResponse.json(
{ error: '缺少用户ID' },
{ status: 400 }
);
}
// 不能修改自己的下载权限
if (userId === session.user.id) {
return NextResponse.json(
{ error: '不能修改自己的下载权限' },
{ status: 400 }
);
}
// 检查用户是否存在
const user = await prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
return NextResponse.json(
{ error: '用户不存在' },
{ status: 404 }
);
}
// 不能修改管理员的下载权限
if (user.role === 'ADMIN') {
return NextResponse.json(
{ error: '不能修改管理员的下载权限' },
{ status: 400 }
);
}
// 切换下载权限
const updatedUser = await prisma.user.update({
where: { id: userId },
data: { canDownload: !user.canDownload },
});
return NextResponse.json({
message: updatedUser.canDownload ? '已开启下载权限' : '已关闭下载权限',
canDownload: updatedUser.canDownload,
});
} catch (error) {
console.error('Error toggling download permission:', error);
return NextResponse.json(
{ error: '操作失败' },
{ status: 500 }
);
}
}