Przeglądaj źródła

修复复杂表达式时错误问题,顺便优化表达式解析

zhouhao 4 lat temu
rodzic
commit
d76996ef7c

+ 25 - 8
hsweb-core/src/main/java/org/hswebframework/web/utils/ExpressionUtils.java

@@ -1,10 +1,13 @@
 package org.hswebframework.web.utils;
 
 import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.beanutils.BeanUtilsBean2;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.hswebframework.expands.script.engine.DynamicScriptEngine;
 import org.hswebframework.expands.script.engine.DynamicScriptEngineFactory;
 import org.hswebframework.expands.script.engine.ExecuteResult;
+import org.springframework.util.StringUtils;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -17,6 +20,7 @@ import java.util.regex.Pattern;
  * @author zhouhao
  * @since 3.0
  */
+@Slf4j
 public class ExpressionUtils {
 
     //表达式提取正则 ${.+?}
@@ -75,7 +79,7 @@ public class ExpressionUtils {
      */
     @SneakyThrows
     public static String analytical(String expression, Map<String, Object> vars, String language) {
-        if(!expression.contains("${")){
+        if (!expression.contains("${")) {
             return expression;
         }
         DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(language);
@@ -84,24 +88,37 @@ public class ExpressionUtils {
         }
 
         return TemplateParser.parse(expression, var -> {
-            Object fast = vars.get(var);
-            if (fast != null) {
-                return fast.toString();
+            if (StringUtils.isEmpty(var)) {
+                return "";
             }
-            String id = DigestUtils.md5Hex(var);
 
+            if (!var.startsWith("#")) {
+                try {
+                    Object fast = BeanUtilsBean2.getInstance().getProperty(vars, var);
+                    if (fast != null) {
+                        return fast.toString();
+                    }
+                } catch (Exception ignore) {
+                    //ignore
+                }
+            }
+            String id = DigestUtils.md5Hex(var);
             try {
                 if (!engine.compiled(id)) {
-
                     engine.compile(id, var);
-
                 }
-                return String.valueOf(engine.execute(id, vars).getIfSuccess());
             } catch (RuntimeException e) {
                 throw e;
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
+            try {
+                return String.valueOf(engine.execute(id, vars).getIfSuccess());
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                return "";
+            }
+
         });
     }
 

+ 25 - 0
hsweb-core/src/test/java/org/hswebframework/web/utils/ExpressionUtilsTest.java

@@ -1,5 +1,6 @@
 package org.hswebframework.web.utils;
 
+import com.alibaba.fastjson.JSON;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -38,4 +39,28 @@ public class ExpressionUtilsTest {
         }));
 
     }
+
+    @Test
+    public void testJson2(){
+        String js = ExpressionUtils.analytical("{\n" +
+                "     \"msgtype\": \"markdown\",\n" +
+                "     \"markdown\": {\n" +
+                "         \"title\":\"消息类型:${messageType}\",\n" +
+                "         \"text\": \" - 设备ID: `${deviceId}` \\n - 设备型号: `${headers.productId}`\\n - 设备名称: `${headers.deviceName}`\"" +
+                "     },\n" +
+                "      \"at\": {\n" +
+                "          \"isAtAll\": false\n" +
+                "      }\n" +
+                "}", JSON.parseObject("{\n" +
+                "  \"deviceId\": \"VIS-Mandrake-12289\",\n" +
+                "  \"headers\": {\n" +
+                "    \"productId\": \"VIS-Mandrake\",\n" +
+                "    \"deviceName\": \"能见度仪-曼德克-01\"\n" +
+                "  },\n" +
+                "  \"messageType\": \"OFFLINE\",\n" +
+                "  \"timestamp\": 1592098397277\n" +
+                "}"), "spel");
+
+        System.out.println(js);
+    }
 }