|
@@ -0,0 +1,66 @@
|
|
|
+package vip.xiaonuo.coldchain.modular.push.utils;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpUserService;
|
|
|
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
|
|
+import me.chanjar.weixin.mp.api.impl.WxMpUserServiceImpl;
|
|
|
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
|
|
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
|
|
+import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
|
|
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
|
|
+import vip.xiaonuo.coldchain.modular.push.config.PushConfigure;
|
|
|
+import vip.xiaonuo.coldchain.modular.push.param.PushParam;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 推送类
|
|
|
+ */
|
|
|
+public class PushUtil {
|
|
|
+ private static WxMpConfigStorage wxMpConfigStorage() {
|
|
|
+ WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
|
|
+ config.setAppId(PushConfigure.getAppId());
|
|
|
+ config.setSecret(PushConfigure.getSecret());
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 消息推送主要业务代码
|
|
|
+ */
|
|
|
+ public static String push(PushParam pushParam) {
|
|
|
+ //1,配置
|
|
|
+ WxMpService wxMpService = new WxMpServiceImpl();
|
|
|
+ wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
|
|
|
+
|
|
|
+ WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram();
|
|
|
+ String miniProgramAppId = PushConfigure.getMiniProgram();
|
|
|
+ miniProgram.setAppid(miniProgramAppId);//小程序appid
|
|
|
+ miniProgram.setUsePath(true);
|
|
|
+ miniProgram.setPagePath(PushConfigure.getPagePath());//用户点击时需要跳转的小程序页面
|
|
|
+ // 推送消息
|
|
|
+ WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
|
|
+ .toUser(pushParam.getUserId())
|
|
|
+ .templateId(PushConfigure.getTemplateId())
|
|
|
+ .miniProgram(miniProgram)
|
|
|
+ .build();
|
|
|
+ // 配置你的信息
|
|
|
+ String dateFormat = DateUtil.format(pushParam.getNoticeTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ templateMessage.addData(new WxMpTemplateData("thing23", pushParam.getDeviceName()));
|
|
|
+ templateMessage.addData(new WxMpTemplateData("thing5", pushParam.getUserName()));
|
|
|
+ templateMessage.addData(new WxMpTemplateData("character_string28", pushParam.getValue()));
|
|
|
+ templateMessage.addData(new WxMpTemplateData("thing25", pushParam.getContext()));
|
|
|
+ templateMessage.addData(new WxMpTemplateData("time3", dateFormat));
|
|
|
+
|
|
|
+ System.out.println(templateMessage.toJson());
|
|
|
+ try {
|
|
|
+ wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("推送失败:" + e.getMessage());
|
|
|
+ return "推送失败:" + e.getMessage();
|
|
|
+ }
|
|
|
+ return "推送成功!";
|
|
|
+ }
|
|
|
+}
|