diff --git a/src/commands/form/codegen.ts b/src/commands/form/codegen.ts index 62be192..f3eb2ce 100644 --- a/src/commands/form/codegen.ts +++ b/src/commands/form/codegen.ts @@ -207,6 +207,73 @@ function classifyFile(item: CodeTreeItem): 'web' | 'entity' | 'model' | 'service return null } +/** + * v0.11d+ Phase 3.3: --auto-menu 实现 (调 webapi /visualdev/Base/{formId}/Actions/Module) + * + * 镜像 web onlineDev/formDesign/components/CreateMenuModal.vue handleSubmit (line 100-120) + * 调 platform /visualdev/Base/{formId}/Actions/Module 后端自动: + * 1. 构造 menu entity (fullName/enCode/propertyJson/urlAddress) + * 2. 选 pcParents 顶级 (从 /system/Menu/SelectorFilter/0 自动找) + * 3. 给当前用户角色授权 + * + * 失败: warn 但不阻断 (codegen 成功, 挂菜单可手动 dacli menu create --formId=X) + */ +async function autoMountMenu( + c: JnpfClient, + formId: string, + fullName: string, + enCode: string, +): Promise { + log.info(`--auto-menu: 调 webapi Actions/Module 挂菜单 (formId=${formId})`) + try { + // 自动从 /system/Menu/SelectorFilter/0 找 type=1 (Web 目录) 顶级 + const sel = (await c.get<{ list?: Array<{ id: string; type: number; children?: any[] }> }>( + '/system/Menu/SelectorFilter/0', + { category: 'Web' }, + )) as { data?: { list?: Array } } + const root = findWebDirectoryParent(sel.data?.list || []) + if (!root) { + log.warn(`--auto-menu: 没找到 Web 目录父菜单 (SelectorFilter 返空), 跳过挂菜单`) + log.warn(' 手动: dacli menu create --formId=' + formId + ' --pcParents=<父menuId>') + return + } + // 调 Actions/Module (镜像 web CreateMenuModal.vue:100-120) + const body = { + pc: 1, + app: 0, + pcModuleParentId: [root], + appModuleParentId: [], + platformRelease: JSON.stringify({ pc: 1, app: 0 }), + } + const r = (await c.postJson(`/visualdev/Base/${formId}/Actions/Module`, body)) as { + code?: number + msg?: string + } + if (r.code !== 200) { + log.warn(`--auto-menu: 挂菜单失败 (code=${r.code}): ${r.msg}`) + log.warn(` 手动: dacli menu create --formId=${formId} --pcParents=${root}`) + return + } + log.ok(`✓ --auto-menu: 已挂菜单 (fullName=${fullName}, enCode=${enCode}, 父菜单=${root})`) + } catch (e) { + log.warn(`--auto-menu: 异常 (${(e as Error).message?.slice(0, 100)}), 跳过`) + } +} + +/** 递归找 SelectorFilter 第一个 type===1 (Web 目录) 节点 (镜像 dacli menu/create.ts:150-162) */ +function findWebDirectoryParent( + items: Array<{ id: string; type: number; children?: any[] }>, +): string | null { + for (const it of items) { + if (it.type === 1) return it.id + if (Array.isArray(it.children) && it.children.length) { + const found = findWebDirectoryParent(it.children) + if (found) return found + } + } + return null +} + /** * JNPF 模板 bug 后处理: 修复 Velocity 未替换的字面 ${...} 占位符 (含方法调用) * @@ -845,8 +912,9 @@ export function buildFormCodegenCommand(): Command { log.info(` 3. mvn package 编译 (boot/${moduleArtifactId}-controller)`) log.info(' 4. 部署到 jnpf-admin') log.info(` 5. 重启 JNPF 后浏览器访问 /api/${company}/${module}/... 验证`) + // v0.11d+ Phase 3.3: --auto-menu 实际实现 (调 webapi /visualdev/Base/{formId}/Actions/Module) if (opts.autoMenu) { - log.info('--auto-menu 暂未实现 (TODO: 调 menu create 自动挂菜单)') + await autoMountMenu(c, formId, fullName, enCode) } }) installAiHelpHook(cmd, {