task-45(账号/权限页面): 实现新建用户表单模型

新增 user-create-model.ts 纯模型:role 仅 normal|admin(未知/超管归一 normal,
镜像后端白名单)、用户名≥2/密码≥6 镜像校验、toCreateUserRequest 去空格/拷
columnIds,字段对齐 Java AdminUserCreateRequest(camelCase)。8 用例全过,
323 单测 + build 绿。
This commit is contained in:
2026-09-05 14:55:11 +08:00
parent 1ae4bb2a36
commit dad9cda4ac
2 changed files with 142 additions and 0 deletions
@@ -0,0 +1,65 @@
/** 新建用户表单模型(任务 45):纯逻辑,镜像 Java AdminUserCreateRequest 字段与后端校验。 */
export const CREATE_USERNAME_MIN_LEN = 2
export const CREATE_PASSWORD_MIN_LEN = 6
/** 新建用户可选角色:super_admin 不可由表单创建(后端只收 admin|normal)。 */
export type CreatableUserRole = 'normal' | 'admin'
export interface CreateUserForm {
username: string
password: string
role: CreatableUserRole
/** 授权菜单 id(后端 replaceDirectPermissions 用)。 */
columnIds: number[]
}
export interface CreateUserFormErrors {
username?: string
password?: string
}
/** 发送 POST /api/admin/user 的请求体(与 Java AdminUserCreateRequest camelCase 对齐)。 */
export interface CreateUserPayload {
username: string
password: string
role: string
columnIds: number[]
}
export function createEmptyCreateUserForm(): CreateUserForm {
return { username: '', password: '', role: 'normal', columnIds: [] }
}
/** 未知/空白/超管角色一律归一到 normal,与后端 "admin"/"normal" 白名单一致。 */
export function normalizeCreateRole(role: string | null | undefined): CreatableUserRole {
return (role || '').trim().toLowerCase() === 'admin' ? 'admin' : 'normal'
}
/** 表单校验(镜像后端:用户名≥2、密码≥6、均必填);返回逐字段错误。 */
export function validateCreateUserForm(form: CreateUserForm): { valid: boolean; errors: CreateUserFormErrors } {
const errors: CreateUserFormErrors = {}
const username = (form.username || '').trim()
if (!username) {
errors.username = '用户名不能为空'
} else if (username.length < CREATE_USERNAME_MIN_LEN) {
errors.username = `用户名至少${CREATE_USERNAME_MIN_LEN}个字符`
}
const password = form.password || ''
if (!password) {
errors.password = '密码不能为空'
} else if (password.length < CREATE_PASSWORD_MIN_LEN) {
errors.password = `密码至少${CREATE_PASSWORD_MIN_LEN}个字符`
}
return { valid: Object.keys(errors).length === 0, errors }
}
/** 表单 → 创建请求体:用户名去空格、角色归一、菜单 id 拷贝不共享引用。 */
export function toCreateUserRequest(form: CreateUserForm): CreateUserPayload {
return {
username: (form.username || '').trim(),
password: form.password || '',
role: normalizeCreateRole(form.role),
columnIds: Array.isArray(form.columnIds) ? form.columnIds.slice() : [],
}
}