import { createError, defineEventHandler, getQuery } from 'h3' import { db } from '~/drizzle/db' import { users } from '~/drizzle/schema' import { and, asc, desc, count, eq, ilike, inArray, isNull, notInArray, or, sql } from 'drizzle-orm' import { resolveAvatarSource } from '~~/server/utils/user-avatar' import { ARCHIVED_USER_STATUSES, resolveArchivedFilter } from '~~/server/utils/user-archive' const UNSET_FILTER_VALUE = '__UNSET__' export default defineEventHandler(async (event) => { try { // 检查用户是否为管理员 const user = event.context.user if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) { throw createError({ statusCode: 403, message: '只有系统管理员可以访问用户列表' }) } const query = getQuery(event) const { grade, class: className, search, page = '1', limit = '50', role, status, sortBy = 'id', sortOrder = 'asc', archived } = query // 构建筛选条件 const whereConditions = [] // 年级筛选 if (grade && typeof grade === 'string' && grade.trim()) { const gradeFilter = grade.trim() whereConditions.push( gradeFilter === UNSET_FILTER_VALUE ? or(isNull(users.grade), eq(users.grade, '')) : eq(users.grade, gradeFilter) ) } // 班级筛选 if (className && typeof className === 'string' && className.trim()) { const classFilter = className.trim() whereConditions.push( classFilter === UNSET_FILTER_VALUE ? or(isNull(users.class), eq(users.class, '')) : eq(users.class, classFilter) ) } // 角色筛选 if (role && typeof role === 'string' && role.trim()) { whereConditions.push(eq(users.role, role.trim())) } // 状态筛选 if (status && typeof status === 'string' && status.trim()) { const statusFilter = status.trim() if (['active', 'pending', 'withdrawn', 'graduate'].includes(statusFilter)) { whereConditions.push(eq(users.status, statusFilter as 'active' | 'pending' | 'withdrawn' | 'graduate')) } } // 归档筛选:archived=1 仅查已归档(graduate/withdrawn);archived=0 排除已归档;缺省不限制 const archivedFilter = resolveArchivedFilter(archived) if (archivedFilter === 'archived') { whereConditions.push(inArray(users.status, [...ARCHIVED_USER_STATUSES])) } else if (archivedFilter === 'unarchived') { whereConditions.push(notInArray(users.status, [...ARCHIVED_USER_STATUSES])) } // 搜索功能(姓名、用户名或IP地址) if (search && typeof search === 'string' && search.trim()) { const searchTerm = search.trim() whereConditions.push( or( ilike(users.name, `%${searchTerm}%`), ilike(users.username, `%${searchTerm}%`), ilike(users.lastLoginIp, `%${searchTerm}%`) ) ) } const whereClause = whereConditions.length > 0 ? and(...whereConditions) : undefined // 分页参数 const pageNum = Math.max(1, parseInt(page as string) || 1) const limitNum = Math.min(Math.max(1, parseInt(limit as string) || 50), 1000) const skip = (pageNum - 1) * limitNum // 获取总数 const totalResult = await db.select({ count: count() }).from(users).where(whereClause) const total = totalResult[0]?.count || 0 // 排序逻辑 let orderByClause if (sortBy === 'name') { // 对中文名进行拼音排序 (使用 GBK 编码转换 hack,适用于大多数常见汉字) orderByClause = sortOrder === 'desc' ? sql`convert_to(${users.name}, 'GBK') DESC` : sql`convert_to(${users.name}, 'GBK') ASC` } else if (sortBy === 'lastLogin') { // 确保未登录用户(NULL)排在最后 orderByClause = sortOrder === 'desc' ? sql`${users.lastLogin} DESC NULLS LAST` : asc(users.lastLogin) } else if (sortBy === 'createdAt') { orderByClause = sortOrder === 'desc' ? desc(users.createdAt) : asc(users.createdAt) } else { // 默认按 id 排序 orderByClause = sortOrder === 'desc' ? desc(users.id) : asc(users.id) } // 获取用户列表 const usersList = await db.query.users.findMany({ where: whereClause, orderBy: orderByClause, limit: limitNum, offset: skip, columns: { id: true, name: true, username: true, role: true, grade: true, class: true, status: true, statusChangedAt: true, lastLogin: true, lastLoginIp: true, passwordChangedAt: true, forcePasswordChange: true, meowNickname: true, meowBoundAt: true, email: true, emailVerified: true, remark: true, createdAt: true, updatedAt: true, avatarProvider: true, avatarProviderUserId: true }, with: { identities: { columns: { provider: true, providerUsername: true, providerUserId: true, avatar: true, createdAt: true } } } }) // 处理用户列表,添加头像字段 const formattedUsers = usersList.map((user) => { const avatarSource = resolveAvatarSource(user, user.identities || []) return { ...user, avatar: avatarSource?.url ?? null } }) // 计算分页信息 const totalPages = Math.ceil(total / limitNum) const hasNextPage = pageNum < totalPages const hasPrevPage = pageNum > 1 return { success: true, users: formattedUsers, pagination: { total, page: pageNum, limit: limitNum, totalPages, hasNextPage, hasPrevPage }, filters: { grade: grade || null, class: className || null, role: role || null, status: status || null, search: search || null } } } catch (error: any) { console.error('获取用户列表失败:', error) throw createError({ statusCode: 500, message: '获取用户列表失败: ' + error.message }) } })