P4-5: 实装改写后脚本挂回 amis onEvent 重放链路

转换器侧(事件段):TemplateToAmisConverter.attachScriptOnEvent 在页面
body 挂 inited+change 两类 onEvent,动作链 ajax 调 /api/script/execute-template
(传 templateId/seq/item:${&})→ replayScriptCommands 重放返回指令。

执行端点:ScriptExecuteController /api/script/execute-template 改接
{templateId,seq,item},RewrittenScriptStore 按 (tid,seq) 从改写产物
output/rewritten 取脚本正文(去头注释)经 facade 执行;原按名端点改
-by-name 保留 P4-2 兼容。

测试:RewrittenScriptStoreTest 4 + ScriptExecuteControllerTest 4 +
TemplateToAmisConverterTest 新增 4 onEvent 例;converter 28 / metadata 77 /
ArchUnit 1 全绿。

出入回写报告第五章 §5.3;新坑入手册 §8.5(组件级动作路由)/§8.6(改写产物取数键)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 16:55:43 +08:00
parent ba6e7559fa
commit 082b439179
9 changed files with 703 additions and 3 deletions

View File

@@ -471,6 +471,90 @@ public class TemplateToAmisConverter {
return page("分组", tabsSchema);
}
// ──────────────────────────────────────────────────────────────────────
// 脚本挂回 onEvent(P4-5 事件生成段)
// ──────────────────────────────────────────────────────────────────────
/** 改写后脚本挂回链路调用的执行端点(带 templateId/seq/item,见 ScriptExecuteController)。 */
static final String SCRIPT_EXECUTE_API = "/api/script/execute-template";
/** 前端 AmisRenderer 注册的自定义重放动作名(拿到指令数组逐条 doAction)。 */
static final String REPLAY_ACTION = "replayScriptCommands";
/**
* 把模板的 FULL_AUTO 改写脚本挂回 amis 页面 schema 的 {@code onEvent}(P4-5 事件生成段)。
*
* <p>每段脚本生成两类联动触发:
* <ul>
* <li>{@code inited}:页面/表单初次渲染时跑一遍(脚本里初始化字段显隐/选项/只读等);</li>
* <li>{@code change}:字段值变更时重跑(脚本据当前数据域 item 做联动)。</li>
* </ul>
* 触发动作链:先 {@code ajax} 调 {@link #SCRIPT_EXECUTE_API}(传 {@code templateId/seq} +
* {@code item:${&}} 全量表单数据),把返回的 {@code commands} 数组交给前端自定义动作
* {@link #REPLAY_ACTION} 逐条重放(show/hidden/setValue/setOptions/toast 等 → amis doAction)。
* componentId 用改写器输出的大写列名,与 amis 字段 name 同源,天然对齐(报告 §5.4)。
*
* @param pageSchema 已转换的页面 schema(顶层 type=page)
* @param templateId 模板 ID(改写产物落盘目录名)
* @param fullAutoSeqs 该模板 FULL_AUTO 改写脚本的序号列表(取自 full_auto_index.json)
* @return 同一 pageSchema 引用(已就地挂上 onEvent),便于链式使用
*/
@SuppressWarnings("unchecked")
public Map<String, Object> attachScriptOnEvent(Map<String, Object> pageSchema, String templateId,
List<Integer> fullAutoSeqs) {
if (pageSchema == null || templateId == null || templateId.isBlank()
|| fullAutoSeqs == null || fullAutoSeqs.isEmpty()) {
return pageSchema;
}
Object bodyObj = pageSchema.get("body");
if (!(bodyObj instanceof Map)) {
return pageSchema;
}
Map<String, Object> body = (Map<String, Object>) bodyObj;
List<Object> initedActions = new ArrayList<>();
List<Object> changeActions = new ArrayList<>();
for (Integer seq : fullAutoSeqs) {
if (seq == null) {
continue;
}
initedActions.addAll(scriptActionChain(templateId, seq));
changeActions.addAll(scriptActionChain(templateId, seq));
}
Map<String, Object> onEvent = new LinkedHashMap<>();
onEvent.put("inited", Map.of("actions", initedActions));
onEvent.put("change", Map.of("actions", changeActions));
body.put("onEvent", onEvent);
return pageSchema;
}
/** 单段脚本的动作链:ajax 调执行端点 → 自定义动作重放返回的指令数组。 */
private List<Object> scriptActionChain(String templateId, int seq) {
Map<String, Object> ajax = new LinkedHashMap<>();
ajax.put("actionType", "ajax");
Map<String, Object> api = new LinkedHashMap<>();
api.put("method", "post");
api.put("url", SCRIPT_EXECUTE_API);
api.put("data", new LinkedHashMap<>(Map.of(
"templateId", templateId,
"seq", seq,
"item", "${&}"))); // ${&} = amis 当前全量数据域,作为脚本 item 上下文
ajax.put("api", api);
ajax.put("outputVar", "scriptResult" + seq);
Map<String, Object> replay = new LinkedHashMap<>();
replay.put("actionType", REPLAY_ACTION);
// 取上一步 ajax 返回体里的 commands 数组交前端重放
replay.put("args", new LinkedHashMap<>(Map.of(
"commands", "${event.data.scriptResult" + seq + ".commands}")));
List<Object> chain = new ArrayList<>();
chain.add(ajax);
chain.add(replay);
return chain;
}
// ──────────────────────────────────────────────────────────────────────
// 公共工具
// ──────────────────────────────────────────────────────────────────────