Files
crawler-plugin/admin-frontend-vue/src/api/users-model.ts
T
huangzd1997 1119c9a41f feat(停用账号): 停用/删除即强制下线并点名成因——写入口、X-Auth-Reason 头、三面接线
- Java:AdminUserUpdateRequest 加 disabled 开关(不能停自己、归属门沿用既有闸),
  AccountExpiryService/AdminAuthSupport 抛停用/删除子类型,GlobalExceptionHandler 打
  X-Auth-Reason: disabled|deleted 响应头;业务码保持 401 不变,不惊动三个既有消费者
- 后台管理:用户页停用/解除停用按钮 + 已停用徽章;拦截器把成因随 &auth= 带给登录页,
  登录页分停用/删除两种文案;源码钉子棘轮基线按正路重生成
- 用户端:kick-handler 按 X-Auth-Reason 分支亮停用/删除标记(互斥清理),
  登录页两段专属文案且各自关自动登录;跳转目标仍是 /login?expired=1
- 反假绿:摘掉成因头注入实测 2/4 红(变异自证),还原等值后复跑回绿
2026-10-09 16:35:35 +08:00

80 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** 用户 API 响应解析模型:列表分页(任务 42)与新建用户(任务 46)负载归一。 */
import { unwrap } from './envelope.ts'
import { emptyUserPageResult, type UserPageResult } from './users-dto.ts'
import type { AdminUser } from '../types/admin'
function text(value: unknown): string {
return typeof value === 'string' ? value : ''
}
function numberOrNull(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? value : null
}
/** 把 Java AdminUserItemVo(snake_case) 映射为前端 AdminUser(camelCase);缺 id 视为无效。 */
export function toAdminUserItem(raw: unknown): AdminUser | null {
if (!raw || typeof raw !== 'object') return null
const r = raw as Record<string, unknown>
const id = numberOrNull(r.id)
if (id === null) return null
const item: AdminUser = { id, username: text(r.username), role: text(r.role) }
if (typeof r.is_admin === 'boolean') item.isAdmin = r.is_admin
const createdById = numberOrNull(r.created_by_id)
if (createdById !== null) item.createdById = createdById
const createdAt = text(r.created_at)
if (createdAt) item.createdAt = createdAt
const updatedAt = text(r.updated_at)
if (updatedAt) item.updatedAt = updatedAt
const creator = text(r.creator_username)
if (creator) item.creatorUsername = creator
const expiresAt = text(r.expires_at)
if (expiresAt) item.expiresAt = expiresAt
const disabledAt = text(r.disabled_at)
if (disabledAt) item.disabledAt = disabledAt
const abbr = text(r.pinyin_abbr)
if (abbr) item.pinyinAbbr = abbr
return item
}
/**
* 归一化 Java 用户列表负载(data:{items,total,page,page_size} 或已解包 VO)为前端
* UserPageResult;空/缺省字段回默认,success=false 抛后端 message。
*/
export function parseUserPage(payload: unknown): UserPageResult {
const out = emptyUserPageResult()
const core = unwrap<unknown>(payload)
if (!core || typeof core !== 'object') return out
const record = core as Record<string, unknown>
if (Array.isArray(record.items)) {
out.items = record.items
.map((raw) => toAdminUserItem(raw))
.filter((item): item is AdminUser => item !== null)
}
if (typeof record.total === 'number') out.total = record.total
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
const rawSize = record.page_size ?? record.pageSize
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
if (typeof record.current_user_id === 'number') out.currentUserId = record.current_user_id
if (typeof record.current_user_username === 'string') out.currentUserUsername = record.current_user_username
if (Array.isArray(record.admins)) {
out.admins = record.admins
.map((raw) => {
if (!raw || typeof raw !== 'object') return null
const r = raw as Record<string, unknown>
const id = numberOrNull(r.id)
if (id === null) return null
return { id, username: text(r.username) }
})
.filter((a): a is { id: number; username: string } => a !== null)
}
return out
}
/** 解包新建用户响应并取回新用户 id(POST /api/admin/user 的 data 为 userId)。 */
export function parseCreatedUserId(payload: unknown): number {
const data = unwrap<unknown>(payload)
const id = typeof data === 'number' && Number.isFinite(data) ? Math.floor(data) : null
if (id === null || id < 1) throw new Error('创建用户响应异常:未返回有效的用户 ID')
return id
}