feat(user): v0.11e+ wire up _validate.ts + enhanceFromBackend

user/create.ts (Phase C1 集成):
- import validateUserCreate + enhanceFromBackend/formatErrorLine
- 加 --roleId 选项 (PS012 必填, 镜像 web user/Form.vue)
- 跑 step machine (2 step: 基础信息 + BS-47 唯一性)
- catch 后端错时 enhanceFromBackend

selfcheck 6/15 pass (15 项新增机制中 6 项符合, 9 项需后续补)
This commit is contained in:
ssw committed 2026-06-27 03:34:56 +08:00
1 parent 4b6fc825a1
commit 7e809c56b4
1 file changed
+28 -2
+28 -2
View File
@@ -10,6 +10,8 @@ import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { encryptByMd5 } from '../../core/crypto.ts'
import { initWebapi, loadUser } from '../../webapi/user.ts'
import { validateUserCreate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
interface CreateOpts {
account: string
@@ -20,12 +22,13 @@ interface CreateOpts {
enabledMark?: string
sortCode?: string
description?: string
roleId?: string
json?: boolean
}
export function buildUserCreateCommand(): Command {
const cmd = new Command('create')
.description('新建用户 (P1 权限三角 2 号, v0.11+ webapi)')
.description('新建用户 (P1 权限三角 2 号, v0.11+ webapi + v0.11e+ step machine)')
.requiredOption('--account <a>', '账号 (登录名)')
.requiredOption('--realName <n>', '真实姓名')
.requiredOption('--password <p>', '明文密码 (后端会自动 md5 加密)')
@@ -34,10 +37,31 @@ export function buildUserCreateCommand(): Command {
.option('--enabledMark <m>', '状态: 0=禁用 1=启用 2=锁定', '1')
.option('--sortCode <n>', '排序', '0')
.option('--description <d>', '说明')
.option('--roleId <id>', 'v0.11e+ Phase C1: 角色 id (PS012 必填, 镜像 web user/Form.vue)')
.option('--json', '原始 JSON 输出', false)
.action(async (opts: CreateOpts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
// v0.11e+ Phase C1: step machine 校验 (镜像 web user/Form.vue + PS007/008/012/204/EXIST006)
const validation = validateUserCreate({
account: opts.account,
fullName: opts.realName,
password: opts.password,
roleId: opts.roleId,
enabledMark: opts.enabledMark,
isCreate: true,
})
if (!validation.ok) {
for (const s of validation.steps) {
for (const e of s.errors) {
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
}
}
process.exit(1)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
// JNPF 接收的 password 是 md5(plain) hex (32 字符), 不是 AES 加密
const md5Pwd = encryptByMd5(opts.password)
const body: Record<string, unknown> = {
@@ -50,10 +74,12 @@ export function buildUserCreateCommand(): Command {
}
if (opts.organizeId) body.organizeId = opts.organizeId
if (opts.description) body.description = opts.description
if (opts.roleId) body.roleId = opts.roleId.split(',')
log.info(`loadUser.createUser (account=${opts.account})`)
const r = (await loadUser.createUser(body)) as { id?: string; code?: number; msg?: string; data?: { id?: string } }
if (r.code !== 200 && r.code !== undefined) {
log.error(`user create 失败: ${r.msg}`)
const enhanced = enhanceFromBackend(r.msg || '', 'user', { entity: 'user' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
process.exit(1)
}
let id = r.id || r.data?.id