JScriptUtil.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package jnpf.util;
  2. import org.springframework.util.ReflectionUtils;
  3. import javax.script.ScriptEngine;
  4. import javax.script.ScriptEngineFactory;
  5. import javax.script.ScriptEngineManager;
  6. import javax.script.ScriptException;
  7. import java.lang.reflect.Method;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * Java执行js代码工具类
  12. *
  13. * @author :JNPF开发平台组
  14. * @version: V3.1.0
  15. * @copyright 引迈信息技术有限公司
  16. * @date :2022/5/28 9:44
  17. */
  18. public class JScriptUtil {
  19. private static ScriptEngineFactory scriptEngineFactory;
  20. private static Method createMathod;
  21. static {
  22. ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
  23. if(!scriptEngineManager.getEngineFactories().isEmpty()) {
  24. scriptEngineFactory = scriptEngineManager.getEngineFactories().get(0);
  25. createMathod = ReflectionUtils.findMethod(scriptEngineFactory.getClass(), "getScriptEngine", String[].class);
  26. }else{
  27. throw new RuntimeException("JS引擎为空");
  28. }
  29. }
  30. /**
  31. * 数据接口通用定义函数
  32. */
  33. public static final String JSCONTENT = "var method = function(data) {" +
  34. "${jsContent}" +
  35. "};" +
  36. "var result = method(${data});" +
  37. "if(typeof(result)=='object'){JSON.stringify(result);}else{result;};;";
  38. /**
  39. * 调用js代码
  40. * @param script 脚本内容
  41. * @return 如果JS内返回的是对象 返回内容为ScriptObjectMirror
  42. * @throws ScriptException
  43. */
  44. public static Object callJs(String script) throws ScriptException {
  45. ScriptEngine scriptEngine = null;
  46. try {
  47. scriptEngine = (ScriptEngine) createMathod.invoke(scriptEngineFactory, new Object[]{new String[]{"--no-java"}});
  48. } catch (Exception e) {
  49. throw new RuntimeException("创建JS引擎失败", e);
  50. }
  51. return scriptEngine.eval(script);
  52. }
  53. /**
  54. * 调用js代码, 处理JSON数据 返回JSON数据
  55. *
  56. * @param dataProcessing 数据处理函数
  57. * @param data JSON对象/数组
  58. * @return JSON对象/数组
  59. */
  60. public static Object callJs(String dataProcessing, Object data) throws ScriptException {
  61. String jsContent = getJsContent(dataProcessing);
  62. if (StringUtil.isEmpty(dataProcessing)) {
  63. return data;
  64. }
  65. String replace = JSCONTENT.replace("${jsContent}", jsContent);
  66. replace = replace.replace("${data}", JsonUtil.getObjectToString(data));
  67. Object result = null;
  68. try {
  69. result = callJs(replace);
  70. } catch (Exception e) {
  71. throw e;
  72. }
  73. try {
  74. List<Map<String, Object>> jsonToListMap = JsonUtil.getJsonToListMap(result.toString());
  75. return jsonToListMap;
  76. } catch (Exception e) {
  77. try {
  78. Map<String, Object> map = JsonUtil.stringToMap(result.toString());
  79. return map;
  80. } catch (Exception ee) {
  81. return result;
  82. }
  83. }
  84. }
  85. /**
  86. * 返回js内容
  87. *
  88. * @param dataProcessing
  89. * @return
  90. */
  91. public static String getJsContent(String dataProcessing) {
  92. if (StringUtil.isNotEmpty(dataProcessing) && dataProcessing.length() > 0) {
  93. // 获取位置
  94. int indexOf = dataProcessing.indexOf("{");
  95. if (indexOf > -1) {
  96. dataProcessing = dataProcessing.substring(indexOf + 1);
  97. }
  98. int lastIndexOf = dataProcessing.lastIndexOf("}");
  99. if (lastIndexOf > -1) {
  100. dataProcessing = dataProcessing.substring(0, lastIndexOf);
  101. }
  102. return dataProcessing;
  103. }
  104. return "";
  105. }
  106. }