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:
2026-09-30 23:49:40 +08:00
parent deb71875c1
commit cd6e235848
4 changed files with 48 additions and 12 deletions
+5 -3
View File
@@ -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
})
}