From cc3fe1ad9b366533d184d9e239b45f731f014e74 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期四, 23 四月 2026 11:22:38 +0800
Subject: [PATCH] Merge branch 'master' into master-手术随访

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

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/WxMpUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/WxMpUtils.java
new file mode 100644
index 0000000..89cc4ef
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/WxMpUtils.java
@@ -0,0 +1,175 @@
+package com.ruoyi.common.utils;
+
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.exception.WxErrorException;
+import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
+
+import java.util.Map;
+
+/**
+ * 寰俊鍏紬鍙锋秷鎭彂閫佸伐鍏风被
+ * 鏀寔锛氭枃鏈鏈嶆秷鎭�佹ā鏉挎秷鎭�
+ */
+@Slf4j
+public class WxMpUtils {
+
+    private final WxMpService wxMpService;
+
+    /**
+     * 鏋勯�犳柟娉曪紝閫氳繃 appId 鍜� appSecret 鍒濆鍖栧井淇″叕浼楀彿鏈嶅姟
+     *
+     * @param appId     寰俊鍏紬鍙� AppID
+     * @param appSecret 寰俊鍏紬鍙� AppSecret
+     */
+    public WxMpUtils(String appId, String appSecret) {
+        WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
+        configStorage.setAppId(appId);
+        configStorage.setSecret(appSecret);
+        wxMpService = new WxMpServiceImpl();
+        wxMpService.setWxMpConfigStorage(configStorage);
+    }
+
+    // -------------------------------------------------------------------------
+    // 鏂囨湰瀹㈡湇娑堟伅
+    // -------------------------------------------------------------------------
+
+    /**
+     * 鍚戞寚瀹� openid 鐨勭敤鎴峰彂閫佹枃鏈鏈嶆秷鎭�
+     * 娉ㄦ剰锛氱敤鎴烽渶鍦�48灏忔椂鍐呬笌鍏紬鍙锋湁杩囦氦浜掓墠鍙彂閫�
+     *
+     * @param openid  鐢ㄦ埛鐨勫井淇� openid
+     * @param content 娑堟伅鍐呭
+     * @return 鍙戦�佹垚鍔熻繑鍥� true锛屽け璐ヨ繑鍥� false
+     */
+    public boolean sendTextMessage(String openid, String content) {
+        if (StringUtils.isEmpty(openid)) {
+            log.warn("銆怶xMpUtils銆憇endTextMessage 澶辫触锛歰penid 涓虹┖");
+            return false;
+        }
+        if (StringUtils.isEmpty(content)) {
+            log.warn("銆怶xMpUtils銆憇endTextMessage 澶辫触锛歝ontent 涓虹┖");
+            return false;
+        }
+        try {
+            WxMpKefuMessage message = WxMpKefuMessage.TEXT()
+                    .toUser(openid)
+                    .content(content)
+                    .build();
+            boolean result = wxMpService.getKefuService().sendKefuMessage(message);
+            log.info("銆怶xMpUtils銆戞枃鏈鏈嶆秷鎭彂閫亄}锛宱penid锛歿}", result ? "鎴愬姛" : "澶辫触", openid);
+            return result;
+        } catch (WxErrorException e) {
+            log.error("銆怶xMpUtils銆戞枃鏈鏈嶆秷鎭彂閫佸紓甯革紝openid锛歿}锛岄敊璇細{}", openid, e.getError(), e);
+            return false;
+        }
+    }
+
+    // -------------------------------------------------------------------------
+    // 妯℃澘娑堟伅
+    // -------------------------------------------------------------------------
+
+    /**
+     * 鍚戞寚瀹� openid 鐨勭敤鎴峰彂閫佹ā鏉挎秷鎭紙鍚烦杞琔RL锛�
+     *
+     * @param openid     鐢ㄦ埛鐨勫井淇� openid
+     * @param templateId 妯℃澘娑堟伅 ID锛堝湪寰俊鍏紬骞冲彴閰嶇疆锛�
+     * @param url        鐐瑰嚮娑堟伅鍚庣殑璺宠浆 URL锛屼笉闇�瑕佽烦杞彲浼� null
+     * @param dataMap    妯℃澘鏁版嵁锛宬ey 涓烘ā鏉垮瓧娈靛悕锛寁alue 涓� WxMpTemplateData锛堝惈鍊煎拰棰滆壊锛�
+     *                   绀轰緥锛歿"first": new WxMpTemplateData("first", "浣犲ソ", "#173177"), ...}
+     * @return 鍙戦�佹垚鍔熻繑鍥炲井淇¤繑鍥炵殑 msgid锛屽け璐ヨ繑鍥� null
+     */
+    public String sendTemplateMessage(String openid, String templateId, String url,
+                                      Map<String, WxMpTemplateData> dataMap) {
+        if (StringUtils.isEmpty(openid)) {
+            log.warn("銆怶xMpUtils銆憇endTemplateMessage 澶辫触锛歰penid 涓虹┖");
+            return null;
+        }
+        if (StringUtils.isEmpty(templateId)) {
+            log.warn("銆怶xMpUtils銆憇endTemplateMessage 澶辫触锛歵emplateId 涓虹┖");
+            return null;
+        }
+        try {
+            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
+            templateMessage.setToUser(openid);
+            templateMessage.setTemplateId(templateId);
+            if (StringUtils.isNotEmpty(url)) {
+                templateMessage.setUrl(url);
+            }
+            if (dataMap != null) {
+                for (WxMpTemplateData data : dataMap.values()) {
+                    templateMessage.addData(data);
+                }
+            }
+            String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
+            log.info("銆怶xMpUtils銆戞ā鏉挎秷鎭彂閫佹垚鍔燂紝openid锛歿}锛宮sgId锛歿}", openid, msgId);
+            return msgId;
+        } catch (WxErrorException e) {
+            log.error("銆怶xMpUtils銆戞ā鏉挎秷鎭彂閫佸紓甯革紝openid锛歿}锛宼emplateId锛歿}锛岄敊璇細{}",
+                    openid, templateId, e.getError(), e);
+            return null;
+        }
+    }
+
+    /**
+     * 鍚戞寚瀹� openid 鐨勭敤鎴峰彂閫佹ā鏉挎秷鎭紙绠�鍖栫増锛寁alue 涓嶈缃鑹诧級
+     *
+     * @param openid     鐢ㄦ埛鐨勫井淇� openid
+     * @param templateId 妯℃澘娑堟伅 ID
+     * @param url        鐐瑰嚮娑堟伅鍚庣殑璺宠浆 URL锛屼笉闇�瑕佽烦杞彲浼� null
+     * @param dataMap    妯℃澘鏁版嵁锛宬ey 涓烘ā鏉垮瓧娈靛悕锛寁alue 涓哄瓧娈垫樉绀虹殑鏂囨湰鍐呭
+     *                   绀轰緥锛歿"first": "浣犲ソ", "keyword1": "2024-01-01", "remark": "鎰熻阿浣跨敤"}
+     * @return 鍙戦�佹垚鍔熻繑鍥炲井淇¤繑鍥炵殑 msgid锛屽け璐ヨ繑鍥� null
+     */
+    public String sendTemplateMessage(String openid, String templateId, String url,
+                                      Map<String, String> dataMap, String defaultColor) {
+        if (StringUtils.isEmpty(openid)) {
+            log.warn("銆怶xMpUtils銆憇endTemplateMessage 澶辫触锛歰penid 涓虹┖");
+            return null;
+        }
+        if (StringUtils.isEmpty(templateId)) {
+            log.warn("銆怶xMpUtils銆憇endTemplateMessage 澶辫触锛歵emplateId 涓虹┖");
+            return null;
+        }
+        try {
+            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
+            templateMessage.setToUser(openid);
+            templateMessage.setTemplateId(templateId);
+            if (StringUtils.isNotEmpty(url)) {
+                templateMessage.setUrl(url);
+            }
+            if (dataMap != null) {
+                String color = StringUtils.isEmpty(defaultColor) ? "#173177" : defaultColor;
+                for (Map.Entry<String, String> entry : dataMap.entrySet()) {
+                    templateMessage.addData(new WxMpTemplateData(entry.getKey(), entry.getValue(), color));
+                }
+            }
+            String msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
+            log.info("銆怶xMpUtils銆戞ā鏉挎秷鎭彂閫佹垚鍔燂紝openid锛歿}锛宮sgId锛歿}", openid, msgId);
+            return msgId;
+        } catch (WxErrorException e) {
+            log.error("銆怶xMpUtils銆戞ā鏉挎秷鎭彂閫佸紓甯革紝openid锛歿}锛宼emplateId锛歿}锛岄敊璇細{}",
+                    openid, templateId, e.getError(), e);
+            return null;
+        }
+    }
+
+    // -------------------------------------------------------------------------
+    // 闈欐�佸伐鍘傛柟娉曪紙蹇�熸瀯閫狅級
+    // -------------------------------------------------------------------------
+
+    /**
+     * 蹇�熷垱寤� WxMpUtils 瀹炰緥
+     *
+     * @param appId     寰俊鍏紬鍙� AppID
+     * @param appSecret 寰俊鍏紬鍙� AppSecret
+     * @return WxMpUtils 瀹炰緥
+     */
+    public static WxMpUtils of(String appId, String appSecret) {
+        return new WxMpUtils(appId, appSecret);
+    }
+}

--
Gitblit v1.9.3