From 7c790c248c137a2fa5525bf66ed04c25043cded7 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期三, 03 十二月 2025 09:32:19 +0800
Subject: [PATCH] feat:用户附加同步
---
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