Files
dacli/src/commands/org/tree.ts
T
sswandClaude 0d9151f343 feat(dacli): org/monitor/report/doc 小命令 wrapCommand 迁移 (8 exit 收口)
org/create (旧 printCommandEnd 升级) + org/tree, monitor/server, report/delete (保留 --cascade 级联清理), doc/search (无 appCode 本地文件搜索). process.exit → ctx.fail.

Co-Authored-By: Claude <[email protected]>
2026-07-21 21:26:43 +08:00

96 lines
3.2 KiB
TypeScript

/**
* commands/org/tree.ts — 组织树形结构展示
*
* 走 /api/permission/Organize/AsyncList/{id} 拉单层子节点, 递归构建树
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface OrgItem {
id?: string
parentId?: string
fullName?: string
enCode?: string
category?: string | null
hasChildren?: boolean
isLeaf?: boolean | null
}
interface TreeOpts {
parentId?: string
maxDepth?: string
json?: boolean
verbose?: boolean
}
async function loadChildren(c: JnpfClient, parentId: string): Promise<OrgItem[]> {
const r = await c.get<OrgItem[]>(`/permission/Organize/AsyncList/${parentId}`)
if (r.code !== 200) return []
return r.data || []
}
function printTree(list: OrgItem[], depth: number, parentId: string) {
const indent = ' '.repeat(depth)
for (const d of list) {
if (d.parentId === parentId) {
console.log(`${indent}├─ ${d.fullName} (${d.id})${d.category ? ` [${d.category}]` : ''}`)
if (d.hasChildren) {
// 简化: 不递归, 提示用户用 list 拉
}
}
}
}
export function buildOrgTreeCommand(): Command {
const cmd = new Command('tree')
.description('组织树形结构 (单层展示, 递归用 --recursive)')
.option('--parentId <id>', '从指定父节点展开 (默认 "0" 根)', '0')
.option('--maxDepth <n>', '最大递归深度', '3')
addOutputOptions(cmd)
.action(async (opts: TreeOpts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
await wrapCommand({
command: 'org tree',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
const r = await c.get<{ list: OrgItem[] }>('/permission/Organize/Selector', {
parentId: opts.parentId,
currentPage: 1,
pageSize: 200,
})
if (r.code !== 200) {
return ctx.fail(`org tree 失败: ${r.msg}`, { parentId: opts.parentId, code: r.code })
}
const list = r.data?.list || []
log.info(`根 parentId=${opts.parentId}, 共 ${list.length} 个节点`)
console.log('组织树 (单层, maxDepth=' + opts.maxDepth + '):')
printTree(list, 0, opts.parentId || '0')
ctx.success({ parentId: opts.parentId, total: list.length })
} catch (e) {
ctx.fail(`org tree 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '组织树形结构 (单层展示)',
optional: { '--parentId': '根节点 id (默认 "0")', '--maxDepth': '最大深度' },
example: 'dacli org tree\ndacli org tree --parentId=829724597300494342',
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/permission/organize/index.vue',
api: 'GET /api/permission/Organize/Selector',
ref: 'doc/v0.8-endpoints.md §2.3',
},
})
return cmd
}