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:
18
docs/迁移手册.md
18
docs/迁移手册.md
@@ -671,6 +671,24 @@ com.dsasp.* com.eip4.*
|
||||
| **对策** | schema 经**独立端点**(`/schema?id=<模板id>` 后端读文件返回)拉取,URL 只传短 id;宿主 `fetch('/schema?id=…')` 后再 `amis.embed`。改后该类模板渲染恢复正常(form 抽样 4/5 → 5/5) |
|
||||
| **影响范围** | P4 闸二冒烟宿主(已修,见 `platform-converter/gate2-smoke/{host.html,smoke.mjs}`);任何用 query 串传 schema 给 headless 宿主的渲染验证脚本 |
|
||||
|
||||
### 8.5 amis show/hidden/setValue/static 不是顶层注册动作,须经组件级路由(脚本挂回重放坑)[NEW 2026-06-11]
|
||||
|
||||
| 栏目 | 内容 |
|
||||
|------|------|
|
||||
| **现象** | P4-5 改写后脚本挂回 onEvent 重放:把 facade 返回的指令(`{actionType:"hidden",componentId:"BEGINDATE"}` 等)当成顶层 amis 动作直接派发时,担心 `registerAction` 列表里查不到 show/hidden/setValue/static/reload 而失效 |
|
||||
| **根因** | amis 6.13 SDK `registerAction(...)` 注册的**顶层动作**仅 `ajax/dialog/toast/copy/broadcast/component/...` 等(实测 30 个,无 show/hidden/setValue/static/reload);显隐/赋值/只读/重载是**组件级动作**——动作对象带 `componentId` 时由 amis 路由到该组件实例的 `doAction`,actionType 名(show/hidden/static/nonstatic/setValue/reload)由组件自身识别。`toast` 是少数顶层动作之一(无 componentId,走 args.msg) |
|
||||
| **对策** | 自定义重放动作 `replayScriptCommands` 里对每条指令调 amis-core `runActions(action, renderer, event)`:带 `componentId` 的自动走组件级路由(show/hidden/setValue/static/reload 生效),`toast` 走顶层。componentId 用改写器输出的大写列名,与 amis 字段 `name` 同源天然对齐,无需映射表。实测 inited 隐藏字段、change 回填字段值均生效(见 `tools/s3_rewriter/P4脚本挂回实测报告.md`) |
|
||||
| **影响范围** | eip4-web `src/amis/{scriptCommands.ts,registerReplayAction.ts}`;任何把后端 UI 动词指令重放到 amis 的胶水层;setOptions/addOption 等「改组件 options」类动词在原生 amis 无对应组件动作,select 选项联动需后端 source 或 amis `setValue` 整体替换 options(非逐条 addOption),落 P4 中段后续 |
|
||||
|
||||
### 8.6 改写后脚本挂回走「改写产物按 templateId+seq 取数」而非「库内 XML 原始脚本」[NEW 2026-06-11]
|
||||
|
||||
| 栏目 | 内容 |
|
||||
|------|------|
|
||||
| **现象** | 报告第五章 §5.2 把衔接接口写成 `/api/script/execute`(带 `{script,item}`,前端直传脚本文本);端到端 §3.2 又用 `/api/script/execute-template?template=<名>`(按模板名从库 XML 取**原始** S3 脚本)。两者都不是「挂回改写后脚本」的正解 |
|
||||
| **根因** | 挂回链路要挂的是**改写后脚本**(`output/rewritten/<tid>/<seq>.FULL_AUTO.groovy` 去头注释正文),不是库内原始 S3 脚本(含未改写的 Vaadin S3 动词);且前端直传脚本文本不安全(任意脚本注入面)。正确取数键是 `(templateId, seq)`——与改写产物落盘命名一致 |
|
||||
| **对策** | P4-5 实装把 `/api/script/execute-template` 改为接受 `{templateId, seq, item}`(RequestBody),后端 `RewrittenScriptStore` 按 `(tid,seq)` 从改写产物取脚本正文执行;原「按模板名取库内 XML 脚本」端点路径区隔为 `/api/script/execute-template-by-name`(保留 P4-2 兼容)。转换器 `attachScriptOnEvent` 生成的 onEvent 即调前者,`item:${&}` 传当前全量表单数据作脚本上下文。出入点已回写报告第五章 |
|
||||
| **影响范围** | `platform-metadata` ScriptExecuteController + RewrittenScriptStore;`platform-converter` TemplateToAmisConverter.attachScriptOnEvent;改写产物落盘根经 `eip4.script.rewritten-root` 配置(默认 `tools/s3_rewriter/output/rewritten`),部署须确保改写产物随应用可达 |
|
||||
|
||||
---
|
||||
|
||||
## 九、待补章节占位
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────
|
||||
// 公共工具
|
||||
// ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -305,4 +305,68 @@ class TemplateToAmisConverterTest {
|
||||
assertFalse(converter.convertGraded("<unknown/>", new ArrayList<>(), noPage).getGrade()
|
||||
== ConversionGrade.SUCCESS);
|
||||
}
|
||||
|
||||
// ── 脚本挂回 onEvent(P4-5 事件生成段)─────────────────────────────────
|
||||
|
||||
@Test
|
||||
void attachScriptOnEvent_挂上inited与change两类触发() {
|
||||
String xml = "<crud entityid='e1'><display><field name='A' text='A'/></display></crud>";
|
||||
ConversionResult r = converter.convertGraded(xml, new ArrayList<>(), noPage);
|
||||
Map<String, Object> schema = r.getSchema();
|
||||
|
||||
converter.attachScriptOnEvent(schema, "021e3ba7-tid", List.of(2));
|
||||
Map<String, Object> body = body(schema);
|
||||
Map<String, Object> onEvent = (Map<String, Object>) body.get("onEvent");
|
||||
assertNotNull(onEvent);
|
||||
assertTrue(onEvent.containsKey("inited"));
|
||||
assertTrue(onEvent.containsKey("change"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachScriptOnEvent_动作链调执行端点并重放指令() {
|
||||
String xml = "<crud entityid='e1'><display><field name='A' text='A'/></display></crud>";
|
||||
Map<String, Object> schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema();
|
||||
converter.attachScriptOnEvent(schema, "tid-x", List.of(1));
|
||||
|
||||
Map<String, Object> onEvent = (Map<String, Object>) body(schema).get("onEvent");
|
||||
Map<String, Object> inited = (Map<String, Object>) onEvent.get("inited");
|
||||
List<Object> actions = (List<Object>) inited.get("actions");
|
||||
assertEquals(2, actions.size()); // ajax + 重放,各一
|
||||
|
||||
Map<String, Object> ajax = (Map<String, Object>) actions.get(0);
|
||||
assertEquals("ajax", ajax.get("actionType"));
|
||||
Map<String, Object> api = (Map<String, Object>) ajax.get("api");
|
||||
assertEquals("/api/script/execute-template", api.get("url"));
|
||||
Map<String, Object> data = (Map<String, Object>) api.get("data");
|
||||
assertEquals("tid-x", data.get("templateId"));
|
||||
assertEquals(1, data.get("seq"));
|
||||
assertEquals("${&}", data.get("item")); // 全量表单数据作脚本 item
|
||||
|
||||
Map<String, Object> replay = (Map<String, Object>) actions.get(1);
|
||||
assertEquals("replayScriptCommands", replay.get("actionType"));
|
||||
Map<String, Object> args = (Map<String, Object>) replay.get("args");
|
||||
assertTrue(((String) args.get("commands")).contains("commands"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachScriptOnEvent_多段脚本动作链累加() {
|
||||
String xml = "<crud entityid='e1'><display><field name='A' text='A'/></display></crud>";
|
||||
Map<String, Object> schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema();
|
||||
converter.attachScriptOnEvent(schema, "tid-x", List.of(1, 2, 3));
|
||||
|
||||
Map<String, Object> onEvent = (Map<String, Object>) body(schema).get("onEvent");
|
||||
Map<String, Object> change = (Map<String, Object>) onEvent.get("change");
|
||||
List<Object> actions = (List<Object>) change.get("actions");
|
||||
assertEquals(6, actions.size()); // 3 段 × (ajax + 重放)
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachScriptOnEvent_无脚本或空入参不挂onEvent() {
|
||||
String xml = "<crud entityid='e1'><display><field name='A' text='A'/></display></crud>";
|
||||
Map<String, Object> schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema();
|
||||
converter.attachScriptOnEvent(schema, "tid-x", List.of()); // 空序号列表
|
||||
assertNull(body(schema).get("onEvent"));
|
||||
converter.attachScriptOnEvent(schema, "", List.of(1)); // 空 templateId
|
||||
assertNull(body(schema).get("onEvent"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.eip4.platform.metadata.script;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 改写后脚本库读取(P4-5 脚本挂回 onEvent 重放链路)。
|
||||
*
|
||||
* <p>S3 改写器(tools/s3_rewriter)把存量脚本按 {@code template_id + 脚本序号} 落盘到
|
||||
* {@code output/rewritten/<template_id>/<seq>.<label>.groovy}(见 P4 脚本迁移执行报告 §1.2)。
|
||||
* 本类按 {@code (templateId, seq)} 定位改写后脚本,去掉头注释块返回可直接执行的正文,
|
||||
* 是「改写后脚本挂回页面 schema」的取数端:转换器生成的 amis onEvent 调
|
||||
* {@code /api/script/execute-template}(带 templateId/seq/item)时由本类供脚本正文。
|
||||
*
|
||||
* <p>路径根经 {@code eip4.script.rewritten-root} 配置,默认相对启动工作目录
|
||||
* {@code tools/s3_rewriter/output/rewritten}(与工程内落盘产物对齐)。
|
||||
*/
|
||||
@Component
|
||||
public class RewrittenScriptStore {
|
||||
|
||||
/** 头注释块前缀(改写器落盘的每段脚本头部,见 §1.2),正文从首个非注释非空行起算。 */
|
||||
private static final String HEADER_PREFIX = "//";
|
||||
|
||||
private final Path root;
|
||||
|
||||
public RewrittenScriptStore(
|
||||
@Value("${eip4.script.rewritten-root:tools/s3_rewriter/output/rewritten}") String rewrittenRoot) {
|
||||
this.root = Path.of(rewrittenRoot);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取改写后脚本正文(去头注释块)。
|
||||
*
|
||||
* @param templateId 模板 ID(落盘目录名)
|
||||
* @param seq 脚本序号(落盘文件名前缀,如 {@code 2.FULL_AUTO.groovy} 的 2)
|
||||
* @return 去头注释的脚本正文;定位失败返回 {@code null}(调用方降级处理,不抛异常)
|
||||
*/
|
||||
public String load(String templateId, int seq) {
|
||||
if (templateId == null || templateId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
Path dir = root.resolve(templateId);
|
||||
if (!Files.isDirectory(dir)) {
|
||||
return null;
|
||||
}
|
||||
String prefix = seq + ".";
|
||||
try (Stream<Path> files = Files.list(dir)) {
|
||||
Path match = files
|
||||
.filter(p -> p.getFileName().toString().startsWith(prefix)
|
||||
&& p.getFileName().toString().endsWith(".groovy"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (match == null) {
|
||||
return null;
|
||||
}
|
||||
return stripHeader(Files.readString(match, StandardCharsets.UTF_8));
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉改写器头注释块:跳过文件起始处连续的 {@code //} 注释行与空行,从首个实质代码行起返回。
|
||||
* 注释块中间的业务注释(脚本正文里的 {@code //...})不受影响(只剥离起始处的头块)。
|
||||
*/
|
||||
static String stripHeader(String raw) {
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String[] lines = raw.split("\n", -1);
|
||||
int start = 0;
|
||||
while (start < lines.length) {
|
||||
String trimmed = lines[start].strip();
|
||||
if (trimmed.isEmpty() || trimmed.startsWith(HEADER_PREFIX)) {
|
||||
start++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
List<String> body = new ArrayList<>();
|
||||
for (int i = start; i < lines.length; i++) {
|
||||
body.add(lines[i]);
|
||||
}
|
||||
return String.join("\n", body).strip();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.dsasp.server.dataDic.binding.ScriptingEnginesManager;
|
||||
import com.eip4.platform.metadata.script.RewrittenScriptStore;
|
||||
import com.eip4.platform.metadata.script.ScriptFacade;
|
||||
|
||||
/**
|
||||
@@ -36,9 +37,11 @@ import com.eip4.platform.metadata.script.ScriptFacade;
|
||||
public class ScriptExecuteController {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final RewrittenScriptStore rewrittenScriptStore;
|
||||
|
||||
public ScriptExecuteController(JdbcTemplate jdbcTemplate) {
|
||||
public ScriptExecuteController(JdbcTemplate jdbcTemplate, RewrittenScriptStore rewrittenScriptStore) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.rewrittenScriptStore = rewrittenScriptStore;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,12 +78,15 @@ public class ScriptExecuteController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按模板名从客户库取真实脚本执行(改写后脚本挂回页面 schema 的端到端验证)。
|
||||
* 按模板名从客户库取「库内原始脚本」执行(P4-2 端到端验证端点,保留兼容)。
|
||||
*
|
||||
* <p>注:P4-5 改写后脚本挂回链路走 {@link #executeTemplateRewritten}(按 templateId/seq 取
|
||||
* 改写产物);本端点按模板名取库内 XML 原始脚本,端点路径区隔为 {@code -by-name}。
|
||||
*
|
||||
* @param template 模板名(VIEW_TEMPLATE.NAME)
|
||||
* @param state 注入 item.ISPLACE 的取值,便于驱动分支
|
||||
*/
|
||||
@PostMapping("/api/script/execute-template")
|
||||
@PostMapping("/api/script/execute-template-by-name")
|
||||
public Map<String, Object> executeTemplate(
|
||||
@RequestParam String template,
|
||||
@RequestParam(required = false) String state) throws Exception {
|
||||
@@ -104,6 +110,61 @@ public class ScriptExecuteController {
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 改写后脚本挂回 onEvent 重放链路的执行端点(P4-5)。
|
||||
*
|
||||
* <p>转换器在 amis 页面 onEvent 里配置「调本端点,传 templateId/seq/item」;本端点按
|
||||
* {@code (templateId, seq)} 从改写后脚本库({@link RewrittenScriptStore})取脚本正文,
|
||||
* 经 facade 执行,返回 amis 事件动作指令数组供前端逐条重放(show/hidden/setValue/setOptions/toast 等)。
|
||||
*
|
||||
* <p>请求体:{@code {"templateId":"021e...","seq":2,"item":{"STORE_ID":"X",...}}}<br>
|
||||
* 返回 :{@code {"commands":[{actionType,componentId,args},...], "passed":true, "item":{...}}}
|
||||
*
|
||||
* @param body templateId(落盘目录名)+ seq(脚本序号)+ item(amis 表单当前数据域)
|
||||
*/
|
||||
@PostMapping("/api/script/execute-template")
|
||||
public Map<String, Object> executeTemplateRewritten(@RequestBody Map<String, Object> body) {
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
String templateId = body.get("templateId") == null ? null : String.valueOf(body.get("templateId"));
|
||||
if (templateId == null || templateId.isBlank()) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", "缺少 templateId");
|
||||
return resp;
|
||||
}
|
||||
int seq = body.get("seq") instanceof Number n ? n.intValue() : parseSeq(body.get("seq"));
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> item = body.get("item") instanceof Map
|
||||
? (Map<String, Object>) body.get("item") : new LinkedHashMap<>();
|
||||
|
||||
String script = rewrittenScriptStore.load(templateId, seq);
|
||||
if (script == null || script.isBlank()) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", "改写后脚本未找到: templateId=" + templateId + " seq=" + seq);
|
||||
return resp;
|
||||
}
|
||||
resp.put("templateId", templateId);
|
||||
resp.put("seq", seq);
|
||||
try {
|
||||
List<Map<String, Object>> commands = runWithFacade("rw:" + templateId + ":" + seq, script, item);
|
||||
resp.put("passed", true);
|
||||
resp.put("item", item);
|
||||
resp.put("commands", commands);
|
||||
resp.put("commandCount", commands.size());
|
||||
} catch (Exception e) {
|
||||
resp.put("passed", false);
|
||||
resp.put("error", e.getMessage());
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static int parseSeq(Object raw) {
|
||||
try {
|
||||
return raw == null ? 1 : Integer.parseInt(String.valueOf(raw).trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** 用 facade 动词表执行脚本,返回捕获的 amis 指令。 */
|
||||
private List<Map<String, Object>> runWithFacade(String key, String script, Map<String, Object> item) {
|
||||
ScriptFacade facade = new ScriptFacade(item);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.eip4.platform.metadata.script;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/**
|
||||
* P4-5 单测:改写后脚本库读取(templateId/seq 定位 + 头注释剥离)。
|
||||
*/
|
||||
class RewrittenScriptStoreTest {
|
||||
|
||||
@Test
|
||||
void stripHeader_剥离起始头注释块保留正文() {
|
||||
String raw = "// === EIP4 P4 脚本迁移产出 ===\n"
|
||||
+ "// template_id : 0d9eac07\n"
|
||||
+ "// label : FULL_AUTO\n"
|
||||
+ "\n"
|
||||
+ "setFieldVisible(\"BEGINDATE\", false);";
|
||||
String body = RewrittenScriptStore.stripHeader(raw);
|
||||
assertEquals("setFieldVisible(\"BEGINDATE\", false);", body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stripHeader_不误删正文中部业务注释() {
|
||||
String raw = "// header line\n"
|
||||
+ "def searchMap = [\"UNITTYPE\":7];//建设单位\n"
|
||||
+ "setFieldFilter(\"BUILDCOMPANY\", searchMap);";
|
||||
String body = RewrittenScriptStore.stripHeader(raw);
|
||||
assertTrue(body.startsWith("def searchMap"), body);
|
||||
assertTrue(body.contains("//建设单位"), "正文中部注释不应被剥离");
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_按templateId与seq定位改写脚本去头注释(@TempDir Path tmp) throws IOException {
|
||||
Path dir = tmp.resolve("tid-abc");
|
||||
Files.createDirectories(dir);
|
||||
Files.writeString(dir.resolve("2.FULL_AUTO.groovy"),
|
||||
"// === EIP4 P4 脚本迁移产出 ===\n// seq : 2\n\naddSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);",
|
||||
StandardCharsets.UTF_8);
|
||||
RewrittenScriptStore store = new RewrittenScriptStore(tmp.toString());
|
||||
String body = store.load("tid-abc", 2);
|
||||
assertEquals("addSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);", body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_缺目录或缺序号返回null(@TempDir Path tmp) {
|
||||
RewrittenScriptStore store = new RewrittenScriptStore(tmp.toString());
|
||||
assertNull(store.load("no-such-tid", 1));
|
||||
assertNull(store.load(null, 1));
|
||||
assertNull(store.load("", 1));
|
||||
assertFalse(store.load("no-such-tid", 9) != null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import com.eip4.platform.metadata.script.RewrittenScriptStore;
|
||||
|
||||
/**
|
||||
* P4-5 单测:改写后脚本挂回执行端点(/api/script/execute-template)。
|
||||
*
|
||||
* <p>构造一个落盘的改写后脚本(templateId/seq 命名 + 头注释块),经端点取出去头执行,
|
||||
* 断言返回 amis 事件动作指令数组(actionType/componentId/args),是「改写后脚本挂回」取数→
|
||||
* 执行→指令产出的端到端单测(脚本经保真层引擎真实跑通,非桩)。
|
||||
*/
|
||||
class ScriptExecuteControllerTest {
|
||||
|
||||
private ScriptExecuteController controller(Path root) {
|
||||
return new ScriptExecuteController(mock(JdbcTemplate.class), new RewrittenScriptStore(root.toString()));
|
||||
}
|
||||
|
||||
private void writeScript(Path root, String tid, String fileName, String content) throws IOException {
|
||||
Path dir = root.resolve(tid);
|
||||
Files.createDirectories(dir);
|
||||
Files.writeString(dir.resolve(fileName), content, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void executeTemplateRewritten_取改写脚本执行产出amis指令(@TempDir Path root) throws IOException {
|
||||
// 福利保险结算单 seq=1:setFieldVisible("BEGINDATE", false) → hidden
|
||||
writeScript(root, "0d9eac07", "1.FULL_AUTO.groovy",
|
||||
"// === EIP4 P4 脚本迁移产出 ===\n// seq : 1\n\nsetFieldVisible(\"BEGINDATE\", false);");
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("templateId", "0d9eac07");
|
||||
body.put("seq", 1);
|
||||
body.put("item", new LinkedHashMap<>());
|
||||
Map<String, Object> resp = c.executeTemplateRewritten(body);
|
||||
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals(1, cmds.size());
|
||||
assertEquals("hidden", cmds.get(0).get("actionType"));
|
||||
assertEquals("BEGINDATE", cmds.get(0).get("componentId"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void executeTemplateRewritten_服务端语义动词从item求值并联动(@TempDir Path root) throws IOException {
|
||||
// 办公用品领用申请 seq=2:addSubTableButton + bindField(getFieldValue("STORE_ID"))
|
||||
writeScript(root, "021e3ba7", "2.FULL_AUTO.groovy",
|
||||
"// header\naddSubTableButton(\"GOODSDETAIL\", \"SELECT\", 0);\n"
|
||||
+ "bindField(\"OLD_STORE_ID\",getFieldValue(\"STORE_ID\"));");
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("STORE_ID", "S-99");
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("templateId", "021e3ba7");
|
||||
body.put("seq", "2"); // 字符串 seq 也能解析
|
||||
body.put("item", item);
|
||||
Map<String, Object> resp = c.executeTemplateRewritten(body);
|
||||
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals(2, cmds.size());
|
||||
assertEquals("addSubTableButton", cmds.get(0).get("actionType"));
|
||||
Map<String, Object> setValue = cmds.get(1);
|
||||
assertEquals("setValue", setValue.get("actionType"));
|
||||
assertEquals("OLD_STORE_ID", setValue.get("componentId"));
|
||||
Map<String, Object> args = (Map<String, Object>) setValue.get("args");
|
||||
assertEquals("S-99", args.get("value")); // getFieldValue 从 item 求值闭环
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeTemplateRewritten_缺templateId或脚本未找到时passed为false(@TempDir Path root) {
|
||||
ScriptExecuteController c = controller(root);
|
||||
|
||||
Map<String, Object> noTid = new LinkedHashMap<>();
|
||||
Map<String, Object> r1 = c.executeTemplateRewritten(noTid);
|
||||
assertEquals(Boolean.FALSE, r1.get("passed"));
|
||||
assertNotNull(r1.get("error"));
|
||||
|
||||
Map<String, Object> missing = new LinkedHashMap<>();
|
||||
missing.put("templateId", "no-such");
|
||||
missing.put("seq", 1);
|
||||
Map<String, Object> r2 = c.executeTemplateRewritten(missing);
|
||||
assertEquals(Boolean.FALSE, r2.get("passed"));
|
||||
assertTrue(((String) r2.get("error")).contains("未找到"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void execute_直接执行脚本仍可用(@TempDir Path root) {
|
||||
ScriptExecuteController c = controller(root);
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("script", "showToast(\"ok\");");
|
||||
body.put("item", new LinkedHashMap<>());
|
||||
Map<String, Object> resp = c.execute(body);
|
||||
assertEquals(Boolean.TRUE, resp.get("passed"));
|
||||
List<Map<String, Object>> cmds = (List<Map<String, Object>>) resp.get("commands");
|
||||
assertEquals("toast", cmds.get(0).get("actionType"));
|
||||
}
|
||||
}
|
||||
172
tools/s3_rewriter/P4脚本挂回实测报告.md
Normal file
172
tools/s3_rewriter/P4脚本挂回实测报告.md
Normal file
@@ -0,0 +1,172 @@
|
||||
# P4 脚本挂回实测报告(改写后脚本 → amis onEvent 重放链路)
|
||||
|
||||
> 任务编号:P4-5
|
||||
> 日期:2026-06-11
|
||||
> 责任:实装「改写后脚本挂回页面 schema」链路(onEvent 回填 + 执行端点指令重放 + componentId 大写列名对齐 + item 数据闭环)
|
||||
> 范围:转换器侧 onEvent 生成段(platform-converter)+ 执行端点(platform-metadata ScriptExecuteController)+ 前端重放胶水(eip4-web AmisRenderer)+ 端到端 playwright 实测
|
||||
> 诚实边界:链路三段均为真实代码 + 真实改写产物 + 真实保真层引擎执行(非桩)。端到端用前端 mock schema(onEvent 形态 = 转换器 `attachScriptOnEvent` 产出,已单测固定)承接,脚本与指令来自后端真实改写产物(`output/rewritten/`)经真实 facade 引擎执行。口径见 §四。
|
||||
|
||||
---
|
||||
|
||||
## 一、链路实装结论(与报告第五章设计的出入)
|
||||
|
||||
第五章给出衔接「设计」,本任务实装为可运行链路,**有三处与第五章设计的出入,以实装为准并已回写第五章 / 手册 §8.5、§8.6**:
|
||||
|
||||
| 衔接点 | 第五章设计 | 实装(以此为准) | 出入说明 |
|
||||
|--------|-----------|-----------------|---------|
|
||||
| **执行端点契约** | §5.2 衔接接口写为 `/api/script/execute`(前端直传 `{script, item}`);§3.2 端到端用 `/api/script/execute-template?template=<名>`(按名取库内 XML 原始脚本) | `/api/script/execute-template` 改为 `{templateId, seq, item}`(RequestBody),后端按 `(tid,seq)` 从**改写产物**取脚本正文执行 | 挂回的是**改写后脚本**(`output/rewritten/<tid>/<seq>.FULL_AUTO.groovy` 去头注释),非库内原始 S3 脚本;前端直传脚本文本有注入面,改为传 `(templateId,seq)` 取数键。原「按名取库内脚本」端点路径区隔为 `/api/script/execute-template-by-name`(保留 P4-2 兼容)。详见手册 §8.6 |
|
||||
| **指令重放机制** | §5.2「前端在对应事件回调里 `dispatchEvent` 重放这些指令」 | 前端注册自定义 amis 动作 `replayScriptCommands`,对每条翻译后指令调 amis-core `runActions(action, renderer, event)`,带 `componentId` 者走组件级路由 | amis 6.13 的 show/hidden/setValue/static/reload **不是顶层注册动作**,是组件级动作(动作带 componentId 时由 amis 路由到组件实例 doAction);`toast` 是少数顶层动作。详见手册 §8.5 |
|
||||
| **改写产物关联** | §5.1「FULL_AUTO 直接替换;改写后脚本回填模板」 | 转换器 `attachScriptOnEvent(schema, templateId, fullAutoSeqs)` 按 `templateId` 关联,FULL_AUTO 脚本序号取自 `full_auto_index.json`,挂上 `inited`+`change` 两类 onEvent | 与设计一致,补充实现细节:每段脚本生成 `inited`(页面渲染时跑一遍)+`change`(字段变更重跑)双触发;动作链 = ajax 调执行端点 → replayScriptCommands 重放 |
|
||||
|
||||
**§5.3 服务端语义动词数据闭环、§5.4 componentId 大写列名对齐:与设计完全一致,实测验证通过**(见 §三端到端证据,office-collar 用例的 `getFieldValue("STORE_ID")` 从 item 求值闭环 + OLD_STORE_ID componentId 直对齐)。
|
||||
|
||||
---
|
||||
|
||||
## 二、实装清单(三段)
|
||||
|
||||
### 2.1 转换器侧 onEvent 生成段(platform-converter,事件段归本任务)
|
||||
|
||||
```
|
||||
platform-converter/src/main/java/com/eip4/platform/converter/
|
||||
└── TemplateToAmisConverter.java # 新增 attachScriptOnEvent + scriptActionChain(84 行纯追加,不动 source/picker 段)
|
||||
attachScriptOnEvent(pageSchema, templateId, fullAutoSeqs)
|
||||
→ 在页面 body(form/crud)挂 onEvent: {inited, change}
|
||||
→ 每段脚本动作链:
|
||||
{ actionType:"ajax", api:{method:post, url:/api/script/execute-template,
|
||||
data:{templateId, seq, item:"${&}"}}, outputVar:"scriptResultN" }
|
||||
{ actionType:"replayScriptCommands",
|
||||
args:{commands:"${event.data.scriptResultN.commands}"} }
|
||||
```
|
||||
|
||||
- `item:"${&}"` = amis 当前全量数据域,作脚本 `item` 上下文(服务端语义动词从此求值)。
|
||||
- componentId 用改写器输出大写列名,与 amis 字段 name 同源天然对齐(无映射表)。
|
||||
|
||||
### 2.2 执行端点(platform-metadata,ScriptExecuteController 归本任务)
|
||||
|
||||
```
|
||||
platform-metadata/src/main/java/com/eip4/platform/metadata/
|
||||
├── script/RewrittenScriptStore.java # 新增:按 (templateId, seq) 从改写产物取脚本正文(去头注释块)
|
||||
│ load(templateId, seq) → output/rewritten/<tid>/<seq>.*.groovy 去头注释
|
||||
│ 路径根 eip4.script.rewritten-root(默认 tools/s3_rewriter/output/rewritten)
|
||||
└── web/ScriptExecuteController.java # 扩展:
|
||||
POST /api/script/execute-template {templateId, seq, item} → 取改写脚本经 facade 执行 → 指令数组(P4-5 挂回主链路)
|
||||
POST /api/script/execute-template-by-name 按模板名取库内 XML 原始脚本(原 P4-2 端点,路径区隔保留兼容)
|
||||
POST /api/script/execute 直传 {script, item}(P4-2 既有,保留)
|
||||
```
|
||||
|
||||
### 2.3 前端重放胶水(eip4-web,AmisRenderer embed 范式)
|
||||
|
||||
```
|
||||
eip4-web/src/
|
||||
├── amis/scriptCommands.ts # 指令 → amis 动作翻译(29 UI 动词逐分支:show/hidden/static/
|
||||
│ setValue/setFilter/setOptions/addOption/addSubTableButton/
|
||||
│ focus/reload/toast/... → amis ActionSchema)
|
||||
├── amis/registerReplayAction.ts # 注册自定义动作 replayScriptCommands(幂等):
|
||||
│ run 内 translateCommands → 逐条 runActions 重放
|
||||
└── components/AmisRenderer.tsx # embed 前 registerReplayAction(amisRequire('amis'))(静默降级)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、端到端证据(≥2 真实模板,playwright,后端 8086 + 前端 dev)
|
||||
|
||||
**起服务**:后端 `java -jar platform-app-0.1.0-SNAPSHOT.jar --server.port=8086`(连客户库 5435,default profile,jar 经反应堆 `mvn -pl platform-app -am package` 重建含新端点,规避手册坑#1);前端 `VITE_BACKEND_TARGET=http://localhost:8086 vite --port 5174`(/api 反代 8086)。
|
||||
|
||||
挑 **2 个真实 FULL_AUTO facade-only 模板**(取自 `full_auto_index.json` ∩ 296 facade-only 集,且模板在 1,489 转换产物中):
|
||||
|
||||
### 用例 1:inited 重放 —— 福利保险结算单(隐藏字段)
|
||||
|
||||
| 项 | 内容 |
|
||||
|----|------|
|
||||
| 模板 | 福利保险结算单 `0d9eac07-1978-4abb-ab1f-f1561f9bd902` seq=1 |
|
||||
| 改写后脚本 | `setFieldVisible("BEGINDATE", false);`(FULL_AUTO,facade-only) |
|
||||
| 后端实测 | `POST /api/script/execute-template {templateId, seq:1, item:{}}` → `passed:true, commands:[{actionType:"hidden", componentId:"BEGINDATE"}]` |
|
||||
| 前端重放 | 页面渲染 → `inited` 触发 → ajax 取指令 → replayScriptCommands 重放 hidden → **BEGINDATE(起始日期)字段从 DOM 消失** |
|
||||
| playwright 断言 | `起始年` 可见 + `起始日期` 隐藏(`toBeHidden`),passed |
|
||||
| 截图 | `eip4-web/e2e/__screenshots__/welfare-hidden.png`(仅渲染出「起始年」,起始日期已被脚本隐藏) |
|
||||
|
||||
### 用例 2:change 重放 + 服务端 getFieldValue 闭环 —— 办公用品领用申请(回填字段值)
|
||||
|
||||
| 项 | 内容 |
|
||||
|----|------|
|
||||
| 模板 | 办公用品领用申请(按库领用)`021e3ba7-e03a-412a-9f0e-c20ef440a885` seq=2 |
|
||||
| 改写后脚本 | `addSubTableButton("GOODSDETAIL", "SELECT", 0);`<br>`bindField("OLD_STORE_ID", getFieldValue("STORE_ID"));`(FULL_AUTO,facade-only) |
|
||||
| 后端实测 | `{templateId, seq:2, item:{STORE_ID:"WH-7788"}}` → `passed:true, commands:[{addSubTableButton,GOODSDETAIL}, {setValue, OLD_STORE_ID, args:{value:"WH-7788", bind:true}}]`(**getFieldValue 服务端从 item 求值闭环**) |
|
||||
| 前端重放 | 改 STORE_ID=WH-7788 → `change` 触发 → ajax 传 item(含 STORE_ID)→ 脚本服务端求值 → 重放 setValue → **OLD_STORE_ID 自动回填为 WH-7788** |
|
||||
| playwright 断言 | `input[name=OLD_STORE_ID]` 值 == `WH-7788`(`toHaveValue`),passed |
|
||||
| 截图 | `eip4-web/e2e/__screenshots__/office-collar-setvalue.png`(仓库=WH-7788,原仓库(脚本回填)=WH-7788) |
|
||||
|
||||
**playwright 结果**:`2 passed`(chromium)。两用例覆盖 inited/change 双触发、UI 语义动词(hidden/setValue/addSubTableButton)重放、服务端语义动词(getFieldValue)数据闭环、componentId 大写列名对齐——链路四要素全验。
|
||||
|
||||
---
|
||||
|
||||
## 四、测试与口径
|
||||
|
||||
### 4.1 单测/组件测试(≥6,实交付 24)
|
||||
|
||||
| 仓 | 测试文件 | 数 | 覆盖 |
|
||||
|----|---------|----|------|
|
||||
| platform-metadata | `RewrittenScriptStoreTest` | 4 | 头注释剥离(不误删正文中部注释)、(tid,seq) 定位、缺目录/缺序号返回 null |
|
||||
| platform-metadata | `ScriptExecuteControllerTest` | 4 | 改写脚本取出执行产指令、服务端 getFieldValue 从 item 闭环、缺 templateId/未找到 passed=false、直执行兼容 |
|
||||
| platform-converter | `TemplateToAmisConverterTest`(新增 4 例) | +4 | onEvent inited+change 挂上、动作链调执行端点传 templateId/seq/item:${&}、多段累加、空入参不挂 |
|
||||
| eip4-web | `scriptCommands.test.ts` | 12 | 29 动词翻译核心分支(show/hidden/static/setValue/setOptions/addOption/setFilter/addSubTableButton/toast/reload/focus/未知跳过/数组过滤) |
|
||||
| eip4-web | `registerReplayAction.test.ts` | 4 | 缺 registerAction 降级、幂等注册、run 翻译后逐条 runActions、空指令不调 |
|
||||
| **小计** | | **24** | (后端 8 新增 + 转换器 4 新增 + 前端 16) |
|
||||
|
||||
### 4.2 两仓 build/test 全绿
|
||||
|
||||
| 仓 | 命令 | 结果 |
|
||||
|----|------|------|
|
||||
| eip4-platform | `mvn -o -pl platform-converter test` | Tests run: 28, Failures: 0, Errors: 0 ✅ |
|
||||
| eip4-platform | `mvn -o -pl platform-metadata test` | Tests run: 77, Failures: 0, Errors: 0 ✅(含新增 8) |
|
||||
| eip4-platform | `mvn -o -pl platform-app test -Dtest=ArchitectureRulesTest` | Tests run: 1, Failures: 0(**ArchUnit 保真层禁依赖现代层 复跑通过**)✅ |
|
||||
| eip4-web | `npm run test`(vitest) | Test Files 2 passed, Tests 16 passed ✅ |
|
||||
| eip4-web | `npm run build`(tsc -b && vite build) | 97 modules transformed, built ✅ |
|
||||
|
||||
### 4.3 端到端口径(诚实声明)
|
||||
|
||||
- **schema 来源**:前端 mock 源(`public/schemas/{welfare-settlement,office-collar}.json`),onEvent 形态 = 转换器 `attachScriptOnEvent` 产出(已单测固定,逐字段对齐)。原因:后端 schema 下发端点 `/api/page/schema/{id}`(`schema.ts` 契约)本期为 mock 源,未接后端转换器(属 picker/schema 代理范围,本任务不动);故 e2e 用 mock schema 承接 onEvent,**onEvent 真实性由转换器单测保证**。
|
||||
- **脚本与指令**:来自后端真实改写产物 `output/rewritten/<tid>/<seq>.FULL_AUTO.groovy` 经真实保真层 facade 引擎执行(非桩,`/api/script/execute-template` 实测产指令)。
|
||||
- **造数口径**:测试页字段(BEGINYEAR/BEGINDATE、STORE_ID/OLD_STORE_ID)为最小造数表单,字段 name = 脚本 componentId(大写列名)对齐;不依赖业务实体真实数据,纯验证「指令重放生效」(显隐/赋值)。本地库无这些业务实体数据,故用最小造数 form 承接,**这是验证「脚本挂回重放」机制的充分口径**(机制与具体实体数据正交)。
|
||||
- **登录态**:种入任意非空令牌过 RequireAuth(schema 走 mock 免后端鉴权;`/api/script/execute-template` 走后端 POC 默认链不拦截)。
|
||||
|
||||
---
|
||||
|
||||
## 五、坑列表(新坑,已同步迁移手册 §8.5 / §8.6)
|
||||
|
||||
1. **amis show/hidden/setValue/static 不是顶层注册动作,须经组件级路由**(手册 §8.5):amis 6.13 `registerAction` 顶层动作仅 ajax/dialog/toast/... 30 个,无 show/hidden/setValue/static/reload;这些是组件级动作,动作带 componentId 时由 amis 路由到组件 doAction。对策:`replayScriptCommands` 里对每条指令调 `runActions(action, renderer, event)`,带 componentId 自动组件级路由,toast 走顶层。
|
||||
2. **改写后脚本挂回走「改写产物按 templateId+seq 取数」而非「库内 XML 原始脚本」**(手册 §8.6):第五章衔接接口写法(前端直传脚本 / 按名取库内原始脚本)均非正解;挂回的是改写后脚本,取数键 `(templateId, seq)` 与落盘命名一致。对策:`/api/script/execute-template` 改 `{templateId,seq,item}` + RewrittenScriptStore 取数;原按名端点改 `-by-name` 保留兼容。
|
||||
3. **setOptions/addOption 类「改组件 options」动词在原生 amis 无对应组件动作**:select 选项联动(clearFieldData + addFieldOption×N)翻译为 setValue/addOption 透传,但 amis 原生组件 doAction 不直接消费逐条 addOption;select 选项联动的稳妥落点是后端 source 或整体 setValue 替换 options(非逐条 addOption),落 P4 中段后续。本任务 e2e 选 hidden/setValue 这类原生组件动作可路由的动词验证机制,setOptions 类已在后端指令产出层验证(指令 JSON 正确),前端原生消费待 P4 中段补 select options 重放适配。
|
||||
4. **改写产物随应用可达性**:执行端点按 `eip4.script.rewritten-root`(默认相对工作目录 `tools/s3_rewriter/output/rewritten`)读改写脚本;起服务工作目录须为工程根(否则取不到脚本,端点返回「改写后脚本未找到」)。部署须把改写产物随应用打包/挂载并配置该路径。
|
||||
|
||||
---
|
||||
|
||||
## 六、提交(两仓各自,只 add 本任务文件)
|
||||
|
||||
### eip4-platform(本任务文件,与 picker/schema/flow-gateway 代理文件隔离)
|
||||
```
|
||||
platform-converter/src/main/java/.../TemplateToAmisConverter.java # 仅 attachScriptOnEvent 段(84 行纯追加)
|
||||
platform-converter/src/test/java/.../TemplateToAmisConverterTest.java # 仅新增 4 onEvent 例
|
||||
platform-metadata/src/main/java/.../web/ScriptExecuteController.java
|
||||
platform-metadata/src/main/java/.../script/RewrittenScriptStore.java
|
||||
platform-metadata/src/test/java/.../web/ScriptExecuteControllerTest.java
|
||||
platform-metadata/src/test/java/.../script/RewrittenScriptStoreTest.java
|
||||
docs/迁移手册.md # §8.5 / §8.6 新坑
|
||||
tools/s3_rewriter/P4脚本挂回实测报告.md # 本报告
|
||||
```
|
||||
**勿 add**:platform-flow-gateway/*、PickerController、PageSchemaController、schema/*、bootstrap/、docs/sql/(另两代理范围)。
|
||||
|
||||
### eip4-web
|
||||
```
|
||||
src/amis/scriptCommands.ts / scriptCommands.test.ts
|
||||
src/amis/registerReplayAction.ts / registerReplayAction.test.ts
|
||||
src/components/AmisRenderer.tsx # 注册 replayScriptCommands
|
||||
src/api/schema.ts # mock map 加 2 个 e2e schema
|
||||
public/schemas/{welfare-settlement,office-collar}.json
|
||||
e2e/script-replay.spec.ts + e2e/__screenshots__/*.png
|
||||
playwright.config.ts / vitest.config.ts
|
||||
tsconfig.app.json(排除 *.test)/ .gitignore(忽略 test-results)
|
||||
package.json / package-lock.json(devDeps: vitest/jsdom/testing-library/playwright + test script)
|
||||
```
|
||||
|
||||
集成结束已 kill 8086 + dev 5174。
|
||||
@@ -239,6 +239,31 @@ ArchUnit 架构纪律单测(保真层禁止依赖现代层)复跑 1 passed
|
||||
- 待 P4 中段实装(与 P4-1 转换器协同):转换器把改写后脚本回填进 amis schema 的 onEvent 配置 +
|
||||
前端事件动作重放胶水(eip4-web 侧)。衔接接口(执行端点契约、componentId 对齐口径)本报告已固定。
|
||||
|
||||
### 5.3 P4-5 实装回写(2026-06-11,以实装为准)
|
||||
|
||||
> 上述 §5.1/§5.2 为衔接「设计」。P4-5 已实装为可运行链路(onEvent 回填 + 执行端点指令重放 +
|
||||
> componentId 大写列名对齐 + item 数据闭环),端到端 playwright 实测 2 真实模板通过。
|
||||
> **有三处与设计出入,以下以实装为准**(详见 `tools/s3_rewriter/P4脚本挂回实测报告.md` §一、手册 §8.5/§8.6):
|
||||
|
||||
1. **执行端点契约**:§5.2 写「衔接接口 `/api/script/execute`(前端直传 `{script,item}`)」;实装改为
|
||||
`/api/script/execute-template` 接受 `{templateId, seq, item}`(RequestBody),后端 `RewrittenScriptStore`
|
||||
按 `(templateId, seq)` 从**改写产物** `output/rewritten/<tid>/<seq>.FULL_AUTO.groovy`(去头注释)取脚本执行——
|
||||
挂回的是改写后脚本而非库内原始 S3,且免前端直传脚本文本的注入面。原「按模板名取库内 XML 脚本」端点
|
||||
路径区隔为 `/api/script/execute-template-by-name`(保留 P4-2 §3.2 兼容)。
|
||||
|
||||
2. **指令重放机制**:§5.2 写「前端 `dispatchEvent` 重放」;实装为前端注册自定义 amis 动作
|
||||
`replayScriptCommands`,对每条翻译后指令调 amis-core `runActions(action, renderer, event)`。原因:
|
||||
amis 6.13 的 show/hidden/setValue/static/reload **不是顶层注册动作**,是组件级动作(动作带 componentId
|
||||
时由 amis 路由到组件实例 doAction);toast 是少数顶层动作(手册 §8.5)。
|
||||
|
||||
3. **onEvent 回填实现**:转换器 `TemplateToAmisConverter.attachScriptOnEvent(schema, templateId, fullAutoSeqs)`
|
||||
在页面 body(form/crud)挂 `inited`+`change` 两类 onEvent;每段脚本动作链 = ajax 调执行端点
|
||||
(传 `templateId/seq` + `item:${&}` 全量表单数据)→ replayScriptCommands 重放返回的 commands 数组。
|
||||
|
||||
> §5.3 服务端语义动词数据闭环(getFieldValue 从 item 求值)、§5.4 componentId 大写列名对齐:**与设计完全一致,
|
||||
> 实测通过**(office-collar 用例 `bindField("OLD_STORE_ID", getFieldValue("STORE_ID"))` 服务端求值 + OLD_STORE_ID
|
||||
> componentId 直对齐,前端回填生效)。
|
||||
|
||||
---
|
||||
|
||||
## 六、坑列表(新坑,已同步迁移手册)
|
||||
|
||||
Reference in New Issue
Block a user