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:
@@ -39,3 +39,14 @@ EIP3 `ReportExporter` 的真实通道(`jor/jatools/server/ReportExporter.java`):
|
||||
- System2 连接为进程级全局(单客户部署 OK;多租户需改造)。
|
||||
- 模板里 `<Url>reportviewer.jsp?...`(钻取链接)需改写为 EIP4 路由(Phase 3)。
|
||||
- eip4dev 是**部分** dev 库,缺部分报表源表/视图;数据验收须对**完整客户库**。
|
||||
|
||||
## 6. Phase 2/3 实现纪要(2026-06-25,已完成+live验收)
|
||||
|
||||
报表页**不再手搭**——由转换器对 type=200 统一生成,且查询条件落在 amis `crud.filter`(实测唯一能让 picker 正常加载选项的上下文;独立 form 里 picker 弹窗恒空)。
|
||||
|
||||
- **后端** `GET /api/report/frame/{templateId}`(`ReportController`):回显 amis CRUD 单行信封 `{status:0,data:{items:[{__url}],total:1}}`,`__url`=`/api/report/render/{id}?format=html&<查询参数>`(templateId 路径段 + 参数键值全 UTF-8 URL 编码,防注入)。crud 的 html 列把 `__url` 渲成 `<iframe>`,JOR HTML 独立加载、CSS 隔离保真。
|
||||
- **转换器** `TemplateToAmisConverter.convertReport` 分流:含 `<reportfile>`(JOR 套打,328/339)→ `convertReportViewer` 产 `page→crud{api:/api/report/frame/{id}, filter:buildSearchFilter(查询条件), columns:[单 html 列 iframe], initFetch:false(有查询条件时等用户「查询」再加载)}`;无 reportfile(9/339,纯数据)→ 保留原数据表降级;2/339 为空 XML 孤儿模板(测试动态报表等),无产物属正常。
|
||||
- **认证根因修(关键)** `EngineContextBinder`:工程 picker「选不到」的真因**不是**转换器/页面结构,而是**认证 admin 的引擎身份没被识别成超管**——JWT 注入 `CURRENTUSER="ADMIN"`(5 字符),引擎跳过行级数据过滤的判定是 exact `ctx.get(CURRENTUSER).equals(LoggedInUser.ADMIN)`,而该常量是 CHAR(36) 补白的 `"ADMIN"+31 空格`,5 字符 ≠ 36 字符 → admin 被当普通用户走数据权限 → `PM_PROJECT` 无规则 → `PrivilegeException` →「暂无该数据源的查询权限」(无 token 的 curl 走 ADMIN 默认上下文,故误以为后端正常)。修=`bind()` 把系统管理员(userId trim=ADMIN,或 account 精确小写='admin',与全站 `requireAdmin` 同口径,不放宽大小写防变体账号提权)的 CURRENTUSER 归一为引擎规范 36 字符 `LoggedInUser.ADMIN`,引擎识别超管→跳过过滤。各 `requireAdmin` 门控均 `trim().equalsIgnoreCase`,对补白形式同样判真,不破鉴权门。
|
||||
- **live 验收**(8093 新 jar + 真库 + Playwright):工程 picker 列出全部 1672 工程→选中值落定→「搜索」→ crud 携 `PROJECTID` 调 frame→iframe `src=…?format=html&PROJECTID=…`→渲出 JOR 套打「在建项目收付款信息表」(单位/中标价/合同价/序号/工程名称/累计收款付款…);菜单 tab 内非独立页。339 报表页已批量重转重导 eip3.sys_page_schema(328 viewer 更新 + 9 表格不变)。
|
||||
- **评审**:独立 code-reviewer + security-reviewer;按建议收口 templateId 路径编码、`/render` 端点 page/perPage 行为保持不变(分流到 frame)、account 判定与 requireAdmin 对齐大小写敏感(防变体账号提权)。
|
||||
- **遗留**:headless 中文字体保真(版式通,需装字体)、PDF/Excel 下载入口、B 类图表(JOR jfreechart 已在 lib 随版式出图)、`<Url>` 钻取链接改 EIP4 路由、完整客户库数据对账。
|
||||
|
||||
@@ -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