diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSetParam/service/impl/DataSetParamServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSetParam/service/impl/DataSetParamServiceImpl.java index 2a351563..40fc5183 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSetParam/service/impl/DataSetParamServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSetParam/service/impl/DataSetParamServiceImpl.java @@ -9,11 +9,13 @@ import com.anjiplus.template.gaea.business.modules.dataSetParam.dao.entity.DataS import com.anjiplus.template.gaea.business.modules.dataSetParam.service.DataSetParamService; import com.anjiplus.template.gaea.business.modules.dataSetParam.util.ParamsResolverHelper; import com.anjiplus.template.gaea.business.code.ResponseCode; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.util.HashMap; @@ -93,13 +95,16 @@ public class DataSetParamServiceImpl implements DataSetParamService { @Override public boolean verification(DataSetParamDto dataSetParamDto) { - String sampleItem = dataSetParamDto.getSampleItem(); String validationRules = dataSetParamDto.getValidationRules(); if (StringUtils.isNotBlank(validationRules)) { - validationRules = validationRules + "\nvar result = verification('" + sampleItem + "');"; try { engine.eval(validationRules); - return Boolean.parseBoolean(engine.get("result").toString()); + if(engine instanceof Invocable){ + Invocable invocable = (Invocable) engine; + Object exec = invocable.invokeFunction("verification", dataSetParamDto); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.convertValue(exec, Boolean.class); + } } catch (Exception ex) { throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_JS_ERROR, ex.getMessage()); 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 5744457c..941cb96c 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 @@ -2,17 +2,17 @@ package com.anjiplus.template.gaea.business.modules.dataSetTransform.service.imp import com.alibaba.fastjson.JSONObject; 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.anjiplus.template.gaea.business.code.ResponseCode; -import jdk.nashorn.api.scripting.ScriptObjectMirror; +import com.fasterxml.jackson.databind.ObjectMapper; 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.List; -import java.util.stream.Collectors; /** * Created by raodeming on 2021/3/23. @@ -50,13 +50,18 @@ public class JsTransformServiceImpl implements TransformStrategy { private List getValueFromJs(DataSetTransformDto def, List data) { String js = def.getTransformScript(); - js = js + "\nvar result = dataTransform(eval(" + data.toString() + "));"; try { engine.eval(js); - ScriptObjectMirror result = (ScriptObjectMirror) engine.get("result"); - return result.values().stream().map(o -> JSONObject.parseObject(JSONObject.toJSONString(o))).collect(Collectors.toList()); + if(engine instanceof Invocable){ + Invocable invocable = (Invocable) engine; + Object exec = invocable.invokeFunction("dataTransform", data); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.convertValue(exec, List.class); + } + } catch (Exception ex) { throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_JS_ERROR, ex.getMessage()); } + return null; } }