refactor(v0.11+): user 3 子命令 (list/create/delete) 改走 webapi 镜像 (loadUser)

- 新建 webapi/user.ts (25+ 函数, 镜像 web/api/permission/user.ts)
- user/list: c.get → loadUser.getUserList
- user/create: c.postJson+c.get → loadUser.createUser+getUserList
- user/delete: c.del → loadUser.delUser

user 18 子命令剩余 15 个 (auth-* / baseinfo / changepwd / resetpwd / set-position / online-kick 等), 待 webapi 化
(不紧急, 这些是次要场景)

webapi 模块累计 17 个

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-06-25 15:38:26 +08:00
1 parent cbe6a1e7d9
commit 7224156cf0
4 files changed
+64 -23

No files matched your search

+13 -12
View File
@@ -1,13 +1,15 @@
/** /**
* commands/user/create.ts — 新建用户 * commands/user/create.ts — 新建用户
* *
* API: POST /api/permission/Users * v0.11+ Phase 2: webapi 化 (loadUser.createUser + getUserList)
* 替代 v0.10 直接 c.postJson/c.get
*/ */
import { Command } from 'commander' import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts' import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts' import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts' import { log, printJson } from '../../core/log.ts'
import { encryptByMd5 } from '../../core/crypto.ts' import { encryptByMd5 } from '../../core/crypto.ts'
import { initWebapi, loadUser } from '../../webapi/user.ts'
interface CreateOpts { interface CreateOpts {
account: string account: string
@@ -23,7 +25,7 @@ interface CreateOpts {
export function buildUserCreateCommand(): Command { export function buildUserCreateCommand(): Command {
const cmd = new Command('create') const cmd = new Command('create')
.description('新建用户 (P1 权限三角 2 号)') .description('新建用户 (P1 权限三角 2 号, v0.11+ webapi)')
.requiredOption('--account <a>', '账号 (登录名)') .requiredOption('--account <a>', '账号 (登录名)')
.requiredOption('--realName <n>', '真实姓名') .requiredOption('--realName <n>', '真实姓名')
.requiredOption('--password <p>', '明文密码 (后端会自动 md5 加密)') .requiredOption('--password <p>', '明文密码 (后端会自动 md5 加密)')
@@ -35,6 +37,7 @@ export function buildUserCreateCommand(): Command {
.option('--json', '原始 JSON 输出', false) .option('--json', '原始 JSON 输出', false)
.action(async (opts: CreateOpts) => { .action(async (opts: CreateOpts) => {
const c = await JnpfClient.fromToken() const c = await JnpfClient.fromToken()
await initWebapi(c)
// JNPF 接收的 password 是 md5(plain) hex (32 字符), 不是 AES 加密 // JNPF 接收的 password 是 md5(plain) hex (32 字符), 不是 AES 加密
const md5Pwd = encryptByMd5(opts.password) const md5Pwd = encryptByMd5(opts.password)
const body: Record<string, unknown> = { const body: Record<string, unknown> = {
@@ -47,24 +50,22 @@ export function buildUserCreateCommand(): Command {
} }
if (opts.organizeId) body.organizeId = opts.organizeId if (opts.organizeId) body.organizeId = opts.organizeId
if (opts.description) body.description = opts.description if (opts.description) body.description = opts.description
const r = await c.postJson<{ id?: string }>('/permission/Users', body) log.info(`loadUser.createUser (account=${opts.account})`)
if (r.code !== 200) { const r = (await loadUser.createUser(body)) as { id?: string; code?: number; msg?: string; data?: { id?: string } }
log.error(`user create 失败 (code=${r.code}): ${r.msg}`) if (r.code !== 200 && r.code !== undefined) {
log.error(`user create 失败: ${r.msg}`)
process.exit(1) process.exit(1)
} }
let id = r.data?.id let id = r.id || r.data?.id
if (!id) { if (!id) {
const listR = await c.get<{ list: { id: string; account: string; realName: string }[] }>( const listR = (await loadUser.getUserList({ keyword: opts.account, pageSize: 50 })) as { list?: { id: string; account: string; realName: string }[] }
'/permission/Users', const found = (listR.list || []).find(
{ keyword: opts.account, pageSize: '50' },
)
const found = (listR.data?.list || []).find(
(d) => d.account === opts.account || d.realName === opts.realName, (d) => d.account === opts.account || d.realName === opts.realName,
) )
id = found?.id id = found?.id
} }
log.info(`✓ 用户创建成功 id=${id}`) log.info(`✓ 用户创建成功 id=${id}`)
if (opts.json) printJson({ ...(r.data as object), id }) if (opts.json) printJson({ ...(r.data as object || r), id })
}) })
installAiHelpHook(cmd, { installAiHelpHook(cmd, {
purpose: '新建用户 (P1 权限三角 2 号)', purpose: '新建用户 (P1 权限三角 2 号)',
+8 -4
View File
@@ -1,12 +1,14 @@
/** /**
* commands/user/delete.ts — 删除用户 * commands/user/delete.ts — 删除用户
* *
* API: DELETE /api/permission/Users/{id} * v0.11+ Phase 2: webapi 化 (loadUser.delUser)
* 替代 v0.10 直接 c.del
*/ */
import { Command } from 'commander' import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts' import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts' import { installAiHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts' import { log } from '../../core/log.ts'
import { initWebapi, loadUser } from '../../webapi/user.ts'
interface DeleteOpts { interface DeleteOpts {
id: string id: string
@@ -15,7 +17,7 @@ interface DeleteOpts {
export function buildUserDeleteCommand(): Command { export function buildUserDeleteCommand(): Command {
const cmd = new Command('delete') const cmd = new Command('delete')
.description('删除用户 (非超管)') .description('删除用户 (非超管, v0.11+ webapi)')
.requiredOption('--id <id>', '用户 id') .requiredOption('--id <id>', '用户 id')
.option('--yes', '跳过确认', false) .option('--yes', '跳过确认', false)
.action(async (opts: DeleteOpts) => { .action(async (opts: DeleteOpts) => {
@@ -24,8 +26,10 @@ export function buildUserDeleteCommand(): Command {
process.exit(1) process.exit(1)
} }
const c = await JnpfClient.fromToken() const c = await JnpfClient.fromToken()
const r = await c.del(`/permission/Users/${opts.id}`) await initWebapi(c)
if (r.code !== 200) { log.info(`loadUser.delUser (id=${opts.id})`)
const r = (await loadUser.delUser(opts.id)) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
log.error(`user delete 失败: ${r.msg}`) log.error(`user delete 失败: ${r.msg}`)
process.exit(1) process.exit(1)
} }
+13 -7
View File
@@ -1,12 +1,14 @@
/** /**
* commands/user/list.ts — 用户列表 * commands/user/list.ts — 用户列表
* *
* API: GET /api/permission/Users * v0.11+ Phase 2: webapi 化 (loadUser.getUserList)
* 替代 v0.10 直接 c.get
*/ */
import { Command } from 'commander' import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts' import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts' import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts' import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadUser } from '../../webapi/user.ts'
interface UserItem { interface UserItem {
id?: string id?: string
@@ -26,23 +28,27 @@ interface ListOpts {
export function buildUserListCommand(): Command { export function buildUserListCommand(): Command {
const cmd = new Command('list') const cmd = new Command('list')
.description('列出用户 (P1 权限三角 2 号)') .description('列出用户 (P1 权限三角 2 号, v0.11+ webapi)')
.option('--keyword <k>', '按账号/姓名/手机模糊搜索') .option('--keyword <k>', '按账号/姓名/手机模糊搜索')
.option('--json', '原始 JSON 输出', false) .option('--json', '原始 JSON 输出', false)
.action(async (opts: ListOpts) => { .action(async (opts: ListOpts) => {
const c = await JnpfClient.fromToken() const c = await JnpfClient.fromToken()
const r = await c.get<{ list: UserItem[] }>('/permission/Users', { await initWebapi(c)
const r = (await loadUser.getUserList({
keyword: opts.keyword, keyword: opts.keyword,
currentPage: 1, currentPage: 1,
pageSize: 50, pageSize: 50,
}) })) as { list?: UserItem[] }
if (r.code !== 200) { if (!r || (r as { code?: number }).code !== 200 && (r as { code?: number }).code !== undefined) {
log.error(`user list 失败 (code=${r.code}): ${r.msg}`) log.error(`user list 失败: ${(r as { msg?: string }).msg}`)
process.exit(1)
}
const list = r.list || []
process.exit(1) process.exit(1)
} }
const list = r.data?.list || [] const list = r.data?.list || []
if (opts.json) { if (opts.json) {
printJson(r.data) printJson(r)
return return
} }
log.info(`共 ${list.length} 个用户`) log.info(`共 ${list.length} 个用户`)
+30
View File
@@ -0,0 +1,30 @@
/**
* webapi/user.ts — 镜像 web 端 permission/user.ts (v0.11+ Phase 2)
*
* 镜像 web/apps/jnpf-web-apps-main/src/api/permission/user.ts 25+ 函数
* (含 list/selector/create/update/info/del/batchDelete/resetPassword/unlock/workHandover 等)
*/
import { JnpfClient } from '../core/client.ts'
import { loadWebModule } from './_loader.ts'
const USER_SRC = '/home/ubuntu/ccWorkDir/web/apps/jnpf-web-apps-main/src/api/permission/user.ts'
let cached: any = null
export async function initWebapi(client: JnpfClient): Promise<void> {
if (cached) return
const { initDefHttpShim } = await import('./_defhttp_shim.ts')
initDefHttpShim(client)
cached = await loadWebModule(USER_SRC)
}
export function isWebapiLoaded(): boolean {
return cached !== null
}
export const loadUser: any = new Proxy({} as any, {
get(_target, prop) {
if (!cached) throw new Error('user 模块未初始化, 请先 await initWebapi(client)')
return cached[prop]
},
})