74 lines
3.2 KiB
TypeScript
74 lines
3.2 KiB
TypeScript
/** 用户 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 creator = text(r.creator_username)
|
||
if (creator) item.creatorUsername = creator
|
||
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
|
||
}
|