import { NextRequest, NextResponse } from 'next/server' import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { prisma } from '@/lib/prisma' // 激活/禁用用户 export async function POST(req: NextRequest) { try { const session = await getServerSession(authOptions) if (!session || (session.user as any).role !== 'ADMIN') { return NextResponse.json( { error: '无权限' }, { status: 403 } ) } const { userId, isActive } = await req.json() if (!userId || typeof isActive !== 'boolean') { return NextResponse.json( { error: '参数错误' }, { status: 400 } ) } // 不能禁用自己 if (userId === (session.user as any).id) { return NextResponse.json( { error: '不能禁用自己' }, { status: 400 } ) } // 更新用户激活状态 await prisma.user.update({ where: { id: userId }, data: { isActive }, }) return NextResponse.json({ message: isActive ? '用户已激活' : '用户已禁用', }) } catch (error) { console.error('Toggle active error:', error) return NextResponse.json( { error: '操作失败' }, { status: 500 } ) } }