wukai 2 месяцев назад
Родитель
Сommit
ef7659c100

+ 64 - 0
jjt-admin/src/test/java/com/test/JsTestT.java

@@ -0,0 +1,64 @@
+package com.test;
+
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+/**
+ * JsTest$
+ *
+ * @author wukai
+ * @date 2025/10/21 23:22
+ */
+public class JsTestT {
+    public static void main(String[] args) {
+        // 先用一个简单的测试函数
+        String simpleJsCode =
+                "function trendAnalysis(data, period) {\n" +
+                        "  console.log('=== 调试信息 ===');\n" +
+                        "  console.log('data:', data);\n" +
+                        "  console.log('typeof data:', typeof data);\n" +
+                        "  console.log('Array.isArray(data):', Array.isArray(data));\n" +
+                        "  console.log('data.length:', data.length);\n" +
+                        "  console.log('period:', period);\n" +
+                        "  \n" +
+                        "  // 检查数据\n" +
+                        "  if (data && typeof data === 'object' && 'length' in data) {\n" +
+                        "    console.log('数据验证通过');\n" +
+                        "    return '测试成功,数据长度:' + data.length;\n" +
+                        "  } else {\n" +
+                        "    return '数据格式错误';\n" +
+                        "  }\n" +
+                        "}";
+
+        Object[] data = new Integer[]{10, 12, 9, 15, 11, 16, 13};
+        String result = exec(simpleJsCode, "trendAnalysis",data, 5);
+        System.out.println("测试结果: " + result);
+    }
+
+    public static String exec(String jsCode, String functionName, Object[] data, Object... params) {
+        try {
+            ScriptEngineManager manager = new ScriptEngineManager();
+            ScriptEngine engine = manager.getEngineByName("graal.js");
+
+            // 执行脚本,注册函数
+            engine.eval(jsCode);
+
+            Invocable invocable = (Invocable) engine;
+
+            // 构建参数数组
+            Object[] allParams = new Object[params.length + 1];
+            allParams[0] = data;
+            System.arraycopy(params, 0, allParams, 1, params.length);
+
+            // 调用命名函数
+            Object result = invocable.invokeFunction(functionName, allParams);
+            return result.toString();
+
+        } catch (ScriptException | NoSuchMethodException e) {
+            e.printStackTrace();
+            return "执行失败: " + e.getMessage();
+        }
+    }
+}

+ 23 - 1
jjt-biz/src/main/java/com/jjt/biz/controller/RipaAlgoController.java

@@ -8,6 +8,7 @@ import com.jjt.common.core.domain.AjaxResult;
 import com.jjt.common.core.page.TableDataInfo;
 import com.jjt.common.enums.BusinessType;
 import com.jjt.common.utils.poi.ExcelUtil;
