|
@@ -0,0 +1,64 @@
|
|
|
+package com.platform.yijia.utils.weixinapp;
|
|
|
+
|
|
|
+
|
|
|
+import me.chanjar.weixin.common.error.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.template.WxMpTemplateData;
|
|
|
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/***
|
|
|
+ * <Title> WxPushUtil </Title>
|
|
|
+ * <Description> 微信公众号消息推送 </Description>
|
|
|
+ * @author JK
|
|
|
+ * @date 2021年2月19日
|
|
|
+ */
|
|
|
+public class WxPushUtil {
|
|
|
+
|
|
|
+ private static Logger logger =(Logger) LoggerFactory.getLogger(WxPushUtil.class);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 微信公众号推送
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @param templateId
|
|
|
+ * @param openId
|
|
|
+ * @param wxMpTemplateDataList
|
|
|
+ */
|
|
|
+ public void push(String appId, String appSecret, String templateId, String openId, List<WxMpTemplateData> wxMpTemplateDataList){
|
|
|
+ logger.info("公众号消息推送参数:"+ "appId:"+ appId + ";App秘钥:"+appSecret+ ";模板ID:"+templateId + ";openId:"+openId + ";模板数据" +wxMpTemplateDataList);
|
|
|
+ //创建对象
|
|
|
+ WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
|
|
|
+ wxMpInMemoryConfigStorage.setAppId(appId);
|
|
|
+ wxMpInMemoryConfigStorage.setSecret(appSecret);
|
|
|
+ Token token = WeiXinUserUtil.getToken(appId, appSecret);
|
|
|
+ logger.info("token信息: " + token.getAccessToken());
|
|
|
+ wxMpInMemoryConfigStorage.setAccessToken(token.getAccessToken());
|
|
|
+
|
|
|
+ WxMpService wxMpService = new WxMpServiceImpl();
|
|
|
+ wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
|
|
|
+ WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
|
|
|
+ .toUser(openId)
|
|
|
+ .templateId(templateId)
|
|
|
+ //.url()
|
|
|
+ .build();
|
|
|
+ //wxMpTemplateMessage.addData(wxMpTemplateData)
|
|
|
+ wxMpTemplateMessage.setData(wxMpTemplateDataList);
|
|
|
+ try {
|
|
|
+// wxMpTemplateMessage.setToUser(openId);
|
|
|
+ wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
|
|
|
+ logger.info("推送成功");
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ logger.error("推送失败" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|