diff --git a/platform-converter/src/main/java/com/eip4/platform/converter/EngineclassDelta.java b/platform-converter/src/main/java/com/eip4/platform/converter/EngineclassDelta.java index b09964d..04bda19 100644 --- a/platform-converter/src/main/java/com/eip4/platform/converter/EngineclassDelta.java +++ b/platform-converter/src/main/java/com/eip4/platform/converter/EngineclassDelta.java @@ -246,7 +246,7 @@ public final class EngineclassDelta { } } - // ── 功能模板下载模板管理/设置 · 去增删按钮 + 改标题 ───────────────────────── + // ── 功能模板下载模板管理/设置 · 去增删按钮 + 改标题 + 附件门控 ───────────────── /** * FuncDownloadComponent.refreshRightLayout 的 EIP4 等价改写。转换后 type=3 树页结构= * {@code grid → 列[左 input-tree][右 button-toolbar[添加] + form]}。改写: @@ -254,10 +254,11 @@ public final class EngineclassDelta { *
  • ① 移除右侧 button-toolbar 内「添加/删除」按钮(老 cl.removeComponent(insert)/(del)——附件记录随 * 菜单/实体生成不应手工增删);toolbar 因此变空则整条移除。
  • *
  • ② 右表单标题改「编辑模板附件」(老 panel.setCaption("编辑模板附件"))。
  • + *
  • ③ 表单 initApi 取回 {@code VIEW_TEMPLATEID} 后,未关联功能模板的菜单不显示附件编辑项和保存按钮 + * (老 pojo.VIEW_TEMPLATEID 非空才 add 右表单)。
  • * - * 未做:老「仅 pojo.VIEW_TEMPLATEID 非空才显示右表单」的门控——amis 表单 {@code visibleOn} 依赖 - * 自身 initApi 数据成循环(隐藏则不 fetch),正解需树节点携带 VIEW_TEMPLATEID(改 TemplateTreeController), - * 价值低,暂延后。 + * 注:不能直接给 form 加 {@code visibleOn=${VIEW_TEMPLATEID}},否则 form 隐藏时不会发 initApi。这里保留 + * form 壳子加载当前树节点,再把 body/action 按返回数据门控。 */ @SuppressWarnings("unchecked") private static void funcDownloadDelta(Map schema, ConversionResult result) { @@ -291,6 +292,7 @@ public final class EngineclassDelta { } else if ("form".equals(m.get("type"))) { // ② 右表单标题改「编辑模板附件」 Object old = m.put("title", "编辑模板附件"); + applyFuncDownloadTemplateGate(m, result); if (result != null) { result.note("engineclass[FuncDownloadComponent] 右表单标题改写: " + old + " → 编辑模板附件"); @@ -303,6 +305,69 @@ public final class EngineclassDelta { } } + /** 按表单 initApi 返回的 VIEW_TEMPLATEID 门控附件编辑项;不隐藏 form 本身,避免阻断 initApi。 */ + @SuppressWarnings("unchecked") + private static void applyFuncDownloadTemplateGate(Map form, ConversionResult result) { + Object bodyObj = form.get("body"); + if (bodyObj instanceof List body) { + List gated = new ArrayList<>(); + gated.add(mapOf( + "type", "tpl", + "id", "funcDownloadNoTemplateTip", + "tpl", "该菜单未关联功能模板,暂无附件可编辑", + "visibleOn", "${selId && !VIEW_TEMPLATEID}")); + for (Object item : body) { + if (item instanceof Map itemMap && shouldGateFuncDownloadItem((Map) itemMap)) { + addVisibleOn((Map) itemMap, "VIEW_TEMPLATEID"); + } + gated.add(item); + } + form.put("body", gated); + } else if (bodyObj instanceof Map bodyMap) { + Map item = (Map) bodyMap; + if (shouldGateFuncDownloadItem(item)) { + addVisibleOn(item, "VIEW_TEMPLATEID"); + } + } + + Object actionsObj = form.get("actions"); + if (actionsObj instanceof List actions) { + for (Object action : actions) { + if (action instanceof Map actionMap) { + addVisibleOn((Map) actionMap, "VIEW_TEMPLATEID"); + } + } + } + + if (result != null) { + result.note("engineclass[FuncDownloadComponent] 右表单按 VIEW_TEMPLATEID 门控附件编辑"); + } + } + + private static boolean shouldGateFuncDownloadItem(Map item) { + if (Boolean.TRUE.equals(item.get("hidden"))) { + return false; + } + return !"hidden".equals(item.get("type")); + } + + private static void addVisibleOn(Map item, String conditionExpr) { + String old = item.get("visibleOn") == null ? "" : String.valueOf(item.get("visibleOn")).trim(); + if (old.isBlank()) { + item.put("visibleOn", "${" + conditionExpr + "}"); + return; + } + item.put("visibleOn", "${(" + unwrapAmisExpr(old) + ") && (" + conditionExpr + ")}"); + } + + private static String unwrapAmisExpr(String expr) { + String trimmed = expr == null ? "" : expr.trim(); + if (trimmed.startsWith("${") && trimmed.endsWith("}") && trimmed.length() >= 3) { + return trimmed.substring(2, trimmed.length() - 1); + } + return trimmed; + } + /** * 剥除 button-toolbar 内「添加/删除」按钮(重建 buttons 列表回填,兼容不可变源);剥后为空返回 true(供丢弃整条)。 */ diff --git a/platform-converter/src/test/java/com/eip4/platform/converter/TemplateToAmisConverterTest.java b/platform-converter/src/test/java/com/eip4/platform/converter/TemplateToAmisConverterTest.java index f494cef..f27c5bf 100644 --- a/platform-converter/src/test/java/com/eip4/platform/converter/TemplateToAmisConverterTest.java +++ b/platform-converter/src/test/java/com/eip4/platform/converter/TemplateToAmisConverterTest.java @@ -1451,11 +1451,12 @@ class TemplateToAmisConverterTest { /** * engineclass=FuncDownloadComponent(功能模板下载模板管理/设置,type=3 树页)→ 整页改写器(机制① MUTATORS): - * 移除右表单「添加」按钮(空 toolbar 整条移除)+ 右表单标题改「编辑模板附件」。复刻 refreshRightLayout。 + * 移除右表单「添加」按钮(空 toolbar 整条移除)+ 右表单标题改「编辑模板附件」 + * + 按 VIEW_TEMPLATEID 门控附件编辑。复刻 refreshRightLayout。 */ @Test @SuppressWarnings("unchecked") - void engineclass_FuncDownload_去添加按钮并改标题() { + void engineclass_FuncDownload_去添加按钮改标题并按模板门控() { // 模拟转换后 type=3 树页:grid → 列[左 input-tree][右 button-toolbar[添加] + form] Map addBtn = new LinkedHashMap<>(); addBtn.put("type", "button"); @@ -1466,6 +1467,18 @@ class TemplateToAmisConverterTest { Map form = new LinkedHashMap<>(); form.put("type", "form"); form.put("title", "功能模板下载模板设置"); + Map document = new LinkedHashMap<>(); + document.put("type", "input-file"); + document.put("name", "DOCUMENT"); + Map hiddenTemplate = new LinkedHashMap<>(); + hiddenTemplate.put("type", "input-text"); + hiddenTemplate.put("name", "VIEW_TEMPLATEID"); + hiddenTemplate.put("hidden", true); + Map submit = new LinkedHashMap<>(); + submit.put("type", "submit"); + submit.put("label", "保存"); + form.put("body", new ArrayList<>(List.of(document, hiddenTemplate))); + form.put("actions", new ArrayList<>(List.of(submit))); Map rightCol = new LinkedHashMap<>(); rightCol.put("body", new ArrayList<>(List.of(toolbar, form))); Map tree = new LinkedHashMap<>(); @@ -1487,6 +1500,13 @@ class TemplateToAmisConverterTest { .anyMatch(o -> o instanceof Map && "button-toolbar".equals(((Map) o).get("type"))); assertFalse(hasToolbar, "添加按钮已删 → 空 button-toolbar 应整条移除"); assertEquals("编辑模板附件", form.get("title"), "右表单标题应改为「编辑模板附件」"); + List formBody = (List) form.get("body"); + Map tip = (Map) formBody.get(0); + assertEquals("tpl", tip.get("type")); + assertEquals("${selId && !VIEW_TEMPLATEID}", tip.get("visibleOn")); + assertEquals("${VIEW_TEMPLATEID}", document.get("visibleOn"), "附件编辑项应仅在 VIEW_TEMPLATEID 非空时显示"); + assertFalse(hiddenTemplate.containsKey("visibleOn"), "隐藏字段不参与视觉门控,避免影响提交上下文"); + assertEquals("${VIEW_TEMPLATEID}", submit.get("visibleOn"), "保存按钮应仅在 VIEW_TEMPLATEID 非空时显示"); // 左树不受影响、表单仍在 assertEquals("input-tree", tree.get("type")); boolean hasForm = rb.stream()