| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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("ãWxMpUtilsãsendTextMessage 失败ï¼openid 为空"); |
| | | return false; |
| | | } |
| | | if (StringUtils.isEmpty(content)) { |
| | | log.warn("ãWxMpUtilsãsendTextMessage 失败ï¼content 为空"); |
| | | return false; |
| | | } |
| | | try { |
| | | WxMpKefuMessage message = WxMpKefuMessage.TEXT() |
| | | .toUser(openid) |
| | | .content(content) |
| | | .build(); |
| | | boolean result = wxMpService.getKefuService().sendKefuMessage(message); |
| | | log.info("ãWxMpUtilsãææ¬å®¢ææ¶æ¯åé{}ï¼openidï¼{}", result ? "æå" : "失败", openid); |
| | | return result; |
| | | } catch (WxErrorException e) { |
| | | log.error("ãWxMpUtilsãææ¬å®¢ææ¶æ¯åéå¼å¸¸ï¼openidï¼{}ï¼é误ï¼{}", openid, e.getError(), e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | // ------------------------------------------------------------------------- |
| | | // æ¨¡æ¿æ¶æ¯ |
| | | // ------------------------------------------------------------------------- |
| | | |
| | | /** |
| | | * åæå® openid çç¨æ·å鿍¡æ¿æ¶æ¯ï¼å«è·³è½¬URLï¼ |
| | | * |
| | | * @param openid ç¨æ·ç微信 openid |
| | | * @param templateId æ¨¡æ¿æ¶æ¯ IDï¼å¨å¾®ä¿¡å
¬ä¼å¹³å°é
ç½®ï¼ |
| | | * @param url ç¹å»æ¶æ¯åç跳转 URLï¼ä¸éè¦è·³è½¬å¯ä¼ null |
| | | * @param dataMap æ¨¡æ¿æ°æ®ï¼key 为模æ¿å段åï¼value 为 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("ãWxMpUtilsãsendTemplateMessage 失败ï¼openid 为空"); |
| | | return null; |
| | | } |
| | | if (StringUtils.isEmpty(templateId)) { |
| | | log.warn("ãWxMpUtilsãsendTemplateMessage 失败ï¼templateId 为空"); |
| | | 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("ãWxMpUtilsãæ¨¡æ¿æ¶æ¯åéæåï¼openidï¼{}ï¼msgIdï¼{}", openid, msgId); |
| | | return msgId; |
| | | } catch (WxErrorException e) { |
| | | log.error("ãWxMpUtilsãæ¨¡æ¿æ¶æ¯åéå¼å¸¸ï¼openidï¼{}ï¼templateIdï¼{}ï¼é误ï¼{}", |
| | | openid, templateId, e.getError(), e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åæå® openid çç¨æ·å鿍¡æ¿æ¶æ¯ï¼ç®åçï¼value ä¸è®¾ç½®é¢è²ï¼ |
| | | * |
| | | * @param openid ç¨æ·ç微信 openid |
| | | * @param templateId æ¨¡æ¿æ¶æ¯ ID |
| | | * @param url ç¹å»æ¶æ¯åç跳转 URLï¼ä¸éè¦è·³è½¬å¯ä¼ null |
| | | * @param dataMap æ¨¡æ¿æ°æ®ï¼key 为模æ¿å段åï¼value ä¸ºåæ®µæ¾ç¤ºçææ¬å
容 |
| | | * 示ä¾ï¼{"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("ãWxMpUtilsãsendTemplateMessage 失败ï¼openid 为空"); |
| | | return null; |
| | | } |
| | | if (StringUtils.isEmpty(templateId)) { |
| | | log.warn("ãWxMpUtilsãsendTemplateMessage 失败ï¼templateId 为空"); |
| | | 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("ãWxMpUtilsãæ¨¡æ¿æ¶æ¯åéæåï¼openidï¼{}ï¼msgIdï¼{}", openid, msgId); |
| | | return msgId; |
| | | } catch (WxErrorException e) { |
| | | log.error("ãWxMpUtilsãæ¨¡æ¿æ¶æ¯åéå¼å¸¸ï¼openidï¼{}ï¼templateIdï¼{}ï¼é误ï¼{}", |
| | | 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); |
| | | } |
| | | } |