feat(dacli): 新增 dacli user info 子命令 (v3.0+ 双入口)
用户反馈: dacli app context --json 把 CurrentUser 集成进去后, 大 JSON (含 menuList/permissionList) 不方便调试. 用户决定: 做双入口, dacli user info 单独入口 (聚焦, 小 JSON). **新增 src/commands/user/info.ts**: - 无 id → CurrentUser (走 webapi/user.getUserInfoApi, 镜像 web/api/core/user.ts:10-15) - 有 id → 特定用户详情 (走 webapi/user.loadUser.getUserInfo, 镜像 web/api/permission/user.ts) - 选项 --backend <0|1>: CurrentUser 用, 默认 1 (与 web 端后台管理端一致) - 选项 --json: 原始 JSON 输出 (含 menuList/permissionList) - 默认输出聚焦字段: userId/userAccount/userName/isAdmin/systemCode/systemId/hasBackend **改 src/commands/user/index.ts**: - 注册 buildUserInfoCommand (放最前, --help 优先显示) - 父命令 description 加 v3.0+ 双入口标记 - 现有 19 个子命令 (list/create/update/...) 完全保留 **双入口设计 (v3.0+)**: - dacli user info — 聚焦, 小 JSON, 调试 / 单命令调用 - dacli app context --json — 含 currentUser, 一站式 AI 工作流 - AI 可按场景选择入口 **应用隔离 (v3 §4)**: - systemId 从 token.json 自动读 (HTTP App-Code 头) - 不需要手动传 --appCode - 与 web 端 user.ts 语义对齐 **实测**: - dacli user info → userId=349057407209541, userAccount=admin, systemCode=jxcj ✓ - dacli user info --json → 完整响应 (menuList 12 个菜单 + permissionList) ✓ - dacli app context --json → currentUser 字段仍正常 ✓ 测试: TS 编译干净 + 19 子命令 --help 正常 + user info 实测通过 详见 docs/cmd-review/regression-2026-06-29.md §4.2 CurrentUser 集成
This commit is contained in:
1 parent
2350da48f9
commit
a0a51afc78
2 files changed
+145
-3
No files matched your search
@@ -1,19 +1,29 @@
|
||||
/**
|
||||
* commands/user/index.ts — user 父命令
|
||||
* commands/user/index.ts — user 父命令 (v3.0+ 双入口)
|
||||
*
|
||||
* JNPF 用户 (permission/user)
|
||||
*
|
||||
* 决策 7: 用户是 P1 权限三角 2 号 (必须绑组织)
|
||||
*
|
||||
* 子命令 (v18):
|
||||
* 子命令 (v18 + v3.0+ 双入口):
|
||||
* 基础: list / create / update / delete / resetpwd / unlock
|
||||
* 批量: batch-state / batch-delete / batch-set-position
|
||||
* 岗位: get-position / set-position
|
||||
* 高级: workHandover / socials-unbind / online-kick
|
||||
* self: changepwd / baseinfo
|
||||
* auth: auth-groups / auth-list
|
||||
* **v3.0+ 新增**: info (CurrentUser 服务器确认 + 特定用户详情, 镜像 web/api/core/user.ts)
|
||||
*
|
||||
* v3.0+ 双入口设计:
|
||||
* - `dacli user info` (本命令, 聚焦, 小 JSON, 调试用)
|
||||
* - `dacli app context --json` 也包含 currentUser (一站式 AI 工作流)
|
||||
*
|
||||
* v3.0+ 应用隔离:
|
||||
* - systemId 从 token.json 自动读 (HTTP App-Code 头, BS-49 全平台唯一性)
|
||||
* - 不需要手动传 --appCode
|
||||
*/
|
||||
import { Command } from 'commander'
|
||||
import { buildUserInfoCommand } from './info.ts' // v3.0+ 新增
|
||||
import { buildUserListCommand } from './list.ts'
|
||||
import { buildUserCreateCommand } from './create.ts'
|
||||
import { buildUserUpdateCommand } from './update.ts'
|
||||
@@ -34,7 +44,9 @@ import { buildUserAuthGroupsCommand } from './auth-groups.ts'
|
||||
import { buildUserAuthListCommand } from './auth-list.ts'
|
||||
|
||||
export function buildUserCommand(): Command {
|
||||
const cmd = new Command('user').description('用户 (P1 权限三角 2 号, 登录主体)')
|
||||
const cmd = new Command('user').description('用户 (P1 权限三角 2 号, 登录主体, v3.0+ 双入口 info)')
|
||||
// v3.0+ 双入口 (放最前, 让 --help 优先显示)
|
||||
cmd.addCommand(buildUserInfoCommand())
|
||||
// 基础
|
||||
cmd.addCommand(buildUserListCommand())
|
||||
cmd.addCommand(buildUserCreateCommand())
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* commands/user/info.ts — 用户信息查询 (v3.0+ 新增, 双入口)
|
||||
*
|
||||
* v3.0+ (2026-06-29) 新增原因:
|
||||
* - 用户反馈: 浏览器监测 /api/oauth/CurrentUser?systemCode=&isBackend= 是关键请求
|
||||
* - 此 API 同时验证 token + appCode + 用户身份 (服务器端确认)
|
||||
* - 走 webapi/user.getUserInfoApi (镜像 web/api/core/user.ts:10-15)
|
||||
*
|
||||
* 双入口设计 (v3.0+):
|
||||
* - `dacli user info` — 本命令 (聚焦, 小 JSON, 调试用)
|
||||
* - `dacli app context --json` 也包含 currentUser (一站式 AI 工作流)
|
||||
*
|
||||
* 子命令:
|
||||
* info (无 id) — CurrentUser (服务器确认的当前登录用户, 默认 isBackend=1)
|
||||
* info <id> — 特定用户详情 (走 webapi/permission/user getUserInfo)
|
||||
*
|
||||
* API:
|
||||
* - GET /api/oauth/CurrentUser?systemCode=&isBackend=&_t= (无 id)
|
||||
* - GET /api/permission/User/{id} (有 id)
|
||||
*/
|
||||
import { Command } from 'commander'
|
||||
import { JnpfClient } from '../../core/client.ts'
|
||||
import { installAiHelpHook } from '../../core/help.ts'
|
||||
import { log, printJson } from '../../core/log.ts'
|
||||
import { loadToken } from '../../core/config.ts'
|
||||
import { getUserInfoApi } from '../../webapi/user.ts'
|
||||
import { initWebapi as initUserWebapi, loadUser } from '../../webapi/user.ts'
|
||||
|
||||
interface InfoOpts {
|
||||
/** 后台管理端标志 (0=前台 1=后台), 默认 1 与 web 端后台管理端一致 */
|
||||
backend?: string
|
||||
/** JSON 输出 */
|
||||
json?: boolean
|
||||
}
|
||||
|
||||
export function buildUserInfoCommand(): Command {
|
||||
const cmd = new Command('info')
|
||||
.description('用户信息查询 (v3.0+ 双入口: 无 id 走 CurrentUser, 有 id 走 web getUserInfo)')
|
||||
.argument('[id]', '用户 id (省略则查 CurrentUser)')
|
||||
.option('--backend <0|1>', '后台管理端标志 (CurrentUser 用, 0=前台 1=后台, 默认 1)', '1')
|
||||
.option('--json', '原始 JSON 输出', false)
|
||||
.action(async (id: string | undefined, opts: InfoOpts) => {
|
||||
const c = await JnpfClient.fromToken()
|
||||
|
||||
// 模式 1: 有 id → 查特定用户详情 (走 webapi/permission/user getUserInfo)
|
||||
if (id) {
|
||||
log.info(`查询用户详情: id=${id}`)
|
||||
await initUserWebapi(c)
|
||||
const r = (await loadUser.getUserInfo(id)) as Record<string, unknown>
|
||||
if (opts.json) {
|
||||
printJson(r)
|
||||
return
|
||||
}
|
||||
log.ok(`✓ 用户详情:`)
|
||||
console.log(` id: ${r.id || '-'}`)
|
||||
console.log(` account: ${r.account || '-'}`)
|
||||
console.log(` realName: ${r.realName || '-'}`)
|
||||
console.log(` roleName: ${r.roleName || '-'}`)
|
||||
console.log(` organize: ${r.organize || '-'}`)
|
||||
console.log(` enabledMark: ${r.enabledMark === undefined ? '-' : r.enabledMark}`)
|
||||
return
|
||||
}
|
||||
|
||||
// 模式 2: 无 id → 查 CurrentUser (服务器确认当前用户)
|
||||
const token = loadToken()
|
||||
const appCode = token?.appCode || ''
|
||||
if (!appCode) {
|
||||
log.error('无 token 或 appCode, 无法调 CurrentUser')
|
||||
log.info(` 请先: dacli app login --account=admin --password=123456 --appCode=<code>`)
|
||||
process.exit(1)
|
||||
}
|
||||
const isBackend = parseInt(opts.backend || '1', 10)
|
||||
log.info(`查询 CurrentUser: appCode=${appCode}, isBackend=${isBackend}`)
|
||||
|
||||
try {
|
||||
const u = await getUserInfoApi(c, { systemCode: appCode, isBackend })
|
||||
if (opts.json) {
|
||||
printJson(u)
|
||||
return
|
||||
}
|
||||
log.ok(`✓ CurrentUser (服务器确认):`)
|
||||
const ui = (u as Record<string, unknown>).userInfo as Record<string, unknown> | undefined
|
||||
if (ui) {
|
||||
console.log(` userId: ${ui.userId || ui.id || '-'}`)
|
||||
console.log(` userAccount: ${ui.userAccount || ui.account || '-'}`)
|
||||
console.log(` userName: ${ui.userName || ui.realName || '-'}`)
|
||||
console.log(` isAdmin: ${ui.isAdministrator || '-'}`)
|
||||
console.log(` systemCode: ${ui.systemCode || appCode}`)
|
||||
console.log(` systemId: ${ui.systemId || '-'}`)
|
||||
console.log(` hasBackend: ${ui.hasBackend || '-'}`)
|
||||
} else {
|
||||
console.log(' (响应无 userInfo 字段, 完整响应见 --json)')
|
||||
printJson(u)
|
||||
}
|
||||
console.log('')
|
||||
log.dim(` 完整响应含 menuList/permissionList/sysConfigInfo, 用 --json 查看`)
|
||||
} catch (e) {
|
||||
log.error(`✗ CurrentUser 失败: ${(e as Error).message}`)
|
||||
log.info(` 可能原因: token 过期 (600/601/602) / appCode 不存在`)
|
||||
process.exit(1)
|
||||
}
|
||||
})
|
||||
|
||||
installAiHelpHook(cmd, {
|
||||
purpose: '用户信息查询 (v3.0+ 双入口: 无 id 走 CurrentUser, 有 id 走 web getUserInfo)',
|
||||
optional: {
|
||||
'<id>': '用户 id (省略则查 CurrentUser)',
|
||||
'--backend': '后台管理端标志 (CurrentUser 用, 默认 1)',
|
||||
'--json': '原始 JSON 输出',
|
||||
},
|
||||
example: [
|
||||
'dacli user info # CurrentUser (服务器确认当前登录用户)',
|
||||
'dacli user info 349057407209541 # 特定用户详情 (web getUserInfo)',
|
||||
'dacli user info --json # JSON 输出',
|
||||
].join('\n'),
|
||||
source: {
|
||||
web: 'web/api/core/user.ts:10-15 getUserInfoApi + web/api/permission/user.ts getUserInfo',
|
||||
api: 'GET /api/oauth/CurrentUser?systemCode=&isBackend= (无 id) / GET /api/permission/User/{id} (有 id)',
|
||||
},
|
||||
notes: [
|
||||
'v3.0+ 双入口: 本命令 (聚焦, 小 JSON) + dacli app context (含 currentUser, 一站式)',
|
||||
'**镜像 web 端**: webapi/user.getUserInfoApi (CurrentUser) + loadUser.getUserInfo (特定用户)',
|
||||
'**CurrentUser 关键性**: 同时验证 token + appCode + 用户身份, 比读本地 token 更可靠',
|
||||
'**webapi 路径**: 不直接 curl, 走 webapi 镜像 (BS-47 全平台一致性)',
|
||||
'**App-Code 头**: 自动从 token.json 读 appCode, 不需要 --appCode 参数',
|
||||
],
|
||||
})
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in new issue
Block a user