F2-8: 写链路异常友好响应 + 附件控件映射

- 新增 WriteExceptionHandler:ValidationException→422、PrivilegeException→403、
  RuntimeException→500,均返回 amis 约定 {status,msg},不再裸透 Spring 500
- UiTypeMapper 新增 MultiFileUploadField/FileUploadField → input-file
  (receiver=/api/file/upload, downloadUrl=/api/file/${id}, multiple)
- 转换器单测新增 form_附件字段MultiFileUploadField转inputFile(28/28 pass)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 00:47:31 +08:00
parent 2d8a16eef6
commit e876327c75
3 changed files with 135 additions and 0 deletions

View File

@@ -132,6 +132,23 @@ final class UiTypeMapper {
result.degrade(origin + "@field " + name + " uitype=MultiSelectDialog(dialogid=" result.degrade(origin + "@field " + name + " uitype=MultiSelectDialog(dialogid="
+ f.attributeValue("dialogid", "") + ") → select(multiple)+source 占位(数据源待实现)"); + f.attributeValue("dialogid", "") + ") → select(multiple)+source 占位(数据源待实现)");
} }
case "MultiFileUploadField", "FileUploadField" -> {
// F2-8 附件控件:旧引擎 MultiFileUploadField/FileUploadField → amis input-file。
// receiver=/api/file/upload(amis multipart 默认字段名 file),
// 下载链 /api/file/${id}(FileController.download)。
// downloadUrl 让 amis 在已上传列表显示可点击下载链。
boolean multi = "MultiFileUploadField".equals(uitype);
item.put("type", "input-file");
item.put("receiver", "/api/file/upload");
item.put("downloadUrl", "/api/file/${id}");
if (multi) {
item.put("multiple", true);
}
// amis input-file 默认 accept 全部类型;maxSize 暂不限制
result.note(origin + "@field " + name + " uitype=" + uitype
+ " → input-file(receiver=/api/file/upload, downloadUrl=/api/file/${id}"
+ (multi ? ", multiple" : "") + ")");
}
default -> { default -> {
item.put("type", "input-text"); item.put("type", "input-text");
result.degrade(origin + "@field " + name + " uitype=" + uitype result.degrade(origin + "@field " + name + " uitype=" + uitype

View File

@@ -188,6 +188,41 @@ class TemplateToAmisConverterTest {
assertTrue(r.getDegrades().stream().anyMatch(d -> d.contains("view_page"))); assertTrue(r.getDegrades().stream().anyMatch(d -> d.contains("view_page")));
} }
@Test
@SuppressWarnings("unchecked")
void form_附件字段MultiFileUploadField转inputFile() {
// F2-8 实证:MultiFileUploadField / FileUploadField → input-file(receiver + downloadUrl)
String pageXml = "<page entityid='e9'><fields>"
+ "<field name='TITLE' label='主题' uitype='TextField' required='true'/>"
+ "<field name='DOCUMENT' label='附件' uitype='MultiFileUploadField'/>"
+ "<field name='ATTACH2' label='单附件' uitype='FileUploadField'/>"
+ "</fields></page>";
Function<String, String> resolver = id -> pageXml;
String xml = "<form entityid='e1' pageid='p1'/>";
List<String> notes = new ArrayList<>();
ConversionResult r = converter.convertGraded(xml, "附件测试表单", notes, resolver);
Map<String, Object> form = body(r.getSchema());
List<Map<String, Object>> items = (List<Map<String, Object>>) form.get("body");
assertEquals(3, items.size());
// MultiFileUploadField → input-file, multiple=true
Map<String, Object> docField = items.get(1);
assertEquals("input-file", docField.get("type"));
assertEquals("/api/file/upload", docField.get("receiver"));
assertEquals("/api/file/${id}", docField.get("downloadUrl"));
assertEquals(true, docField.get("multiple"));
// FileUploadField → input-file, no multiple
Map<String, Object> singleField = items.get(2);
assertEquals("input-file", singleField.get("type"));
assertEquals("/api/file/upload", singleField.get("receiver"));
assertNull(singleField.get("multiple"));
// 映射台账中有 input-file 记录
assertTrue(notes.stream().anyMatch(n -> n.contains("input-file")));
}
// ── tree ───────────────────────────────────────────────────────────── // ── tree ─────────────────────────────────────────────────────────────
@Test @Test

View File

@@ -0,0 +1,83 @@
package com.eip4.platform.metadata.web;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.dsasp.server.common.exception.PrivilegeException;
import com.dsasp.server.common.exception.ValidationException;
/**
* F2-8 写链路异常 → amis 友好响应。
*
* <p>拦截 EntityWriteController / FileController 抛出的业务异常,
* 转换成 amis 约定格式,避免 Spring 默认 500 裸 JSON 透传前端。
*
* <ul>
* <li>{@link ValidationException} — 业务校验失败(必填/重复/约束等)→
* HTTP 422 + {@code {status:422, msg}} (amis form 默认对 status≠0 显示顶部错误提示)</li>
* <li>{@link PrivilegeException} — 数据权限拒绝 →
* HTTP 403 + {@code {status:403, msg}}</li>
* </ul>
*
* <p>纪律:零改 {@code com.dsasp};不依赖 Spring Validation(引擎校验经 ValidationException 抛出)。
*/
@RestControllerAdvice(assignableTypes = {EntityWriteController.class, FileController.class})
public class WriteExceptionHandler {
/**
* 业务校验失败(引擎 ValidationException)→ HTTP 422 amis 格式。
*
* <p>amis form 约定:{@code status≠0} 时显示顶部错误提示({@code msg} 字段);
* 若需字段级行内错误则需 {@code status:422, errors:{fieldName:msg}},
* 但引擎 ValidationException 消息不含字段名(如"分类是必填项"),
* 故只返回 msg,amis form 会在顶部 toast/alert 区显示。
*/
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Map<String, Object>> handleValidation(ValidationException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 422);
body.put("msg", ex.getMessage() != null ? ex.getMessage() : "数据校验失败,请检查输入");
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(body);
}
/**
* 数据权限拒绝(写侧 PrivilegeException)→ HTTP 403 amis 格式。
*
* <p>amis form 收到 403 时默认显示 msg 文案;前端不再裸显 Spring 默认 403 JSON。
*/
@ExceptionHandler(PrivilegeException.class)
public ResponseEntity<Map<String, Object>> handlePrivilege(PrivilegeException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 403);
body.put("msg", ex.getMessage() != null ? ex.getMessage() : "无操作权限");
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
}
/**
* 兜底:其他 RuntimeException(引擎 SQL/状态机异常等)→ HTTP 500 amis 格式。
*
* <p>把裸 Spring 500 JSON 替换为 amis 约定体,前端 amis form 可显示 msg 而非空白。
* 注意仅对 EntityWriteController / FileController 生效(assignableTypes 限定),
* 不干扰其他 Controller 的异常处理链。
*/
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<Map<String, Object>> handleRuntime(RuntimeException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("status", 1);
// 暴露简短消息(不含堆栈);内部引擎消息通常已是中文业务描述
String msg = ex.getMessage();
if (msg == null || msg.isBlank()) {
msg = "服务器内部错误,请稍后重试";
} else if (msg.length() > 200) {
// 截断过长的技术消息,防止泄露内部细节
msg = msg.substring(0, 200) + "…";
}
body.put("msg", msg);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body);
}
}