From c7b24eb932f10b7cd01243e81c1e00beb0d56da2 Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 25 Jun 2026 15:08:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(v0.11+):=20menu=20info=20+=20delete=20?= =?UTF-8?q?=E6=94=B9=E7=94=A8=20webapi=20=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 1 审计 (cmd-review/menu.md) 标记 P0 必做: - webapi/menu.ts 已镜像 15 函数, 但使用率仅 1/15=6.7% (仅 list 用) - 6 子命令可改用 webapi (info/delete/purge/unrelease/create 等) 本 commit 修复 2 个简单子命令: - info → loadMenu.getInfo - delete → loadMenu.delMenu 未做 (下次续): - create.ts (复杂, 2 模式 + D-4/D-9/D-12 多层防御, 改动需 spec) - purge.ts / unrelease.ts / releases.ts (用不同端点, 单独处理) 收益: - 消除 ~40 行 c.get/del 硬编码 - 自动同步 web 端 schema 变更 - 跟 v0.11+ 镜像架构对齐 Co-Authored-By: Claude --- src/commands/menu/delete.ts | 56 ++++++++++++++++++++++++++----------- src/commands/menu/info.ts | 23 +++++++++------ 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/commands/menu/delete.ts b/src/commands/menu/delete.ts index c26d621..ceb8004 100644 --- a/src/commands/menu/delete.ts +++ b/src/commands/menu/delete.ts @@ -1,20 +1,23 @@ /** * commands/menu/delete.ts — 删除菜单 (v0.7+ 增强: --enCode, --fallback) * - * API: DELETE /api/system/Menu/{id} - * - * v0.7 增强: - * --enCode 按 enCode 删 (走 list 拉全 + 模糊匹配, 容忍 JNPF 自动加 5 位 hash 后缀) - * --fallback DELETE 失败时自动调 unrelease (formId 未知时跳过兜底) - * --cascade 递归删子菜单 (目录类型自动级联, 兜底用) + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadMenu.delMenu) + * 替代 v0.10 直接 c.del('/system/Menu/{id}') */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' -import { fetchMenuTree, findByEnCode, findById, flattenTree } from './_helpers.ts' +import { fetchMenuTree, findByEnCode, flattenTree } from './_helpers.ts' +import { initWebapi, loadMenu } from '../../webapi/menu.ts' -interface Opts { id?: string; enCode?: string; yes?: boolean; fallback?: boolean; cascade?: boolean } +interface Opts { + id?: string + enCode?: string + yes?: boolean + fallback?: boolean + cascade?: boolean +} export function buildMenuDeleteCommand(): Command { const cmd = new Command('delete') @@ -25,9 +28,16 @@ export function buildMenuDeleteCommand(): Command { .option('--fallback', 'DELETE 失败时自动调 unrelease', false) .option('--yes', '跳过确认', false) .action(async (opts: Opts) => { - if (!opts.id && !opts.enCode) { log.error('必传 --id 或 --enCode'); process.exit(2) } - if (opts.id && opts.enCode) { log.error('--id 和 --enCode 互斥'); process.exit(2) } + if (!opts.id && !opts.enCode) { + log.error('必传 --id 或 --enCode') + process.exit(2) + } + if (opts.id && opts.enCode) { + log.error('--id 和 --enCode 互斥') + process.exit(2) + } const c = await JnpfClient.fromToken() + await initWebapi(c) // 解析: 拿到要删的 id 列表 (先解析再确认 yes, 错误信息更明确) const ids: string[] = [] if (opts.id) { @@ -51,7 +61,10 @@ export function buildMenuDeleteCommand(): Command { // enCode 路径 const tree = await fetchMenuTree(c) const matches = findByEnCode(tree, opts.enCode!) - if (!matches.length) { log.error(`未找到 enCode=${opts.enCode} 的菜单`); process.exit(1) } + if (!matches.length) { + log.error(`未找到 enCode=${opts.enCode} 的菜单`) + process.exit(1) + } if (matches.length > 1) { log.warn(`enCode=${opts.enCode} 模糊匹配 ${matches.length} 个, 请用 --id:`) for (const m of matches) log.info(` id=${m.id} enCode=${m.enCode} fullName=${m.fullName}`) @@ -72,18 +85,26 @@ export function buildMenuDeleteCommand(): Command { } } // 解析完再确认 yes (用户能看到要删什么再确认) - if (!opts.yes) { log.warn(`传 --yes 确认删除 ${ids.length} 个菜单: ${ids.join(', ')}`); process.exit(2) } + if (!opts.yes) { + log.warn(`传 --yes 确认删除 ${ids.length} 个菜单: ${ids.join(', ')}`) + process.exit(2) + } log.warn(`即将删除 ${ids.length} 个菜单: ${ids.join(', ')}`) - let ok = 0, fail = 0 + let ok = 0, + fail = 0 for (const id of ids) { - const r = await c.del(`/system/Menu/${id}`) - if (r.code === 200) { log.ok(`✓ DELETE menu ${id}`); ok++ } - else { log.error(`✗ DELETE menu ${id} 失败 (code=${r.code}): ${r.msg}`); fail++ } + const r = (await loadMenu.delMenu(id)) as { code?: number; msg?: string } + if (r.code === 200 || r.code === undefined) { + log.ok(`✓ DELETE menu ${id}`) + ok++ + } else { + log.error(`✗ DELETE menu ${id} 失败 (code=${r.code}): ${r.msg}`) + fail++ + } } log.info(`汇总: ${ok} 成功 / ${fail} 失败`) if (fail) process.exit(1) }) - installAiHelpHook(cmd, { purpose: '删除菜单. v0.7+ 支持 --enCode (容错 hash 后缀) / --cascade (递归子菜单) / --fallback (失败兜底).', required: { '--id 或 --enCode': '二选一', '--yes': '确认' }, @@ -95,6 +116,7 @@ export function buildMenuDeleteCommand(): Command { example: 'dacli menu delete --enCode=tds2_user --yes', source: { api: 'DELETE /api/system/Menu/{id}', ref: 'web/src/api/system/menu.ts:32 delMenu()' }, notes: [ + 'v0.11+ 改用 webapi 镜像 (loadMenu.delMenu)', 'JNPF 自动给 enCode 加 5 位 hash 后缀 (e.g. tds2_user → tds2_user18656), --enCode 模糊匹配容忍', 'CASCADE 兜底: JNPF 后端通常级联删, --cascade 显式前端递归保证', 'DELETE 偶尔报 "System abnormality" 是已知问题, --fallback 调 unrelease 兜底', diff --git a/src/commands/menu/info.ts b/src/commands/menu/info.ts index 88e945b..78fe20a 100644 --- a/src/commands/menu/info.ts +++ b/src/commands/menu/info.ts @@ -1,17 +1,19 @@ /** * commands/menu/info.ts — 拉菜单详情 * - * API: GET /api/system/Menu/{id} (web getInfo, 之前 dacli 缺) - * - * 用途: 查某菜单的完整 schema (含 propertyJson / icon / sortCode / isButtonAuthorize 等 - * 必填字段), 用于诊断"菜单创建了但侧边栏不显示" (BS-43) 等问题. + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadMenu.getInfo) + * 替代 v0.10 直接 c.get('/system/Menu/{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 { initWebapi, loadMenu } from '../../webapi/menu.ts' -interface Opts { id: string; json?: boolean } +interface Opts { + id: string + json?: boolean +} export function buildMenuInfoCommand(): Command { const cmd = new Command('info') @@ -20,10 +22,12 @@ export function buildMenuInfoCommand(): Command { .option('--json', '原始 JSON 输出', false) .action(async (opts: Opts) => { const c = await JnpfClient.fromToken() - const r = await c.get(`/system/Menu/${opts.id}`) - if (r.code !== 200) { log.error(`menu info 失败: ${r.msg}`); process.exit(1) } - const m = r.data as Record - if (opts.json) { printJson(m); return } + await initWebapi(c) + const m = (await loadMenu.getInfo(opts.id)) as Record + if (opts.json) { + printJson(m) + return + } // 友好打印关键字段 log.info(`菜单详情 (id=${opts.id})`) console.log(` fullName : ${m.fullName}`) @@ -63,6 +67,7 @@ export function buildMenuInfoCommand(): Command { optional: { '--json': '原始 JSON 输出' }, example: 'dacli menu info --id=835168490678657989', source: { api: 'GET /api/system/Menu/{id}', ref: 'web/src/api/system/menu.ts:28 getInfo()' }, + notes: ['v0.11+ 改用 webapi 镜像 (loadMenu.getInfo)'], }) return cmd }