From af8cab142a6b15c06e131a8474574dd5b00df982 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期四, 04 十二月 2025 22:09:58 +0800
Subject: [PATCH] feat: 改造微信accesstoken存放在系统配置表中

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

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/WechatUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/WechatUtils.java
index 53ff3a3..9a34103 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/WechatUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/WechatUtils.java
@@ -200,4 +200,87 @@
         }
         return userAgent.toLowerCase().contains("micromessenger");
     }
+
+    /**
+     * 鎴柇thing绫诲瀷瀛楁锛屽井淇¤闃呮秷鎭痶hing绫诲瀷鏈�闀�20涓瓧绗�
+     * 
+     * @param value 鍘熷瀛楃涓�
+     * @return 鎴柇鍚庣殑瀛楃涓诧紙鏈�闀�20瀛楃锛�
+     */
+    public static String truncateThingValue(String value) {
+        if (StringUtils.isEmpty(value)) {
+            return value;
+        }
+        
+        // 寰俊thing绫诲瀷鏈�闀�20涓瓧绗�
+        final int MAX_THING_LENGTH = 20;
+        
+        if (value.length() <= MAX_THING_LENGTH) {
+            return value;
+        }
+        
+        // 鎴柇骞舵坊鍔犵渷鐣ュ彿锛岀‘淇濇�婚暱搴︿笉瓒呰繃20
+        return value.substring(0, MAX_THING_LENGTH - 1) + "鈥�";
+    }
+
+    /**
+     * 鍙戦�佸皬绋嬪簭璁㈤槄娑堟伅
+     *
+     * @param accessToken 寰俊鎺ュ彛璋冪敤鍑瘉
+     * @param touser 鎺ユ敹浜簅penId
+     * @param templateId 妯℃澘ID
+     * @param page 灏忕▼搴忚烦杞〉闈�
+     * @param data 妯℃澘鏁版嵁锛宬ey涓哄瓧娈靛悕(濡倀hing1銆乼ime27)锛寁alue涓哄瓧娈靛��
+     * @return 寰俊杩斿洖缁撴灉JSON
+     */
+    public static JSONObject sendSubscribeMessage(String accessToken,
+                                                  String touser,
+                                                  String templateId,
+                                                  String page,
+                                                  Map<String, String> data) {
+        try {
+            if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(touser) || StringUtils.isEmpty(templateId)) {
+                log.error("鍙戦�佽闃呮秷鎭弬鏁颁笉瀹屾暣锛宎ccessToken={}, touser={}, templateId={}", accessToken, touser, templateId);
+                return null;
+            }
+
+            String url = WECHAT_API_BASE_URL_SERVER + "/cgi-bin/message/subscribe/send?access_token=" + accessToken;
+
+            Map<String, Object> body = new HashMap<>();
+            body.put("touser", touser);
+            body.put("template_id", templateId);
+            if (StringUtils.isNotEmpty(page)) {
+                body.put("page", page);
+            }
+             body.put("miniprogram_state", "formal");
+
+            Map<String, Object> dataNode = new HashMap<>();
+            if (data != null && !data.isEmpty()) {
+                for (Map.Entry<String, String> entry : data.entrySet()) {
+                    Map<String, String> valueNode = new HashMap<>();
+                    valueNode.put("value", entry.getValue());
+                    dataNode.put(entry.getKey(), valueNode);
+                }
+            }
+            body.put("data", dataNode);
+
+            String jsonBody = JSON.toJSONString(body);
+            String response = HttpUtils.sendPost(url, jsonBody);
+
+            JSONObject jsonObject = JSON.parseObject(response);
+            if (jsonObject == null) {
+                log.error("鍙戦�佽闃呮秷鎭繑鍥炰负绌�");
+                return null;
+            }
+            if (jsonObject.getIntValue("errcode") != 0) {
+                log.error("鍙戦�佽闃呮秷鎭け璐�: {}", jsonObject.toJSONString());
+            } else {
+                log.info("鍙戦�佽闃呮秷鎭垚鍔燂紝touser={}, templateId={}", touser, templateId);
+            }
+            return jsonObject;
+        } catch (Exception e) {
+            log.error("鍙戦�佽闃呮秷鎭紓甯�: {}", e.getMessage(), e);
+            return null;
+        }
+    }
 }

--
Gitblit v1.9.1