+import com.jjt.utils.JavaScriptUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.web.bind.annotation.*;
@@ -83,9 +84,30 @@ public class RipaAlgoController extends BaseController {
     @Log(title = "算法管理", businessType = BusinessType.UPDATE)
     @PostMapping("/edit")
     public AjaxResult edit(@RequestBody RipaAlgo ripaAlgo) {
-        return toAjax(ripaAlgoService.updateRipaAlgo(ripaAlgo));
+        String result;
+        try {
+            int length = 50;
+            Double[] data = new Double[length];
+            for (int i = 0; i < length; i++) {
+                data[i] = Math.random() * 100;
+            }
+            if ("1".equals(ripaAlgo.getAlgoPara())) {
+                result = JavaScriptUtil.exec(ripaAlgo.getAlgoScript(), data);
+            } else {
+                Double[] data1 = new Double[50];
+                for (int i = 0; i < length; i++) {
+                    data1[i] = Math.random() * 100;
+                }
+                result = JavaScriptUtil.exec(ripaAlgo.getAlgoScript(), data, data1);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return error("算法后台验证失败,错误信息:" + e.getMessage());
+        }
+        return toAjax(ripaAlgoService.updateRipaAlgo(ripaAlgo)).put("result", result);
     }
 
+
     /**
      * 删除算法管理
      */

+ 39 - 43
jjt-biz/src/main/java/com/jjt/utils/JavaScriptUtil.java

@@ -3,7 +3,6 @@ package com.jjt.utils;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
 
 /**
  * JavaScript工具类
@@ -13,56 +12,53 @@ import javax.script.ScriptException;
  */
 public class JavaScriptUtil {
     public static void main(String[] args) {
-        System.out.println("JavaScriptUtil.main");
     }
 
-    public static String exec() {
-        try {
-            // 创建脚本引擎管理器
-            ScriptEngineManager manager = new ScriptEngineManager();
+    /**
+     * 执行JavaScript代码并返回结果
+     *
+     * @param jsCode     JavaScript代码
+     * @param dataArrays 数据数组
+     * @return 执行结果
+     * @throws Exception 执行异常
+     */
+    public static String exec(String jsCode, Double[]... dataArrays) throws Exception {
+        // 创建脚本引擎管理器
+        ScriptEngineManager manager = new ScriptEngineManager();
+        ScriptEngine engine = manager.getEngineByName("graal.js");
 
-            // 获取GraalJS引擎
-            ScriptEngine engine = manager.getEngineByName("graal.js");
+        // 替换函数名
+        jsCode = jsCode.replace("function", "let testFun=function");
 
-            // 定义JavaScript函数
-            String jsFunctions =
-                    "function add(a, b) { " +
-                            "    return a + b; " +
-                            "} " +
-                            "" +
-                            "function factorial(n) { " +
-                            "    if (n <= 1) return 1; " +
-                            "    return n * factorial(n - 1); " +
-                            "} " +
-                            "" +
-                            "function getUserInfo(name, age) { " +
-                            "    return { " +
-                            "        name: name, " +
-                            "        age: age, " +
-                            "        birthYear: 2024 - age " +
-                            "    }; " +
-                            "}";
+        // 转换所有数组为JavaScript数组
+        Object[] jsArrays = new Object[dataArrays.length];
+        for (int i = 0; i < dataArrays.length; i++) {
+            StringBuilder arrayBuilder = new StringBuilder();
+            arrayBuilder.append("[");
+            for (int j = 0; j < dataArrays[i].length; j++) {
+                if (j > 0) {
+                    arrayBuilder.append(",");
+                }
+                arrayBuilder.append(dataArrays[i][j]);
+            }
+            arrayBuilder.append("]");
+            String jsArrayStr = arrayBuilder.toString();
 
-            // 执行脚本,注册函数
-            engine.eval(jsFunctions);
+            // 设置变量并保存引用
+            String varName = "javaData" + (i + 1);
+            engine.eval("var " + varName + " = " + jsArrayStr + ";");
+            jsArrays[i] = engine.get(varName);
+        }
 
-            // 转换为Invocable来调用函数
-            Invocable invocable = (Invocable) engine;
+        // 执行脚本,注册函数
+        engine.eval(jsCode);
 
-            // 调用add函数
-            Object addResult = invocable.invokeFunction("add", 15, 25);
-            System.out.println("15 + 25 = " + addResult);
+        // 转换为Invocable来调用函数
+        Invocable invocable = (Invocable) engine;
 
-            // 调用factorial函数
-            Object factorialResult = invocable.invokeFunction("factorial", 5);
-            System.out.println("5! = " + factorialResult);
+        // 调用函数,传递所有数组
+        Object result = invocable.invokeFunction("testFun", jsArrays);
 
-            // 调用返回对象的函数
-            Object userInfo = invocable.invokeFunction("getUserInfo", "张三", 25);
-            System.out.println("用户信息: " + userInfo);
-        } catch (ScriptException | NoSuchMethodException e) {
-            e.printStackTrace();
-        }
-        return "JavaScriptUtil.exec";
+        return result.toString();
     }
 }