Files
dacli/src/commands/user/create.ts
T

80 lines
2.9 KiB
TypeScript

/**
* commands/user/create.ts — 新建用户
*
* API: POST /api/permission/Users
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { encryptByMd5 } from '../../core/crypto.ts'
interface CreateOpts {
account: string
realName: string
password: string
organizeId?: string
gender?: string
json?: boolean
}
export function buildUserCreateCommand(): Command {
const cmd = new Command('create')
.description('新建用户 (P1 权限三角 2 号)')
.requiredOption('--account <a>', '账号 (登录名)')
.requiredOption('--realName <n>', '真实姓名')
.requiredOption('--password <p>', '明文密码 (后端会自动 md5 加密)')
.option('--organizeId <id>', '所属组织 id (决策 7: 必须)')
.option('--gender <g>', '性别: 1=男 2=女 3=未知', '1')
.option('--json', '原始 JSON 输出', false)
.action(async (opts: CreateOpts) => {
const c = await JnpfClient.fromToken()
// JNPF 接收的 password 是 md5(plain) hex (32 字符), 不是 AES 加密
const md5Pwd = encryptByMd5(opts.password)
const body: Record<string, unknown> = {
account: opts.account,
realName: opts.realName,
password: md5Pwd,
gender: opts.gender || '1',
enabledMark: 1,
sortCode: 0,
}
if (opts.organizeId) body.organizeId = opts.organizeId
const r = await c.postJson<{ id?: string }>('/permission/Users', body)
if (r.code !== 200) {
log.error(`user create 失败 (code=${r.code}): ${r.msg}`)
process.exit(1)
}
let id = r.data?.id
if (!id) {
const listR = await c.get<{ list: { id: string; account: string; realName: string }[] }>(
'/permission/Users',
{ keyword: opts.account, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.account === opts.account || d.realName === opts.realName,
)
id = found?.id
}
log.info(`✓ 用户创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id })
})
installAiHelpHook(cmd, {
purpose: '新建用户 (P1 权限三角 2 号)',
required: { '--account': '账号', '--realName': '真实姓名', '--password': '明文密码' },
optional: { '--organizeId': '所属组织 id (决策 7 必填)' },
example:
'dacli user create --account=test001 --realName="测试用户" --password=123456 --organizeId=<orgId>',
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/permission/user/Form.vue',
api: 'POST /api/permission/Users',
ref: 'doc/v0.8-endpoints.md §2.2',
},
notes: [
'**决策 7**: 用户必须绑组织 (organizeId 必填), 否则报"组织不存在"',
'**web Form.vue**: password = md5(plain), 不是 AES 加密 (与 datasource 不同)',
],
})
return cmd
}