From 353e7e9104b10a15b1f38da0631e6e76fa41299f Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 16 Jul 2026 00:27:07 +0800 Subject: [PATCH] =?UTF-8?q?P0-4:=20=E4=BF=AE=20menu=20create=20Actions/Mod?= =?UTF-8?q?ule=20=E5=A4=B1=E8=B4=A5=E8=87=AA=E5=8A=A8=20fallback=20?= =?UTF-8?q?=E4=BD=8E=E7=BA=A7=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/menu/create.ts | 283 ++++++++++++++++++++---------------- 1 file changed, 161 insertions(+), 122 deletions(-) diff --git a/src/commands/menu/create.ts b/src/commands/menu/create.ts index f5c8077..b613d6a 100644 --- a/src/commands/menu/create.ts +++ b/src/commands/menu/create.ts @@ -303,6 +303,38 @@ export function buildMenuCreateCommand(): Command { } log.error(` ↳ 重试仍失败 code=${rr.code}: ${rr.msg}`) } + // P0-4 fix (2026-07-16 jxcj 实战): Actions/Module 报"找不到该上级" (code=400) 时, + // 实为 pcParents=[...] 找不到有效父菜单 (SelectorFilter 自动发现失效 / 跨 app 引用). + // 自动 fallback 低级 POST /system/Menu 路径 (Mode B), 用 form 默认值 (parentId=-1, type=3, icon=form). + // 用户显式传 --icon/--parentId/--type 高级参数时优先尊重. + // 镜像 plan.md §6 修复方向: e.code === 400 && e.msg.includes('找不到该上级') → lowLevelPath() + const needFallbackLow = + r.code === 400 && /找不到该上级/.test(r.msg || '') + if (needFallbackLow) { + log.warn(`⚠ P0-4: Actions/Module 失败 (${r.msg}), 自动 fallback 低级 POST /system/Menu 路径`) + // 拉 form enCode 拼 urlAddress + propertyJson (镜像 Mode B D-9 逻辑) + let fallbackEnCode = opts.enCode || '' + try { + if (!fallbackEnCode) { + const fR = await c.get<{ data?: { enCode?: string } }>(`/visualdev/Base/${opts.formId}`) + fallbackEnCode = (fR.data as { enCode?: string } | undefined)?.enCode || opts.fullName || '' + } + } catch { + fallbackEnCode = fallbackEnCode || opts.fullName || '' + } + // 复用 Mode B 路径: 强制 type=3 (VisualDev), icon=form 默认 (用户 --icon 优先), parentId=低级默认 (用户 --parentId 优先) + const fallbackOpts: Opts = { + ...opts, + type: '3', + icon: opts.icon || 'icon-ym icon-ym-form', + // parentId 保留 opts.parentId (用户显式值 或 commander 默认 '-1') + urlAddress: opts.urlAddress || `model/${fallbackEnCode}`, + } + log.info(` ↳ fallback params: parentId=${fallbackOpts.parentId} type=3 icon=${fallbackOpts.icon} urlAddress=${fallbackOpts.urlAddress}`) + // 直接调用 Mode B 实现 (low-level path) + await runLowLevelPath(c, fallbackOpts) + return + } log.error(`menu create (Actions/Module) 失败 code=${r.code}: ${r.msg}`) // v0.11e+ Phase A1: 后端错误增强 — 自动从 msg 提取 ErrorCode const enhanced = enhanceFromBackend(r.msg || '', 'menu', { entity: 'menu' }) @@ -329,128 +361,10 @@ export function buildMenuCreateCommand(): Command { return } - // 模式 B: 低级 POST /system/Menu (目录/外链等) - // v0.12+ BS-New-8: 模式 B fullName 仍必填 (后端 MenuCrForm 必填, 且没有 form 可推导) - if (!opts.fullName) { - log.error('--fullName 必填 (低/级模式, 目录/外链等)') - process.exit(2) - } - const enCode = opts.enCode || opts.fullName - const type = parseInt(opts.type || '1', 10) - const icon = opts.icon ?? (type === 1 ? 'icon-ym icon-ym-folder' : '') - - // D-9 (BS-46 修复): 低级 mode 接收 --formId, 自动拉 form 详情拼 urlAddress + propertyJson - // 解决: 列表页白屏 (low-level API 不填 columnList, 前端找不到数据源) - let resolvedUrl = opts.urlAddress - // v0.21+ R14 fix BS-新41: 表单类菜单 type ∈ [3, 4, 9, 11] 都应注入 moduleId - // 原 code 仅 type === 3 走自动注入, type=9 (自定义表单) moduleId 被吞 → 浏览器层 form link 断裂 - const isFormMenuType = [3, 4, 9, 11].includes(type as number) - - let resolvedPage = opts.urlAddress - let propertyJson = '{"moduleId":"","iconBackgroundColor":"","isTree":0}' - - if (opts.formId && isFormMenuType) { - // 拉 form 拿 enCode + columnData 自动填 - try { - const formR = await c.get<{ data?: { enCode?: string; columnData?: string; webType?: string | number } }>(`/visualdev/Base/${opts.formId}`) - const fData = (formR.data as { enCode?: string; columnData?: string; webType?: string | number } | undefined) || {} - const formEnCode = fData.enCode || '' - const formWebType = Number(fData.webType || 0) - resolvedUrl = resolvedUrl || `model/${formEnCode}` - resolvedPage = resolvedPage || resolvedUrl - // propertyJson 嵌入 moduleId + webType (前端根据 webType 决定渲染列表/表单) - propertyJson = JSON.stringify({ - moduleId: opts.formId, - iconBackgroundColor: '', - isTree: 0, - webType: formWebType, - }) - log.info(`D-9: 自动拉 form (enCode=${formEnCode}, webType=${formWebType}) 拼 propertyJson.moduleId=${opts.formId}`) - } catch (e) { - log.warn(`D-9: GET form ${opts.formId} 失败 (${(e as Error).message?.slice(0, 80)}), 走默认 propertyJson`) - } - } else if (!opts.formId && isFormMenuType && resolvedUrl?.startsWith('model/')) { - // D-12 修复 (2026-06-23 jxcj E2E 复盘): type=3 + urlAddress=model/xxx 但没传 --formId - // 时, 自动从 enCode 拉 form id 填 propertyJson.moduleId, 否则浏览器层 form 列表空 - // (前端 vue vxe-table 用 moduleId 找数据源, 不看 urlAddress) - const modelEnCode = resolvedUrl.replace(/^model\//, '') - try { - const formR = await c.get<{ list?: { id: string; enCode: string; webType?: number | string }[] }>('/visualdev/Base', { keyword: modelEnCode, currentPage: 1, pageSize: 5 }) - const forms = (formR.data as { list?: { id: string; enCode: string; webType?: number | string }[] } | undefined)?.list || [] - const matched = forms.find(f => f.enCode === modelEnCode) - if (matched) { - const formWebType = Number(matched.webType || 0) - propertyJson = JSON.stringify({ - moduleId: matched.id, - iconBackgroundColor: '', - isTree: 0, - webType: formWebType, - }) - log.info(`D-12: urlAddress=model/${modelEnCode} 但未传 --formId, 自动从 keyword 查询并填 moduleId=${matched.id}`) - } else { - log.warn(`D-12: urlAddress=model/${modelEnCode} 但未找到 form, 建议传 --formId=X 或用 --formId 走 Actions/Module (避免浏览器层列表空)`) - } - } catch (e) { - log.warn(`D-12: GET form by enCode 失败 (${(e as Error).message?.slice(0, 80)})`) - } - } - - // v0.21+ BS-drift-M-1 fix: 手动 --moduleId 覆盖 D-9/D-12 自动查找结果 - // 场景: 跨 app 引用 form (form 在 workSystem 但 menu 在 testspace), D-9 GET 失败 - // 用户手动指定 moduleId 填 propertyJson.moduleId, 浏览器层 form 关联不丢 - if (opts.moduleId && isFormMenuType) { - const existing = JSON.parse(propertyJson) as Record - existing.moduleId = opts.moduleId - propertyJson = JSON.stringify(existing) - log.info(`BS-drift-M-1 fix: --moduleId=${opts.moduleId} 覆盖 propertyJson.moduleId (type=${type})`) - } - - // v0.11 修复: web Form.vue:281-291 条件化 is*Authorize - // 仅 type ∈ [2, 3, 4, 9, 11] (表单类菜单) = 1, 其余 (目录/报表/外链/代码生成) = 0 - const isFormType = [2, 3, 4, 9, 11].includes(type) - const body: Record = { - fullName: opts.fullName, - enCode, - parentId: opts.parentId || '-1', - type, - enabledMark: 1, - // v0.21+ BS-drift-M-2 fix: --sortCode 透传 (默认 0) - sortCode: parseInt(opts.sortCode || '0', 10), - // v0.11 修复: 用 opts.category (默认 Web), 支持 --category=App - category: opts.category || 'Web', - linkTarget: '_self', - isButtonAuthorize: isFormType ? 1 : 0, - isColumnAuthorize: isFormType ? 1 : 0, - isDataAuthorize: isFormType ? 1 : 0, - isFormAuthorize: isFormType ? 1 : 0, - icon, - propertyJson, - } - if (type === 7 && !opts.urlAddress) { - log.error('type=7 (外链) 必须传 --urlAddress') - process.exit(2) - } - if (resolvedUrl) { - body.urlAddress = resolvedUrl - body.pageAddress = resolvedPage - } - log.info(`POST /system/Menu ${enCode} (type=${type})`) - const r = await c.postJson<{ id?: string }>('/system/Menu', body) - if (r.code !== 200) { - log.error(`menu create 失败 code=${r.code}: ${r.msg}`) - // v0.11e+ Phase A1: 后端错误增强 — 自动从 msg 提取 ErrorCode - const enhanced = enhanceFromBackend(r.msg || '', 'menu', { entity: 'menu' }) - log.error(` ${formatErrorLine(enhanced, { step: 3, stepName: '后端API' })}`) - process.exit(1) - } - // 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) { - menuId = await lookupMenuIdByEnCode(enCode) - } - log.ok(`✓ 创建: id=${menuId || '(unknown)'} enCode=${enCode}`) - if (opts.json) printJson({ id: menuId, enCode, ...body }) + // 模式 B: 低级 POST /system/Menu (目录/外链等) — 复用 runLowLevelPath + // P0-4: Mode A Actions/Module "找不到该上级" 失败时也走这个函数 (强制 type=3, icon=form 默认) + await runLowLevelPath(c, opts) + return }) installAiHelpHook(cmd, { @@ -493,6 +407,131 @@ export function buildMenuCreateCommand(): Command { return cmd } +/** + * v0.21+ P0-4 (2026-07-16 jxcj 实战): 低级 POST /system/Menu 路径 — Mode B 主路径 + Mode A "找不到该上级" 自动 fallback 共用 + * + * 用法: + * - Mode B 主路径 (目录/外链等): `dacli menu create --fullName=X --type=1` + * - Mode A fallback (P0-4): `Actions/Module` 报 code=400 "找不到该上级" 时自动调用, 强制 type=3, icon='icon-ym icon-ym-form' + * + * 镜像: web/views/permission/menu/Form.vue handleSubmit (后端: jnpf/.../MenuController.java) + * + D-9/D-12 自动拉 form 拼 propertyJson.moduleId (BS-46/BS-50 修复) + * + BS-drift-M-1: --moduleId 覆盖 (跨 app 引用 form) + * + BS-drift-M-2: --sortCode 透传 (默认 0) + */ +async function runLowLevelPath(c: JnpfClient, opts: Opts): Promise { + // v0.12+ BS-New-8: Mode B fullName 仍必填 (后端 MenuCrForm 必填, 且没有 form 可推导) + if (!opts.fullName) { + log.error('--fullName 必填 (低级模式, 目录/外链等)') + process.exit(2) + } + const enCode = opts.enCode || opts.fullName + const type = parseInt(opts.type || '1', 10) + const icon = opts.icon ?? (type === 1 ? 'icon-ym icon-ym-folder' : '') + + // D-9 (BS-46 修复): 低级 mode 接收 --formId, 自动拉 form 详情拼 urlAddress + propertyJson + let resolvedUrl = opts.urlAddress + // v0.21+ R14 fix BS-新41: 表单类菜单 type ∈ [3, 4, 9, 11] 都应注入 moduleId + const isFormMenuType = [3, 4, 9, 11].includes(type as number) + + let resolvedPage = opts.urlAddress + let propertyJson = '{"moduleId":"","iconBackgroundColor":"","isTree":0}' + + if (opts.formId && isFormMenuType) { + // 拉 form 拿 enCode + columnData 自动填 + try { + const formR = await c.get<{ data?: { enCode?: string; columnData?: string; webType?: string | number } }>(`/visualdev/Base/${opts.formId}`) + const fData = (formR.data as { enCode?: string; columnData?: string; webType?: string | number } | undefined) || {} + const formEnCode = fData.enCode || '' + const formWebType = Number(fData.webType || 0) + resolvedUrl = resolvedUrl || `model/${formEnCode}` + resolvedPage = resolvedPage || resolvedUrl + propertyJson = JSON.stringify({ + moduleId: opts.formId, + iconBackgroundColor: '', + isTree: 0, + webType: formWebType, + }) + log.info(`D-9: 自动拉 form (enCode=${formEnCode}, webType=${formWebType}) 拼 propertyJson.moduleId=${opts.formId}`) + } catch (e) { + log.warn(`D-9: GET form ${opts.formId} 失败 (${(e as Error).message?.slice(0, 80)}), 走默认 propertyJson`) + } + } else if (!opts.formId && isFormMenuType && resolvedUrl?.startsWith('model/')) { + // D-12 修复 (2026-06-23 jxcj E2E 复盘): type=3 + urlAddress=model/xxx 但没传 --formId + const modelEnCode = resolvedUrl.replace(/^model\//, '') + try { + const formR = await c.get<{ list?: { id: string; enCode: string; webType?: number | string }[] }>('/visualdev/Base', { keyword: modelEnCode, currentPage: 1, pageSize: 5 }) + const forms = (formR.data as { list?: { id: string; enCode: string; webType?: number | string }[] } | undefined)?.list || [] + const matched = forms.find(f => f.enCode === modelEnCode) + if (matched) { + const formWebType = Number(matched.webType || 0) + propertyJson = JSON.stringify({ + moduleId: matched.id, + iconBackgroundColor: '', + isTree: 0, + webType: formWebType, + }) + log.info(`D-12: urlAddress=model/${modelEnCode} 但未传 --formId, 自动从 keyword 查询并填 moduleId=${matched.id}`) + } else { + log.warn(`D-12: urlAddress=model/${modelEnCode} 但未找到 form, 建议传 --formId=X 或用 --formId 走 Actions/Module (避免浏览器层列表空)`) + } + } catch (e) { + log.warn(`D-12: GET form by enCode 失败 (${(e as Error).message?.slice(0, 80)})`) + } + } + + // v0.21+ BS-drift-M-1 fix: 手动 --moduleId 覆盖 D-9/D-12 自动查找结果 + if (opts.moduleId && isFormMenuType) { + const existing = JSON.parse(propertyJson) as Record + existing.moduleId = opts.moduleId + propertyJson = JSON.stringify(existing) + log.info(`BS-drift-M-1 fix: --moduleId=${opts.moduleId} 覆盖 propertyJson.moduleId (type=${type})`) + } + + // v0.11 修复: web Form.vue:281-291 条件化 is*Authorize + const isFormType = [2, 3, 4, 9, 11].includes(type) + const body: Record = { + fullName: opts.fullName, + enCode, + parentId: opts.parentId || '-1', + type, + enabledMark: 1, + sortCode: parseInt(opts.sortCode || '0', 10), + category: opts.category || 'Web', + linkTarget: '_self', + isButtonAuthorize: isFormType ? 1 : 0, + isColumnAuthorize: isFormType ? 1 : 0, + isDataAuthorize: isFormType ? 1 : 0, + isFormAuthorize: isFormType ? 1 : 0, + icon, + propertyJson, + } + if (type === 7 && !opts.urlAddress) { + log.error('type=7 (外链) 必须传 --urlAddress') + process.exit(2) + } + if (resolvedUrl) { + body.urlAddress = resolvedUrl + body.pageAddress = resolvedPage + } + log.info(`POST /system/Menu ${enCode} (type=${type})`) + const r = await c.postJson<{ id?: string }>('/system/Menu', body) + if (r.code !== 200) { + log.error(`menu create 失败 code=${r.code}: ${r.msg}`) + // v0.11e+ Phase A1: 后端错误增强 — 自动从 msg 提取 ErrorCode + const enhanced = enhanceFromBackend(r.msg || '', 'menu', { entity: 'menu' }) + log.error(` ${formatErrorLine(enhanced, { step: 3, stepName: '后端API' })}`) + process.exit(1) + } + // B-19 fix: JNPF POST /system/Menu 返 {data:null}, 反查真实 id (B-15 dict 镜像) + let menuId: string | null = (r.data?.id as string | undefined) || null + if (!menuId) { + menuId = await lookupMenuIdByEnCode(enCode) + } + log.ok(`✓ 创建: id=${menuId || '(unknown)'} enCode=${enCode}`) + if (opts.json) printJson({ id: menuId, enCode, ...body }) +} + /** * v0.11d+ step machine 校验 (4 步镜像 web Form.vue + 后端 ModuleController) * 在发 API 前暴露问题, 聚合错误一次报, AI 不用去浏览器试错