From 80d51272bfcc9a832ca5fe5110cc543c98fffc23 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期日, 20 九月 2026 18:13:16 +0800
Subject: [PATCH] 【景宁】定时发送异常信息-景宁配置

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/TemplateUtils.java |  104 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 104 insertions(+), 0 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/TemplateUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TemplateUtils.java
new file mode 100644
index 0000000..04d0eef
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/TemplateUtils.java
@@ -0,0 +1,104 @@
+package com.ruoyi.common.utils;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import java.beans.IntrospectionException;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class TemplateUtils {
+    private static final Pattern PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");
+    private static final ObjectMapper OM = new ObjectMapper();
+
+    public static String render(String template, Object bean) {
+        if (template == null || bean == null) {
+            return template;
+        }
+        Matcher matcher = PATTERN.matcher(template);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String exp = matcher.group(1);
+            Object val = getValueByExp(bean, exp);
+            String strVal = val == null ? "" : val.toString();
+            matcher.appendReplacement(sb, Matcher.quoteReplacement(strVal));
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+    private static Object getValueByExp(Object root, String exp) {
+        try {
+            Object curr = root;
+            if (exp.contains("#json.")) {
+                String[] parts = exp.split("#json\\.", 2);
+                String beanField = parts[0];
+                String jsonPath = parts[1];
+
+                Object jsonStrObj = getBeanField(curr, beanField);
+                // ========== Java8 鍏煎淇敼鐐� ==========
+                if (!(jsonStrObj instanceof String)) {
+                    return null;
+                }
+                String jsonStr = (String) jsonStrObj;
+                if (jsonStr.trim().isEmpty()) {
+                    return null;
+                }
+                JsonNode node = OM.readTree(jsonStr);
+                return getJsonNode(node, jsonPath);
+            }
+
+            String[] props = exp.split("\\.");
+            for (String p : props) {
+                curr = getBeanField(curr, p);
+                if (curr == null) {
+                    break;
+                }
+            }
+            return curr;
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    private static Object getBeanField(Object bean, String fieldName)
+            throws IntrospectionException, InvocationTargetException, IllegalAccessException {
+        if (bean == null) {
+            return null;
+        }
+        PropertyDescriptor pd = new PropertyDescriptor(fieldName, bean.getClass());
+        Method getter = pd.getReadMethod();
+        return getter.invoke(bean);
+    }
+
+    private static Object getJsonNode(JsonNode node, String path) {
+        String[] arr = path.split("\\.");
+        JsonNode curr = node;
+        for (String k : arr) {
+            curr = curr.get(k);
+            if (curr == null) {
+                return null;
+            }
+        }
+        return curr.isValueNode() ? curr.asText() : curr.toString();
+    }
+
+    // 娴嬭瘯
+    public static void main(String[] args) {
+        class User {
+            private String name = "寮犱笁";
+            private Integer age = 25;
+            private String ext = "{\"phone\":\"13800138000\",\"addr\":{\"city\":\"鏉窞\"}}";
+
+            public String getName() { return name; }
+            public Integer getAge() { return age; }
+            public String getExt() { return ext; }
+        }
+        User u = new User();
+        String tpl = "濮撳悕:${name},骞撮緞:${age},鎵嬫満鍙�:${ext#json.phone},鍩庡競:${ext#json.addr.city}";
+        System.out.println(render(tpl, u));
+    }
+}

--
Gitblit v1.9.3