feat: 迁移自定义功能至 1.5.9.9 版本
包含音乐源限制、CI 防 OOM 优化、数据库连接池优化、管理端权限及 UI 细节定制等功能
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { db } from '~/drizzle/db'
|
||||
import { CacheService } from '~~/server/services/cacheService'
|
||||
import { users } from '~/drizzle/schema'
|
||||
import { inArray } from 'drizzle-orm'
|
||||
|
||||
@@ -8,7 +9,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (event.node.req.method !== 'POST') {
|
||||
throw createError({
|
||||
statusCode: 405,
|
||||
message: 'Method Not Allowed'
|
||||
statusMessage: 'Method Not Allowed'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,33 +21,21 @@ export default defineEventHandler(async (event) => {
|
||||
if (!userIds || !Array.isArray(userIds) || userIds.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'userIds is required and must be a non-empty array'
|
||||
})
|
||||
}
|
||||
|
||||
// 对输入的 userIds 进行去重和类型转换
|
||||
const uniqueUserIds = [
|
||||
...new Set(userIds.map((id) => parseInt(id)).filter((id) => !isNaN(id) && id > 0))
|
||||
]
|
||||
|
||||
if (uniqueUserIds.length === 0) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: '没有有效的用户ID'
|
||||
statusMessage: 'userIds is required and must be a non-empty array'
|
||||
})
|
||||
}
|
||||
|
||||
if (!targetGrade || typeof targetGrade !== 'string') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'targetGrade is required and must be a string'
|
||||
statusMessage: 'targetGrade is required and must be a string'
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof keepClass !== 'boolean') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'keepClass is required and must be a boolean'
|
||||
statusMessage: 'keepClass is required and must be a boolean'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,7 +45,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (!currentUser) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
message: 'Authentication required'
|
||||
statusMessage: 'Authentication required'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -64,7 +53,7 @@ export default defineEventHandler(async (event) => {
|
||||
if (!['ADMIN', 'SUPER_ADMIN'].includes(currentUser.role)) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
message: 'Insufficient permissions'
|
||||
statusMessage: 'Insufficient permissions'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -75,50 +64,26 @@ export default defineEventHandler(async (event) => {
|
||||
name: users.name,
|
||||
username: users.username,
|
||||
grade: users.grade,
|
||||
class: users.class,
|
||||
role: users.role
|
||||
class: users.class
|
||||
})
|
||||
.from(users)
|
||||
.where(inArray(users.id, uniqueUserIds))
|
||||
.where(inArray(users.id, userIds))
|
||||
|
||||
const existingUserIds = existingUsers.map((user) => user.id)
|
||||
const nonExistentUserIds = uniqueUserIds.filter((id) => !existingUserIds.includes(id))
|
||||
const nonExistentUserIds = userIds.filter((id) => !existingUserIds.includes(id))
|
||||
|
||||
let updated = 0
|
||||
let failed = 0
|
||||
const errors: Array<{ userId: number | string; error: string }> = []
|
||||
const errors: string[] = []
|
||||
|
||||
// 记录不存在的用户ID
|
||||
if (nonExistentUserIds.length > 0) {
|
||||
failed += nonExistentUserIds.length
|
||||
nonExistentUserIds.forEach((id) => {
|
||||
errors.push({ userId: id, error: '用户不存在' })
|
||||
})
|
||||
}
|
||||
|
||||
// 过滤出有权限修改的用户
|
||||
const validExistingUserIds: number[] = []
|
||||
for (const user of existingUsers) {
|
||||
if (user.id === 1) {
|
||||
failed++
|
||||
errors.push({ userId: user.id, error: '无法修改系统初始超级管理员' })
|
||||
continue
|
||||
}
|
||||
if (user.id === currentUser.id) {
|
||||
failed++
|
||||
errors.push({ userId: user.id, error: '禁止在用户管理中批量更新自己的账户' })
|
||||
continue
|
||||
}
|
||||
if (user.role === 'SUPER_ADMIN' && currentUser.role !== 'SUPER_ADMIN') {
|
||||
failed++
|
||||
errors.push({ userId: user.id, error: '权限不足:普通管理员无法修改超级管理员信息' })
|
||||
continue
|
||||
}
|
||||
validExistingUserIds.push(user.id)
|
||||
errors.push(`用户ID ${nonExistentUserIds.join(', ')} 不存在`)
|
||||
}
|
||||
|
||||
// 批量更新存在的用户
|
||||
if (validExistingUserIds.length > 0) {
|
||||
if (existingUserIds.length > 0) {
|
||||
try {
|
||||
const updateData: any = {
|
||||
grade: targetGrade,
|
||||
@@ -131,16 +96,25 @@ export default defineEventHandler(async (event) => {
|
||||
const updateResult = await db
|
||||
.update(users)
|
||||
.set(updateData)
|
||||
.where(inArray(users.id, validExistingUserIds))
|
||||
.where(inArray(users.id, existingUserIds))
|
||||
.returning({ id: users.id })
|
||||
|
||||
updated = updateResult.length
|
||||
} catch (error) {
|
||||
console.error('批量更新失败:', error)
|
||||
failed += validExistingUserIds.length
|
||||
validExistingUserIds.forEach((id) => {
|
||||
errors.push({ userId: id, error: '批量更新操作失败' })
|
||||
})
|
||||
failed += existingUserIds.length
|
||||
errors.push('批量更新操作失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有用户更新成功,清除相关缓存
|
||||
if (updated > 0) {
|
||||
try {
|
||||
const cacheService = CacheService.getInstance()
|
||||
await cacheService.clearSongsCache()
|
||||
console.log('批量年级更新后缓存清除成功')
|
||||
} catch (cacheError) {
|
||||
console.error('批量年级更新后缓存清除失败:', cacheError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +133,7 @@ export default defineEventHandler(async (event) => {
|
||||
failed,
|
||||
errors,
|
||||
details: {
|
||||
totalRequested: uniqueUserIds.length,
|
||||
totalRequested: userIds.length,
|
||||
existingUsers: existingUsers.length,
|
||||
nonExistentUsers: nonExistentUserIds.length,
|
||||
targetGrade,
|
||||
@@ -175,7 +149,7 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: 'Internal Server Error'
|
||||
statusMessage: 'Internal Server Error'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user