feat: 迁移自定义功能至 1.5.9.9 版本
包含音乐源限制、CI 防 OOM 优化、数据库连接池优化、管理端权限及 UI 细节定制等功能
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { z } from 'zod'
|
||||
import { db } from '~/drizzle/db'
|
||||
import { CacheService } from '~~/server/services/cacheService'
|
||||
import { users, userStatusLogs } from '~/drizzle/schema'
|
||||
import { and, eq, inArray, ne } from 'drizzle-orm'
|
||||
|
||||
@@ -27,7 +28,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Authentication required'
|
||||
statusMessage: 'Authentication required'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -35,7 +36,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (!['ADMIN', 'SUPER_ADMIN'].includes(currentUser.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
message: 'Insufficient permissions'
|
||||
statusMessage: 'Insufficient permissions'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -46,7 +47,8 @@ export default defineEventHandler(async (event) => {
|
||||
if (!validationResult.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '请求参数无效: ' + validationResult.error.issues.map((e) => e.message).join(', ')
|
||||
statusMessage:
|
||||
'请求参数无效: ' + validationResult.error.errors.map((e) => e.message).join(', ')
|
||||
})
|
||||
}
|
||||
|
||||
@@ -55,7 +57,7 @@ export default defineEventHandler(async (event) => {
|
||||
// 获取所有要更新的用户ID
|
||||
const userIds = updates.map((update) => update.userId)
|
||||
|
||||
// 验证用户是否存在
|
||||
// 验证用户是否存在且为学生角色
|
||||
const existingUsers = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
@@ -65,38 +67,25 @@ export default defineEventHandler(async (event) => {
|
||||
status: users.status
|
||||
})
|
||||
.from(users)
|
||||
.where(inArray(users.id, userIds))
|
||||
.where(and(inArray(users.id, userIds), eq(users.role, 'USER')))
|
||||
|
||||
// 检查是否所有用户都存在
|
||||
// 检查是否所有用户都存在且为学生
|
||||
const existingUserIds = new Set(existingUsers.map((user) => user.id))
|
||||
const missingUserIds = userIds.filter((id) => !existingUserIds.has(id))
|
||||
|
||||
if (missingUserIds.length > 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: `以下用户ID不存在: ${missingUserIds.join(', ')}`
|
||||
statusMessage: `以下用户ID不存在或不是学生: ${missingUserIds.join(', ')}`
|
||||
})
|
||||
}
|
||||
|
||||
// 执行批量更新
|
||||
const updateResults = []
|
||||
const errors = []
|
||||
const userMap = new Map(existingUsers.map((u) => [u.id, u]))
|
||||
|
||||
for (const update of updates) {
|
||||
try {
|
||||
const targetUser = userMap.get(update.userId)
|
||||
if (!targetUser) continue
|
||||
|
||||
// 保护系统初始超级管理员
|
||||
if (targetUser.id === 1) {
|
||||
errors.push({
|
||||
userId: update.userId,
|
||||
error: '无法修改系统初始超级管理员'
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// 禁止批量更新包含当前操作者自身
|
||||
if (update.userId === currentUser.id) {
|
||||
errors.push({
|
||||
@@ -105,15 +94,6 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// 越级修改保护
|
||||
if (targetUser.role === 'SUPER_ADMIN' && currentUser.role !== 'SUPER_ADMIN') {
|
||||
errors.push({
|
||||
userId: update.userId,
|
||||
error: '权限不足:普通管理员无法修改超级管理员信息'
|
||||
})
|
||||
continue
|
||||
}
|
||||
// 构建更新数据
|
||||
const updateData: any = {}
|
||||
|
||||
@@ -162,7 +142,7 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
let updatedUser
|
||||
|
||||
|
||||
// 使用事务确保更新和日志记录的原子性
|
||||
await db.transaction(async (tx) => {
|
||||
// 更新用户信息
|
||||
@@ -225,6 +205,17 @@ export default defineEventHandler(async (event) => {
|
||||
response.message = `批量更新部分完成,成功更新 ${updateResults.length} 个用户,${errors.length} 个用户更新失败`
|
||||
}
|
||||
|
||||
// 如果有用户更新成功,清除相关缓存
|
||||
if (updateResults.length > 0) {
|
||||
try {
|
||||
const cacheService = CacheService.getInstance()
|
||||
await cacheService.clearSongsCache()
|
||||
console.log('批量用户更新后缓存清除成功')
|
||||
} catch (cacheError) {
|
||||
console.error('批量用户更新后缓存清除失败:', cacheError)
|
||||
}
|
||||
}
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error('批量更新用户失败:', error)
|
||||
@@ -237,7 +228,7 @@ export default defineEventHandler(async (event) => {
|
||||
// 未知错误
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: '批量更新用户失败: ' + error.message
|
||||
statusMessage: '批量更新用户失败: ' + error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user