diff --git a/src/commands/user/create.ts b/src/commands/user/create.ts index 3c4c284..14a559a 100644 --- a/src/commands/user/create.ts +++ b/src/commands/user/create.ts @@ -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 ', '账号 (登录名)') .requiredOption('--realName ', '真实姓名') .requiredOption('--password

', '明文密码 (后端会自动 md5 加密)') @@ -34,10 +37,31 @@ export function buildUserCreateCommand(): Command { .option('--enabledMark ', '状态: 0=禁用 1=启用 2=锁定', '1') .option('--sortCode ', '排序', '0') .option('--description ', '说明') + .option('--roleId ', '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 = { @@ -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