feat(report): /api/report/frame 端点 + Phase 2/3 报表查看器设计
报表查看器数据源端点 GET /api/report/frame/{templateId}:回显 amis CRUD 单行信封
{status:0,data:{items:[{__url}],total:1}},__url=/api/report/render/{id}?format=html&<查询参数>。
报表页是 amis crud——查询条件落 crud.filter(实测唯一能让 picker 正常加载选项的上下文;
独立 form 里 picker 弹窗恒空),单 html 列把 __url 渲成 <iframe>,JOR HTML 独立加载、CSS 隔离保真。
「选条件→查询」时 crud 携 filter 参数请求 frame→拿到带新参数的 __url→iframe 重载→按工程过滤刷新。
- reportParamsFrom:frame 专用,剥 format/tpl/page/perPage(crud 自动追加分页位),多值取首、跳空。
- buildFrameUrl:templateId 路径段 + 参数键值全 UTF-8 URL 编码,防注入(__url 注入 iframe src,
编码确保 " & # / < > 不破坏 query 结构或逃逸属性);templateId 实为 view_template_id(UUID)编码无副作用。
- frameResult:单行信封。collectReportParams(/render)保持原有 format/tpl-only 行为不变(与 frame 分流)。
- ReportFrameTest:5 个纯逻辑单测(剥参/编码/信封/注入中和)。
design(含 Phase 2/3 实现纪要 + 认证根因)见 docs/plans/2026-06-25-报表迁移-JOR引擎重宿主-design.md §6。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package com.eip4.platform.report;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
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 java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -95,6 +99,83 @@ public class ReportController {
|
||||
.body(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* 报表查看器数据源(Phase 3):{@code GET /api/report/frame/{templateId}?<查询参数...>}
|
||||
* → 返回 amis CRUD 单行信封 {@code {status:0,msg:"",data:{items:[{__url}],total:1}}},
|
||||
* {@code __url} = {@code /api/report/render/{id}?format=html&<回显的查询参数>}。
|
||||
*
|
||||
* <p>报表页是 amis {@code crud}:查询条件放在 {@code crud.filter}(实测唯一能让 picker
|
||||
* 正常加载选项的上下文;放在独立 form 里 picker 弹窗「暂无数据」)。crud 的单个 html 列把
|
||||
* {@code __url} 渲成 {@code <iframe>}。用户「选工程→查询」时 crud 携 filter 参数请求本端点,
|
||||
* 拿到带新参数的 {@code __url} → iframe 重新加载 → 报表按工程过滤刷新。本端点只回显 URL(不渲染),
|
||||
* 真正的 JOR 渲染由 iframe 加载 {@code /api/report/render} 完成(HTML 文档独立,CSS 隔离保真)。
|
||||
*/
|
||||
@GetMapping("/api/report/frame/{templateId}")
|
||||
public Map<String, Object> frame(@PathVariable("templateId") String templateId,
|
||||
HttpServletRequest request) {
|
||||
Map<String, String> params = reportParamsFrom(request.getParameterMap());
|
||||
return frameResult(buildFrameUrl(templateId, params));
|
||||
}
|
||||
|
||||
/**
|
||||
* frame 端点的非报表参数:渲染控制位(format/tpl)+ amis crud 自动追加的分页位(page/perPage)。
|
||||
* 注:{@code /render} 端点不剥分页位(见 {@link #collectReportParams}),保持其原有行为不变。
|
||||
*/
|
||||
private static final Set<String> FRAME_NON_REPORT_PARAMS = Set.of("format", "tpl", "page", "perPage");
|
||||
|
||||
/**
|
||||
* 从原始请求参数表抽出报表参数(剔除 {@link #FRAME_NON_REPORT_PARAMS},多值取第一个,跳过空/null)。
|
||||
* frame 端点专用——crud 会带 page/perPage,需剥掉。报表 SQL 里的 {@code ${PARAM}} 据此绑定。纯函数,便于单测。
|
||||
*/
|
||||
static Map<String, String> reportParamsFrom(Map<String, String[]> raw) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
if (raw == null) {
|
||||
return params;
|
||||
}
|
||||
raw.forEach((name, values) -> {
|
||||
if (name == null || FRAME_NON_REPORT_PARAMS.contains(name)) {
|
||||
return;
|
||||
}
|
||||
if (values != null && values.length > 0 && values[0] != null) {
|
||||
params.put(name, values[0]);
|
||||
}
|
||||
});
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 iframe 用的 HTML 渲染 URL:{@code /api/report/render/{id}?format=html&k=v&...}。
|
||||
* {@code templateId} 路径段与各参数键值均经 UTF-8 URL 编码 —— 该 URL 最终被注入 amis html 列的
|
||||
* {@code <iframe src="${__url}">},编码确保任何特殊字符({@code " & # / < >})都不会破坏 query 结构
|
||||
* 或逃逸出 src 属性(防注入,安全评审防御纵深)。templateId 实为 view_template_id(UUID),编码对其无副作用。
|
||||
*/
|
||||
static String buildFrameUrl(String templateId, Map<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder("/api/report/render/")
|
||||
.append(URLEncoder.encode(templateId == null ? "" : templateId, StandardCharsets.UTF_8))
|
||||
.append("?format=html");
|
||||
if (params != null) {
|
||||
params.forEach((k, v) -> sb.append('&')
|
||||
.append(URLEncoder.encode(k, StandardCharsets.UTF_8))
|
||||
.append('=')
|
||||
.append(URLEncoder.encode(v == null ? "" : v, StandardCharsets.UTF_8)));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** 把单条 URL 包成 amis CRUD 单行信封:{@code {status:0,msg:"",data:{items:[{__url}],total:1}}}。 */
|
||||
static Map<String, Object> frameResult(String url) {
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("__url", url);
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("items", List.of(item));
|
||||
data.put("total", 1);
|
||||
Map<String, Object> env = new LinkedHashMap<>();
|
||||
env.put("status", 0);
|
||||
env.put("msg", "");
|
||||
env.put("data", data);
|
||||
return env;
|
||||
}
|
||||
|
||||
/** templateId / ?tpl 解析为模板文件路径。 */
|
||||
private Path resolveTemplate(String templateId, String tplPath) {
|
||||
Path xml;
|
||||
@@ -114,7 +195,11 @@ public class ReportController {
|
||||
return xml;
|
||||
}
|
||||
|
||||
/** 除 format/tpl 外的查询参数 = 报表参数(只取第一个值)。 */
|
||||
/**
|
||||
* {@code /render} 端点的报表参数 = 请求参数剔除渲染控制位 {@code format}/{@code tpl}(仅此两位)。
|
||||
* 不剥 {@code page}/{@code perPage} —— 保持本端点<strong>原有行为不变</strong>(与 frame 端点的
|
||||
* {@link #reportParamsFrom} 有意分流:仅 crud 驱动的 frame 才会带分页位需要剥离)。
|
||||
*/
|
||||
private Map<String, String> collectReportParams(HttpServletRequest request) {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
request.getParameterMap().forEach((name, values) -> {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.eip4.platform.report;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Phase 3 报表查看器:{@code GET /api/report/frame/{templateId}} 的纯逻辑单测。
|
||||
*
|
||||
* <p>该端点把报表查询参数回显成 amis CRUD 单行信封 {@code {status:0,data:{items:[{__url}],total:1}}},
|
||||
* {@code __url} 指向 {@code /api/report/render/{id}?format=html&<查询参数>}。报表页是一个 amis {@code crud}
|
||||
* (查询条件落在 {@code crud.filter} —— 唯一能让 picker 正常加载的上下文),其单个 html 列把 {@code __url}
|
||||
* 渲成 {@code <iframe>},于是「选工程→查询」时 crud 携 filter 参数请求本端点 → iframe 刷新报表。
|
||||
*/
|
||||
class ReportFrameTest {
|
||||
|
||||
@Test
|
||||
void reportParamsFrom_strips_crud_and_format_control_params() {
|
||||
Map<String, String[]> raw = new LinkedHashMap<>();
|
||||
raw.put("PROJECTID", new String[]{"P-001"});
|
||||
raw.put("format", new String[]{"amis"});
|
||||
raw.put("tpl", new String[]{"/abs/x.xml"});
|
||||
raw.put("page", new String[]{"1"});
|
||||
raw.put("perPage", new String[]{"10"});
|
||||
|
||||
Map<String, String> params = ReportController.reportParamsFrom(raw);
|
||||
|
||||
assertEquals("P-001", params.get("PROJECTID"));
|
||||
assertFalse(params.containsKey("format"), "format 应被剔除");
|
||||
assertFalse(params.containsKey("tpl"), "tpl 应被剔除");
|
||||
assertFalse(params.containsKey("page"), "page 应被剔除");
|
||||
assertFalse(params.containsKey("perPage"), "perPage 应被剔除");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportParamsFrom_keeps_first_value_and_skips_blank_keys() {
|
||||
Map<String, String[]> raw = new LinkedHashMap<>();
|
||||
raw.put("A", new String[]{"first", "second"});
|
||||
raw.put("B", new String[]{}); // 空值数组 → 跳过
|
||||
raw.put("C", null); // null → 跳过
|
||||
|
||||
Map<String, String> params = ReportController.reportParamsFrom(raw);
|
||||
|
||||
assertEquals("first", params.get("A"));
|
||||
assertFalse(params.containsKey("B"));
|
||||
assertFalse(params.containsKey("C"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildFrameUrl_points_at_html_render_and_url_encodes_params() {
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("PROJECTID", "66e5/7e6d &x"); // 含需转义字符
|
||||
params.put("DEPT", "研发部"); // 中文
|
||||
|
||||
String url = ReportController.buildFrameUrl("5558497f-id", params);
|
||||
|
||||
assertTrue(url.startsWith("/api/report/render/5558497f-id?format=html"), url);
|
||||
// 值被 URL 编码(空格/&// 不会破坏 query 结构;中文按 UTF-8 %xx)
|
||||
assertTrue(url.contains("PROJECTID=66e5%2F7e6d+%26x"), url);
|
||||
assertTrue(url.contains("DEPT=%E7%A0%94%E5%8F%91%E9%83%A8"), url);
|
||||
// 未编码的裸 "&x" 不应出现(否则会被当成新参数 x)
|
||||
assertFalse(url.contains("&x"), url);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildFrameUrl_no_params_is_just_html_render() {
|
||||
String url = ReportController.buildFrameUrl("abc", new LinkedHashMap<>());
|
||||
assertEquals("/api/report/render/abc?format=html", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void frameResult_is_single_row_amis_crud_envelope() {
|
||||
Map<String, Object> env = ReportController.frameResult("/api/report/render/x?format=html");
|
||||
|
||||
assertEquals(0, env.get("status"));
|
||||
Map<String, Object> data = (Map<String, Object>) env.get("data");
|
||||
assertEquals(1, data.get("total"));
|
||||
List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("items");
|
||||
assertEquals(1, items.size());
|
||||
assertEquals("/api/report/render/x?format=html", items.get(0).get("__url"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user