From 3b999b4fe9069db8cc03c086a84c018f5c4f4750 Mon Sep 17 00:00:00 2001 From: Raod <1130305001@qq.com> Date: Thu, 16 Mar 2023 19:29:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96----js=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=87=AA=E5=AE=9A=E4=B9=89=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=8D=E6=94=AF=E6=8C=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/JsTransformServiceImpl.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java index a33a7dff..c236155e 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/datasettransform/service/impl/JsTransformServiceImpl.java @@ -5,13 +5,14 @@ import com.anji.plus.gaea.exception.BusinessExceptionBuilder; import com.anjiplus.template.gaea.business.code.ResponseCode; import com.anjiplus.template.gaea.business.modules.datasettransform.controller.dto.DataSetTransformDto; import com.anjiplus.template.gaea.business.modules.datasettransform.service.TransformStrategy; -import com.fasterxml.jackson.databind.ObjectMapper; +import jdk.nashorn.api.scripting.ScriptObjectMirror; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; +import java.util.ArrayList; import java.util.List; /** @@ -54,7 +55,20 @@ public class JsTransformServiceImpl implements TransformStrategy { engine.eval(js); if(engine instanceof Invocable){ Invocable invocable = (Invocable) engine; - return (List) invocable.invokeFunction("dataTransform", data); + Object dataTransform = invocable.invokeFunction("dataTransform", data); + if (dataTransform instanceof List) { + return (List) dataTransform; + } + //前端js自定义的数组[{"aa":"bb"}]解析后变成{"0":{"aa":"bb"}} + ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) dataTransform; + List result = new ArrayList<>(); + scriptObjectMirror.forEach((key, value) -> { + ScriptObjectMirror valueObject = (ScriptObjectMirror) value; + JSONObject jsonObject = new JSONObject(); + jsonObject.putAll(valueObject); + result.add(jsonObject); + }); + return result; } } catch (Exception ex) {