128 lines
3.2 KiB
TypeScript
128 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
|
|
// Get all users (admin only) - 支持分页
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any).role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '无权限' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const page = parseInt(searchParams.get('page') || '1', 10)
|
|
const pageSize = Math.min(parseInt(searchParams.get('pageSize') || '20', 10), 100) // 最大100条
|
|
const skip = (page - 1) * pageSize
|
|
const keyword = searchParams.get('search')?.trim() || ''
|
|
|
|
// 并行查询总数和数据
|
|
const where =
|
|
keyword
|
|
? {
|
|
OR: [
|
|
{ username: { contains: keyword, mode: 'insensitive' as const } },
|
|
{ email: { contains: keyword, mode: 'insensitive' as const } },
|
|
{ department: { contains: keyword, mode: 'insensitive' as const } },
|
|
],
|
|
}
|
|
: {}
|
|
|
|
const [total, users] = await Promise.all([
|
|
prisma.user.count({ where }),
|
|
prisma.user.findMany({
|
|
where,
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
username: true,
|
|
parentId: true,
|
|
role: true,
|
|
isActive: true,
|
|
canDownload: true,
|
|
department: true,
|
|
managedDepartment: true,
|
|
isSuperAdmin: true,
|
|
createdAt: true,
|
|
_count: {
|
|
select: {
|
|
materials: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
createdAt: 'asc', // 按创建时间升序,第一个用户就是最高权限账号
|
|
},
|
|
skip,
|
|
take: pageSize,
|
|
})
|
|
])
|
|
|
|
return NextResponse.json({
|
|
users,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
total,
|
|
totalPages: Math.ceil(total / pageSize)
|
|
}
|
|
})
|
|
} catch (error) {
|
|
console.error('[AdminUsers] Get error:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取用户列表失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// Delete user (admin only)
|
|
export async function DELETE(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session || (session.user as any).role !== 'ADMIN') {
|
|
return NextResponse.json(
|
|
{ error: '无权限' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
const id = searchParams.get('id')
|
|
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: '缺少用户ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Don't allow deleting self
|
|
if (id === (session.user as any).id) {
|
|
return NextResponse.json(
|
|
{ error: '不能删除自己' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
await prisma.user.delete({
|
|
where: { id },
|
|
})
|
|
|
|
return NextResponse.json({ message: '删除成功' })
|
|
} catch (error) {
|
|
console.error('Error deleting user:', error)
|
|
return NextResponse.json(
|
|
{ error: '删除失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|