diff --git a/src/commands/user/index.ts b/src/commands/user/index.ts index 2b18ccc..30f73bd 100644 --- a/src/commands/user/index.ts +++ b/src/commands/user/index.ts @@ -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()) diff --git a/src/commands/user/info.ts b/src/commands/user/info.ts new file mode 100644 index 0000000..958e5d1 --- /dev/null +++ b/src/commands/user/info.ts @@ -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 — 特定用户详情 (走 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 + 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=`) + 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).userInfo as Record | 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 (省略则查 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 +} \ No newline at end of file