初次提交
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { updateUserPassword } from '~~/server/services/userService'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
// 检查认证和权限
|
||||
const user = event.context.user
|
||||
if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
message: '没有权限访问'
|
||||
})
|
||||
}
|
||||
|
||||
// 安全获取ID参数
|
||||
const params = event.context.params || {}
|
||||
const id = parseInt(params.id || '')
|
||||
|
||||
if (isNaN(id)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '无效的用户ID'
|
||||
})
|
||||
}
|
||||
|
||||
const body = await readBody(event)
|
||||
|
||||
if (!body.newPassword) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '新密码不能为空'
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
// 禁止对自身进行密码重置(通过用户管理界面)
|
||||
if (id === user.id) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '禁止在用户管理中重置自己的密码'
|
||||
})
|
||||
}
|
||||
|
||||
// 使用统一服务重置密码 (forceReset = true)
|
||||
await updateUserPassword(id, body.newPassword, true)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: '密码重置成功'
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('重置密码失败:', error)
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: '重置密码失败'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,272 @@
|
||||
import { db } from '~/drizzle/db'
|
||||
import { schedules, songReplayRequests, songs, users, votes } from '~/drizzle/schema'
|
||||
import { and, count, desc, eq, inArray } from 'drizzle-orm'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// 使用认证中间件提供的用户信息
|
||||
const currentUser = event.context.user
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: '未授权访问'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查是否为管理员、歌曲管理员或超级管理员
|
||||
const allowedRoles = ['ADMIN', 'SONG_ADMIN', 'SUPER_ADMIN']
|
||||
if (!allowedRoles.includes(currentUser.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: '权限不足'
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户ID
|
||||
const userId = parseInt(getRouterParam(event, 'id') as string)
|
||||
if (!userId || isNaN(userId)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '无效的用户ID'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证用户是否存在
|
||||
const userResult = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
username: users.username,
|
||||
grade: users.grade,
|
||||
class: users.class
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, userId))
|
||||
.limit(1)
|
||||
|
||||
const user = userResult[0]
|
||||
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: '用户不存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户投稿的歌曲
|
||||
const submittedSongs = await db
|
||||
.select({
|
||||
id: songs.id,
|
||||
title: songs.title,
|
||||
artist: songs.artist,
|
||||
semester: songs.semester,
|
||||
createdAt: songs.createdAt,
|
||||
played: songs.played
|
||||
})
|
||||
.from(songs)
|
||||
.where(eq(songs.requesterId, userId))
|
||||
.orderBy(desc(songs.createdAt))
|
||||
|
||||
// 获取投稿歌曲的投票数
|
||||
const submittedSongIds = submittedSongs.map((song) => song.id)
|
||||
const submittedVoteCountsMap = new Map()
|
||||
if (submittedSongIds.length > 0) {
|
||||
const submittedVoteCountsResult = await db
|
||||
.select({
|
||||
songId: votes.songId,
|
||||
voteCount: count(votes.id)
|
||||
})
|
||||
.from(votes)
|
||||
.where(inArray(votes.songId, submittedSongIds))
|
||||
.groupBy(votes.songId)
|
||||
|
||||
submittedVoteCountsResult.forEach((item) => {
|
||||
submittedVoteCountsMap.set(item.songId, item.voteCount)
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户投票的歌曲
|
||||
const votedSongs = await db
|
||||
.select({
|
||||
id: votes.id,
|
||||
createdAt: votes.createdAt,
|
||||
songId: votes.songId,
|
||||
songTitle: songs.title,
|
||||
songArtist: songs.artist,
|
||||
songSemester: songs.semester,
|
||||
songPlayed: songs.played,
|
||||
requesterName: users.name,
|
||||
requesterGrade: users.grade,
|
||||
requesterClass: users.class
|
||||
})
|
||||
.from(votes)
|
||||
.leftJoin(songs, eq(votes.songId, songs.id))
|
||||
.leftJoin(users, eq(songs.requesterId, users.id))
|
||||
.where(eq(votes.userId, userId))
|
||||
.orderBy(desc(votes.createdAt))
|
||||
|
||||
// 获取投票歌曲的投票数
|
||||
const votedSongIds = votedSongs.map((vote) => vote.songId).filter((id) => id !== null)
|
||||
const votedVoteCountsMap = new Map()
|
||||
if (votedSongIds.length > 0) {
|
||||
const votedVoteCountsResult = await db
|
||||
.select({
|
||||
songId: votes.songId,
|
||||
voteCount: count(votes.id)
|
||||
})
|
||||
.from(votes)
|
||||
.where(inArray(votes.songId, votedSongIds))
|
||||
.groupBy(votes.songId)
|
||||
|
||||
votedVoteCountsResult.forEach((item) => {
|
||||
votedVoteCountsMap.set(item.songId, item.voteCount)
|
||||
})
|
||||
}
|
||||
|
||||
// 获取投稿歌曲的排期状态
|
||||
// 只查询已发布的排期,草稿不算作已排期
|
||||
const submittedScheduleMap = new Map()
|
||||
if (submittedSongIds.length > 0) {
|
||||
const submittedScheduleResult = await db
|
||||
.select({
|
||||
songId: schedules.songId
|
||||
})
|
||||
.from(schedules)
|
||||
.where(and(inArray(schedules.songId, submittedSongIds), eq(schedules.isDraft, false)))
|
||||
|
||||
submittedScheduleResult.forEach((item) => {
|
||||
submittedScheduleMap.set(item.songId, true)
|
||||
})
|
||||
}
|
||||
|
||||
// 获取投票歌曲的排期状态
|
||||
// 只查询已发布的排期,草稿不算作已排期
|
||||
const votedScheduleMap = new Map()
|
||||
if (votedSongIds.length > 0) {
|
||||
const votedScheduleResult = await db
|
||||
.select({
|
||||
songId: schedules.songId
|
||||
})
|
||||
.from(schedules)
|
||||
.where(and(inArray(schedules.songId, votedSongIds), eq(schedules.isDraft, false)))
|
||||
|
||||
votedScheduleResult.forEach((item) => {
|
||||
votedScheduleMap.set(item.songId, true)
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户申请重播的歌曲
|
||||
const replayRequestedSongs = await db
|
||||
.select({
|
||||
id: songReplayRequests.id,
|
||||
createdAt: songReplayRequests.createdAt,
|
||||
songId: songReplayRequests.songId,
|
||||
songTitle: songs.title,
|
||||
songArtist: songs.artist,
|
||||
songSemester: songs.semester,
|
||||
songPlayed: songs.played,
|
||||
requesterName: users.name,
|
||||
requesterGrade: users.grade,
|
||||
requesterClass: users.class
|
||||
})
|
||||
.from(songReplayRequests)
|
||||
.leftJoin(songs, eq(songReplayRequests.songId, songs.id))
|
||||
.leftJoin(users, eq(songs.requesterId, users.id))
|
||||
.where(eq(songReplayRequests.userId, userId))
|
||||
.orderBy(desc(songReplayRequests.createdAt))
|
||||
|
||||
// 获取重播申请歌曲的总申请数
|
||||
const replaySongIds = replayRequestedSongs.map((req) => req.songId).filter((id) => id !== null)
|
||||
const replayRequestCountsMap = new Map()
|
||||
if (replaySongIds.length > 0) {
|
||||
const replayRequestCountsResult = await db
|
||||
.select({
|
||||
songId: songReplayRequests.songId,
|
||||
requestCount: count(songReplayRequests.id)
|
||||
})
|
||||
.from(songReplayRequests)
|
||||
.where(inArray(songReplayRequests.songId, replaySongIds))
|
||||
.groupBy(songReplayRequests.songId)
|
||||
|
||||
replayRequestCountsResult.forEach((item) => {
|
||||
replayRequestCountsMap.set(item.songId, item.requestCount)
|
||||
})
|
||||
}
|
||||
|
||||
// 获取重播申请歌曲的排期状态
|
||||
const replayScheduleMap = new Map()
|
||||
if (replaySongIds.length > 0) {
|
||||
const replayScheduleResult = await db
|
||||
.select({
|
||||
songId: schedules.songId
|
||||
})
|
||||
.from(schedules)
|
||||
.where(and(inArray(schedules.songId, replaySongIds), eq(schedules.isDraft, false)))
|
||||
|
||||
replayScheduleResult.forEach((item) => {
|
||||
replayScheduleMap.set(item.songId, true)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
user: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
username: user.username,
|
||||
grade: user.grade,
|
||||
class: user.class
|
||||
},
|
||||
submittedSongs: submittedSongs.map((song) => ({
|
||||
id: song.id,
|
||||
title: song.title,
|
||||
artist: song.artist,
|
||||
semester: song.semester,
|
||||
createdAt: song.createdAt,
|
||||
played: song.played,
|
||||
scheduled: submittedScheduleMap.has(song.id),
|
||||
voteCount: submittedVoteCountsMap.get(song.id) || 0
|
||||
})),
|
||||
votedSongs: votedSongs.map((vote) => ({
|
||||
id: vote.songId,
|
||||
title: vote.songTitle,
|
||||
artist: vote.songArtist,
|
||||
semester: vote.songSemester,
|
||||
played: vote.songPlayed,
|
||||
scheduled: votedScheduleMap.has(vote.songId),
|
||||
voteCount: votedVoteCountsMap.get(vote.songId) || 0,
|
||||
votedAt: vote.createdAt,
|
||||
requester: {
|
||||
name: vote.requesterName,
|
||||
grade: vote.requesterGrade,
|
||||
class: vote.requesterClass
|
||||
}
|
||||
})),
|
||||
replayRequestedSongs: replayRequestedSongs.map((req) => ({
|
||||
id: req.songId,
|
||||
title: req.songTitle,
|
||||
artist: req.songArtist,
|
||||
semester: req.songSemester,
|
||||
played: req.songPlayed,
|
||||
scheduled: replayScheduleMap.has(req.songId),
|
||||
requestCount: replayRequestCountsMap.get(req.songId) || 0,
|
||||
requestedAt: req.createdAt,
|
||||
requester: {
|
||||
name: req.requesterName,
|
||||
grade: req.requesterGrade,
|
||||
class: req.requesterClass
|
||||
}
|
||||
}))
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户歌曲信息失败:', error)
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: '服务器内部错误'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,123 @@
|
||||
import { createError, defineEventHandler, getQuery, getRouterParam } from 'h3'
|
||||
import { db } from '~/drizzle/db'
|
||||
import { users, userStatusLogs } from '~/drizzle/schema'
|
||||
import { count, desc, eq } from 'drizzle-orm'
|
||||
import { getStatusText } from '~~/server/utils/user'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// 检查认证和权限
|
||||
const user = event.context.user
|
||||
if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: '没有权限访问'
|
||||
})
|
||||
}
|
||||
|
||||
const userId = getRouterParam(event, 'id')
|
||||
const query = getQuery(event)
|
||||
const { page = '1', limit = '20' } = query
|
||||
|
||||
// 验证用户ID
|
||||
const numUserId = parseInt(userId)
|
||||
if (isNaN(numUserId) || numUserId <= 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '无效的用户ID'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查用户是否存在
|
||||
const existingUser = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
username: users.username
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, numUserId))
|
||||
.limit(1)
|
||||
|
||||
if (existingUser.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: '用户不存在'
|
||||
})
|
||||
}
|
||||
|
||||
// 分页参数
|
||||
const pageNum = Math.max(1, parseInt(page as string) || 1)
|
||||
const limitNum = Math.max(1, parseInt(limit as string) || 20)
|
||||
const skip = (pageNum - 1) * limitNum
|
||||
|
||||
// 获取总数
|
||||
const totalResult = await db
|
||||
.select({ count: count() })
|
||||
.from(userStatusLogs)
|
||||
.where(eq(userStatusLogs.userId, numUserId))
|
||||
const total = totalResult[0].count
|
||||
|
||||
// 获取状态变更日志列表
|
||||
const logs = await db
|
||||
.select({
|
||||
id: userStatusLogs.id,
|
||||
oldStatus: userStatusLogs.oldStatus,
|
||||
newStatus: userStatusLogs.newStatus,
|
||||
reason: userStatusLogs.reason,
|
||||
createdAt: userStatusLogs.createdAt,
|
||||
operatorId: userStatusLogs.operatorId,
|
||||
operatorName: users.name,
|
||||
operatorUsername: users.username
|
||||
})
|
||||
.from(userStatusLogs)
|
||||
.leftJoin(users, eq(userStatusLogs.operatorId, users.id))
|
||||
.where(eq(userStatusLogs.userId, numUserId))
|
||||
.orderBy(desc(userStatusLogs.createdAt))
|
||||
.limit(limitNum)
|
||||
.offset(skip)
|
||||
|
||||
// 计算分页信息
|
||||
const totalPages = Math.ceil(total / limitNum)
|
||||
const hasNextPage = pageNum < totalPages
|
||||
const hasPrevPage = pageNum > 1
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: existingUser[0],
|
||||
logs: logs.map((log) => ({
|
||||
id: log.id,
|
||||
oldStatus: log.oldStatus,
|
||||
newStatus: log.newStatus,
|
||||
oldStatusDisplay: getStatusText(log.oldStatus || ''),
|
||||
newStatusDisplay: getStatusText(log.newStatus || ''),
|
||||
reason: log.reason,
|
||||
createdAt: log.createdAt,
|
||||
operator: {
|
||||
id: log.operatorId,
|
||||
name: log.operatorName || '未知操作员',
|
||||
username: log.operatorUsername || 'unknown'
|
||||
}
|
||||
})),
|
||||
pagination: {
|
||||
total,
|
||||
page: pageNum,
|
||||
limit: limitNum,
|
||||
totalPages,
|
||||
hasNextPage,
|
||||
hasPrevPage
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户状态变更日志失败:', error)
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: '获取用户状态变更日志失败: ' + error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
import { createError, defineEventHandler, getRouterParam, readBody } from 'h3'
|
||||
import { db } from '~/drizzle/db'
|
||||
import { users, userStatusLogs } from '~/drizzle/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { getBeijingTime } from '~/utils/timeUtils'
|
||||
import { getStatusText } from '~~/server/utils/user'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// 检查认证和权限
|
||||
const user = event.context.user
|
||||
if (!user || !['ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: '没有权限访问'
|
||||
})
|
||||
}
|
||||
|
||||
const userId = getRouterParam(event, 'id')
|
||||
const body = await readBody(event)
|
||||
const { status, reason } = body
|
||||
|
||||
// 验证必填字段
|
||||
if (!status || !['active', 'withdrawn', 'graduate'].includes(status)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '状态必须为 active, withdrawn 或 graduate'
|
||||
})
|
||||
}
|
||||
|
||||
if (!reason || reason.trim().length === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '变更原因为必填项'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查用户是否存在
|
||||
const existingUser = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
username: users.username,
|
||||
status: users.status,
|
||||
role: users.role
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, parseInt(userId)))
|
||||
.limit(1)
|
||||
|
||||
if (existingUser.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: '用户不存在'
|
||||
})
|
||||
}
|
||||
|
||||
const targetUser = existingUser[0]
|
||||
|
||||
// 检查是否为学生用户
|
||||
if (targetUser.role !== 'USER') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '只能修改学生用户的状态'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查状态是否有变化
|
||||
if (targetUser.status === status) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '用户状态未发生变化'
|
||||
})
|
||||
}
|
||||
|
||||
const currentTime = getBeijingTime()
|
||||
const oldStatus = targetUser.status
|
||||
|
||||
// 开始事务
|
||||
await db.transaction(async (tx) => {
|
||||
// 更新用户状态
|
||||
await tx
|
||||
.update(users)
|
||||
.set({
|
||||
status: status,
|
||||
statusChangedAt: currentTime,
|
||||
statusChangedBy: user.id
|
||||
})
|
||||
.where(eq(users.id, parseInt(userId)))
|
||||
|
||||
// 记录状态变更日志
|
||||
await tx.insert(userStatusLogs).values({
|
||||
userId: parseInt(userId),
|
||||
oldStatus: oldStatus,
|
||||
newStatus: status,
|
||||
reason: reason.trim(),
|
||||
operatorId: user.id,
|
||||
createdAt: currentTime
|
||||
})
|
||||
})
|
||||
|
||||
// 清除相关缓存
|
||||
try {
|
||||
const { cache } = await import('~~/server/utils/cache-helpers')
|
||||
await cache.delete(`auth:user:${parseInt(userId)}`)
|
||||
console.log('[Cache] 用户认证缓存已清除(状态更新)')
|
||||
} catch (cacheError) {
|
||||
console.warn('[Cache] 清除缓存失败:', cacheError)
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `用户状态已更新为${getStatusText(status)}`,
|
||||
data: {
|
||||
userId: parseInt(userId),
|
||||
oldStatus,
|
||||
newStatus: status,
|
||||
changedAt: currentTime,
|
||||
changedBy: user.name
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('更新用户状态失败:', error)
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: '更新用户状态失败: ' + error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user