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 })