js脚本执行修改

Raod 3 years ago
parent 71ec8a1b73
commit f8a84768e2

@ -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());

@ -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<JSONObject> getValueFromJs(DataSetTransformDto def, List<JSONObject> 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;
}
}

Loading…
Cancel
Save