/** 新建用户表单模型(任务 45):纯逻辑,镜像 Java AdminUserCreateRequest 字段与后端校验。 */ // 相对路径而非 @/ 别名:本文件被 node --test 直接 import,别名只有 vite 认。 import { COMPANY_STATUS_ACTIVE, type CompanyItem } from '../../api/companies-dto.ts' export const CREATE_USERNAME_MIN_LEN = 2 export const CREATE_PASSWORD_MIN_LEN = 6 /** 新建用户可选角色:super_admin 不可由表单创建(后端只收 admin|normal)。 */ export type CreatableUserRole = 'normal' | 'admin' /** 当前操作者的公司归属(来自 /api/admin/current-user)。 */ export interface CreateOperatorScope { role?: string | null companyId?: number | null } export interface CreateUserForm { username: string password: string role: CreatableUserRole /** 授权菜单 id(后端 replaceDirectPermissions 用)。 */ columnIds: number[] /** 所属管理员 id(超管创建普通账号时可指定,对齐 admin 的 created_by_id)。 */ createdBy?: number | null /** 所属公司 id(V143)。超管必选;公司内 admin 由后端强制落自己公司。 */ companyId?: number | null } export interface CreateUserFormErrors { username?: string password?: string company?: string } /** 发送 POST /api/admin/user 的请求体(与 Java AdminUserCreateRequest camelCase 对齐)。 */ export interface CreateUserPayload { username: string password: string role: string columnIds: number[] /** 仅 role=normal 且选了所属管理员时发送,与参考 admin.js 的 created_by_id 语义一致。 */ createdById?: number companyId?: number } export function createEmptyCreateUserForm(): CreateUserForm { return { username: '', password: '', role: 'normal', columnIds: [], createdBy: null, companyId: null } } /** 未知/空白/超管角色一律归一到 normal,与后端 "admin"/"normal" 白名单一致。 */ export function normalizeCreateRole(role: string | null | undefined): CreatableUserRole { return (role || '').trim().toLowerCase() === 'admin' ? 'admin' : 'normal' } function positiveInt(value: unknown): number | null { return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : null } /** * 建号能选哪家公司,口径与后端 AdminUserService#resolveTargetCompanyId 一致: * 超管 = 全部启用中的公司(后端 requireActive 会拒停用公司); * 其余角色 = 只有自己也那一家,且不看启停——后端对非超管直接落 operator.companyId、 * 不校验状态,前端若跟着过滤就会出现「下拉没得选、提交却能建号」的自相矛盾。 */ export function selectableCompanies(companies: CompanyItem[], operator: CreateOperatorScope): CompanyItem[] { const rows = Array.isArray(companies) ? companies : [] if (operator?.role === 'super_admin') { return rows.filter((company) => company.status === COMPANY_STATUS_ACTIVE) } const own = positiveInt(operator?.companyId) if (own === null) return [] return rows.filter((company) => company.id === own) } /** * 弹窗打开时预选哪家公司:候选只有一个(公司内 admin 的本公司)就落它; * 多个(超管)返回 null——给超管填默认值等于替他决定账号挂在哪家,这是会写进库的判断。 */ export function initialCompanyId(operator: CreateOperatorScope, companies: CompanyItem[]): number | null { const options = selectableCompanies(companies, operator) return options.length === 1 ? options[0].id : null } /** 表单校验(镜像后端:用户名≥2、密码≥6、均必填;超管必须显式选公司);返回逐字段错误。 */ export function validateCreateUserForm( form: CreateUserForm, operator?: CreateOperatorScope, ): { 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}个字符` } if (operator?.role === 'super_admin' && positiveInt(form.companyId) === null) { errors.company = '请选择所属公司' } return { valid: Object.keys(errors).length === 0, errors } } /** 表单 → 创建请求体:用户名去空格、角色归一、菜单 id 拷贝不共享引用; * createdById 仅在 role=normal 且选了所属管理员时携带(对齐 admin.js 的 created_by_id 语义); * companyId 选了才带,未选时由后端落操作者自己公司。 */ export function toCreateUserRequest(form: CreateUserForm): CreateUserPayload { const payload: CreateUserPayload = { username: (form.username || '').trim(), password: form.password || '', role: normalizeCreateRole(form.role), columnIds: Array.isArray(form.columnIds) ? form.columnIds.slice() : [], } if (payload.role === 'normal' && form.createdBy != null) { payload.createdById = form.createdBy } const companyId = positiveInt(form.companyId) if (companyId !== null) payload.companyId = companyId return payload }