From eb3cac1dba82fa2bc70710c0c36c99c544b14785 Mon Sep 17 00:00:00 2001 From: ssw Date: Sat, 27 Jun 2026 22:26:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(menu):=20BS-19=20menu=20create=20=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E7=9C=9F=E5=AE=9E=20id=20(B-15=20=E6=89=A9=E5=B1=95,?= =?UTF-8?q?=20web=20Form.vue=20=E5=88=9B=E5=BB=BA=E5=90=8E=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E5=88=97=E8=A1=A8=E5=AF=B9=E9=BD=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **4 步法实施**: - 步骤 1 读源码: dacli/src/commands/dict/_lookup.ts:21-40 (B-15 lookupTypeIdByEnCode 模板), dacli/src/commands/dict/datacreate.ts:78-99 (B-15 修后镜像), dacli/src/webapi/menu.ts:8-10 (web getMenuList 镜像) - 步骤 2 理解意图: JNPF POST /api/system/Menu 返 {code:200, msg, data:null}, 与 dict 同病 (B-15). web 端 handleSubmit 后列表自动刷新拿真实 id - 步骤 3 镜像设计: 镜像 B-15 修法, 创建 lookupMenuIdByEnCode() 用 getMenuList 按 enCode 反查 - 步骤 4 实现: 新建 menu/_lookup.ts (~50 行) + menu/create.ts (3 处模式 A/B/D-4 都接入 lookup, +12/-3 行) **为什么**: menu create 输出 id=(unknown) 是 cosmetic 缺陷, 后端成功但不返 id. 镜像 web 端 handleSubmit → 列表刷新拿 id 流程. **How to apply**: 后续 menu create 后会输出真实 id (类似 dict typeCreate/dataCreate), list/update/delete 可用. --- src/commands/menu/_lookup.ts | 54 ++++++++++++++++++++++++++++++++++++ src/commands/menu/create.ts | 34 +++++++++++++++-------- 2 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/commands/menu/_lookup.ts diff --git a/src/commands/menu/_lookup.ts b/src/commands/menu/_lookup.ts new file mode 100644 index 0000000..0e59ae9 --- /dev/null +++ b/src/commands/menu/_lookup.ts @@ -0,0 +1,54 @@ +/** + * commands/menu/_lookup.ts — 菜单 id 反查工具 (B-19 fix) + * + * 问题: JNPF POST /api/system/Menu 成功时返 {code:200, msg, data:null}, + * 响应里没有新创建的 id (dict / form / 等都一样, B-15 镜像). + * + * 解决: 镜像 web Form.vue handleSubmit → "创建成功" → 列表自动刷新拿真实 id, + * dacli 端 POST 成功后用 GET list 接口按 enCode 反查真实 id. + * + * 复用: create.ts (模式 B 低级 API) 模式 A (Actions/Module) 也走这条 lookup, + * future update / delete by enCode / etc. + * + * 镜像源: + * - web GetMenuList (web/src/api/system/menu.ts:8-10) GET /api/system/Menu/ModuleBySystem + * - B-15 fix: dacli/src/commands/dict/_lookup.ts lookupTypeIdByEnCode + */ +import { JnpfClient } from '../../core/client.ts' +import { initWebapi, loadMenu } from '../../webapi/menu.ts' + +/** + * 按 enCode 反查菜单 id. 返回 id 字符串或 null (找不到). + * + * web GET /api/system/Menu/ModuleBySystem 返回树 [{id, enCode, children: [...]}], + * 递归展开找 enCode 匹配节点. + * + * 镜像: dacli/src/commands/dict/_lookup.ts:21 lookupTypeIdByEnCode. + */ +export async function lookupMenuIdByEnCode(enCode: string): Promise { + if (!enCode) return null + const c = await JnpfClient.fromToken() + await initWebapi(c) + try { + const r = (await loadMenu.getMenuList({})) as unknown + const flatten = (nodes: Array>): Array> => { + const out: Array> = [] + for (const n of nodes) { + if (n && typeof n === 'object') { + out.push(n) + if (Array.isArray(n.children) && n.children.length) { + out.push(...flatten(n.children)) + } + } + } + return out + } + const list = Array.isArray(r) + ? (r as Array>) + : flatten(((r as { list?: Array> })?.list) || []) + const found = list.find((n) => n.enCode === enCode) + return found?.id ? String(found.id) : null + } catch { + return null + } +} \ No newline at end of file diff --git a/src/commands/menu/create.ts b/src/commands/menu/create.ts index 0ac0b30..91489c1 100644 --- a/src/commands/menu/create.ts +++ b/src/commands/menu/create.ts @@ -30,6 +30,7 @@ import { runGuards, handleGuardResult } from '../_blindspot_guards.ts' import { validateMenuCreate, type MenuNodeLite, type FormLite } from './_validate.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { fetchMenuTree } from './_helpers.ts' +import { lookupMenuIdByEnCode } from './_lookup.ts' interface Opts { fullName: string @@ -130,8 +131,10 @@ export function buildMenuCreateCommand(): Command { if (opts.json) printJson(r) process.exit(1) } - log.ok(`✓ 视图菜单创建成功 (formId=${opts.formId}, webType=4): id=${r.data?.id || '(unknown)'}`) - if (opts.json) printJson(r.data || r) + // B-19 fix: D-4 视图菜单也不返 id, 用 lookup 反查 + const d4MenuId = await lookupMenuIdByEnCode(formEnCode || opts.enCode || opts.fullName) + log.ok(`✓ 视图菜单创建成功 (formId=${opts.formId}, webType=4): id=${d4MenuId || r.data?.id || '(unknown)'} (B-19)`) + if (opts.json) printJson({ id: d4MenuId, formId: opts.formId, ...(r.data || {}) }) return } } catch (e) { @@ -211,7 +214,20 @@ export function buildMenuCreateCommand(): Command { } log.ok(`✓ 菜单发布成功 (formId=${opts.formId}, pc=${pc}, app=${app})`) log.ok(' ↳ 后端已自动构造 moduleId/propertyJson/urlAddress/pageAddress + 给当前角色授权') - if (opts.json) printJson(r) + // B-19 fix: 模式 A Actions/Module 不返 id, 用 lookup 反查 (镜像 B-15 dict) + // 后端 menu enCode 默认 = form enCode, 用 form enCode 查 + let menuId: string | null = null + try { + const formR = await c.get<{ data?: { enCode?: string } }>(`/visualdev/Base/${opts.formId}`) + const fData = (formR.data as { enCode?: string } | undefined) || {} + const formEnCode = fData.enCode || opts.fullName + menuId = await lookupMenuIdByEnCode(formEnCode) + } catch { + // 拉 form 失败也不阻断 (lookup 仍按 fullName 查) + menuId = await lookupMenuIdByEnCode(opts.fullName) + } + log.ok(` ↳ 菜单 id=${menuId || '(unknown)'} (B-19 反查自 ModuleBySystem)`) + if (opts.json) printJson({ id: menuId, formId: opts.formId, fullName: opts.fullName }) return } @@ -309,15 +325,11 @@ export function buildMenuCreateCommand(): Command { log.error(` ${formatErrorLine(enhanced, { step: 3, stepName: '后端API' })}`) process.exit(1) } - let menuId: string | undefined = r.data?.id + // B-19 fix: JNPF POST /system/Menu 返 {data:null}, 反查真实 id (B-15 dict 镜像) + // r.data?.id 永远是 undefined, 直接调 lookup + let menuId: string | null = (r.data?.id as string | undefined) || null if (!menuId) { - const listR = await c.get<{ list?: { id: string; enCode: string }[] }>('/system/Menu', { - currentPage: 1, - pageSize: 50, - parentId: body.parentId as string, - keyword: enCode, - }) - menuId = listR.data?.list?.find((m) => m.enCode === enCode)?.id + menuId = await lookupMenuIdByEnCode(enCode) } log.ok(`✓ 创建: id=${menuId || '(unknown)'} enCode=${enCode}`) if (opts.json) printJson({ id: menuId, enCode, ...body })