fix(menu): BS-19 menu create 提取真实 id (B-15 扩展, web Form.vue 创建后刷新列表对齐)

**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 可用.
This commit is contained in:
ssw committed 2026-06-27 22:26:19 +08:00
1 parent 8b74f31496
commit eb3cac1dba
2 files changed
+77 -11

No files matched your search

+54
View File
@@ -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<string | null> {
if (!enCode) return null
const c = await JnpfClient.fromToken()
await initWebapi(c)
try {
const r = (await loadMenu.getMenuList({})) as unknown
const flatten = (nodes: Array<Record<string, any>>): Array<Record<string, any>> => {
const out: Array<Record<string, any>> = []
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<Record<string, any>>)
: flatten(((r as { list?: Array<Record<string, any>> })?.list) || [])
const found = list.find((n) => n.enCode === enCode)
return found?.id ? String(found.id) : null
} catch {
return null
}
}
+23 -11
View File
@@ -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 })