fix(users): 状态大小写不敏感 + 彻底修掉「无法编辑其它用户」
站长反馈仍然无法编辑其它用户。上一版只把**小程序端**的状态改成小写, 但如果小程序没重新编译(或用的还是旧版本),发上来的仍是 `ACTIVE`, 服务端 `['active','withdrawn','graduate'].includes(status)` 依旧不通过 → 400 「用户状态只能是 active, withdrawn 或 graduate」——编辑任何用户都会失败。 这版把容忍度放到**服务端**,与客户端版本解耦: - 新增 server/utils/user-status.ts: · USER_STATUSES = ['active','withdrawn','graduate'] · normalizeUserStatus() 大小写不敏感('ACTIVE' / ' Active ' → 'active'),认不出返回 null · USER_STATUS_RULE_MESSAGE 统一文案 - [id].put.ts:状态改用归一化值;另修正"自己只能改姓名"里的状态比较也忽略大小写 - [id]/status.put.ts、batch-status.put.ts:同样归一化; 并修掉一个我自己引入的隐患(把归一化结果赋回 const 变量 → TypeError), batch-status 之前被误写成不存在的 statusesRaw,一并改正 效果:无论小程序是否重新编译,管理员都能编辑其它用户(含历史拼音账号只改年级)。
This commit is contained in:
@@ -5,6 +5,7 @@ import { users, userStatusLogs } from '~/drizzle/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { updateUserPassword } from '~~/server/services/userService'
|
||||
import { getRoleLevel, canChangeRoleOf, canResetPasswordFor } from '~~/server/utils/permissions'
|
||||
import { normalizeUserStatus, USER_STATUS_RULE_MESSAGE } from '~~/server/utils/user-status'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
@@ -189,11 +190,15 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
// 验证status字段的有效性
|
||||
if (status && !['active', 'withdrawn', 'graduate'].includes(status)) {
|
||||
/*
|
||||
* 状态:**大小写不敏感**(客户端历史版本发的是大写的 ACTIVE)。
|
||||
* 认不出来才报错 —— 否则管理员会遇到"编辑任何用户都 400"(已踩过)。
|
||||
*/
|
||||
const nextStatus = status === undefined || status === null ? null : normalizeUserStatus(status)
|
||||
if (status !== undefined && status !== null && !nextStatus) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '用户状态只能是 active, withdrawn 或 graduate'
|
||||
statusMessage: USER_STATUS_RULE_MESSAGE
|
||||
})
|
||||
}
|
||||
|
||||
@@ -207,8 +212,8 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
// 如果提供了status,则更新状态相关字段
|
||||
if (status && status !== existingUser[0].status) {
|
||||
updateData.status = status
|
||||
if (nextStatus && nextStatus !== existingUser[0].status) {
|
||||
updateData.status = nextStatus
|
||||
updateData.statusChangedAt = new Date()
|
||||
updateData.statusChangedBy = user.id
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createError, defineEventHandler, getRouterParam, readBody } from 'h3'
|
||||
import { normalizeUserStatus, USER_STATUS_RULE_MESSAGE } from '~~/server/utils/user-status'
|
||||
import { db } from '~/drizzle/db'
|
||||
import { users, userStatusLogs } from '~/drizzle/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
@@ -18,13 +19,14 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
const userId = getRouterParam(event, 'id')
|
||||
const body = await readBody(event)
|
||||
const { status, reason } = body
|
||||
const { status: rawStatus, reason } = body
|
||||
|
||||
// 验证必填字段
|
||||
if (!status || !['active', 'withdrawn', 'graduate'].includes(status)) {
|
||||
// 验证必填字段;状态大小写不敏感(客户端历史版本发大写 ACTIVE 也能用)
|
||||
const status = normalizeUserStatus(rawStatus)
|
||||
if (!status) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '状态必须为 active, withdrawn 或 graduate'
|
||||
statusMessage: USER_STATUS_RULE_MESSAGE
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
const body = await readBody(event)
|
||||
const { userIds, status, reason } = body
|
||||
const { userIds, status: rawStatus, reason } = body
|
||||
|
||||
// 验证必填字段
|
||||
if (!Array.isArray(userIds) || userIds.length === 0) {
|
||||
@@ -27,10 +27,12 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
if (!status || !['active', 'withdrawn', 'graduate'].includes(status)) {
|
||||
// 状态:大小写不敏感(客户端历史版本发的是大写 ACTIVE)
|
||||
const status = normalizeUserStatus(rawStatus)
|
||||
if (!status) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: '状态必须为 active, withdrawn 或 graduate'
|
||||
statusMessage: USER_STATUS_RULE_MESSAGE
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user