利用spring 事件发送模板消息

1.定义事件

 import com.ruoyi.project.salerauth.domain.TemplateMessage;
import org.springframework.context.ApplicationEvent; public class TemplateMessageEvent extends ApplicationEvent { public TemplateMessageEvent(TemplateMessage templateMessage) {
super(templateMessage);
} }

2.定义事件监听

import com.ruoyi.framework.config.WpFactoryConfig;
import com.ruoyi.framework.templateMessageEvent.event.TemplateMessageEvent;
import com.ruoyi.framework.web.domain.WMTemplateData;
import com.ruoyi.framework.web.domain.WMTemplateMessage;
import com.ruoyi.framework.web.service.TemplateMessageService;
import com.ruoyi.project.salerauth.domain.TemplateMessage;
import com.ruoyi.project.salerauth.mapper.ShopSalerAuthMapper;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import java.util.Map;
import java.util.TreeMap; @Component
public class TemplateMessageListener implements ApplicationListener<TemplateMessageEvent> { @Autowired
private TemplateMessageService templateMessageService; @Autowired
private WpFactoryConfig wpFactoryConfig; @Autowired
private ShopSalerAuthMapper shopSalerAuthMapper; @Override
public void onApplicationEvent(TemplateMessageEvent templateMessageEvent) { Object source = templateMessageEvent.getSource(); if (null != source && source instanceof TemplateMessage) {
TemplateMessage templateMessage = (TemplateMessage) source; //获得认证工厂的名称
String companyName = templateMessage.getShortName(); //获得提交认证时的form_id
String formId = shopSalerAuthMapper.querySalerAuthById(templateMessage.getAuthId()).getFormId(); //获得openId
String openId = templateMessage.getOpenId(); String templateId = "";
String page = ""; Map<String, WMTemplateData> data = new TreeMap<>();
//认证审核通过
if (templateMessage.getAuthStatus().equals("1")) { //获得申请人的姓名
String authorApplyName = templateMessage.getMemName(); data.put("keyword1", new WMTemplateData(companyName));
data.put("keyword2", new WMTemplateData(templateMessage.getMessage()));
data.put("keyword3", new WMTemplateData(authorApplyName));
data.put("keyword4", new WMTemplateData(""));
page = wpFactoryConfig.getPage() + "?authStatus='1'";
templateId = wpFactoryConfig.getOtemplateId(); //认证审核失败
} else { data.put("keyword1", new WMTemplateData(companyName));
data.put("keyword2", new WMTemplateData(templateMessage.getMessage()));
data.put("keyword3", new WMTemplateData(templateMessage.getRefuseReason()));
page = wpFactoryConfig.getPage() + "?authStatus='2'";
templateId = wpFactoryConfig.getFtemplateId(); } try {
sendTemplateMessage(openId, page, templateId, formId, data);
} catch (WxErrorException e) {
e.printStackTrace();
} } } /**
* 发送微信小程序模板消息
*
* @param touser
* @param page
* @param form_id
* @param data
*/
public void sendTemplateMessage(String touser, String page, String templateId, String form_id, Map<String, WMTemplateData> data) throws WxErrorException { WMTemplateMessage wmTemplateMessage = new WMTemplateMessage();
wmTemplateMessage.setTouser(touser);
wmTemplateMessage.setTemplate_id(templateId);
wmTemplateMessage.setPage(page);
wmTemplateMessage.setForm_id(form_id);
wmTemplateMessage.setData(data);
wmTemplateMessage.setEmphasis_keyword("keyword1.DATA");
templateMessageService.send(wmTemplateMessage); } }
import com.ruoyi.project.salerauth.domain.TemplateMessage;
import org.springframework.context.ApplicationEvent; public class TemplateMessageEvent extends ApplicationEvent { public TemplateMessageEvent(TemplateMessage templateMessage) {
super(templateMessage);
} }

  

3.定义小程序模板消息的请求体模型

 import lombok.Data;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap; /**
*
* 小程序模板消息实体
*
*/
@Data
public class WMTemplateMessage implements Serializable { /**
* 微信用户的openId
*/
private String touser; private String template_id; private String page; private String form_id; private Map<String,WMTemplateData> data=new TreeMap<>(); /**
* 加大字段信息
*/
private String emphasis_keyword; public String toJson() {
return WxMpGsonBuilder.INSTANCE.create().toJson(this);
}
}

4.调用发送执行的业务类

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
import com.ruoyi.framework.web.domain.WMTemplateMessage;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.io.StringReader; @Service
public class TemplateMessageService { @Autowired
private WxMpService wxMpService; /**
* 发送模板消息
*
* @param wmTemplateMessage
* @return
* @throws WxErrorException
*/
public String send(WMTemplateMessage wmTemplateMessage) throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
String responseContent =wxMpService.execute(new SimplePostRequestExecutor(), url, wmTemplateMessage.toJson());
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); if (jsonObject.get("errcode").getAsInt() == 0)
return jsonObject.get("msgid").getAsString();
throw new WxErrorException(WxError.fromJson(responseContent)); } }
import java.io.Serializable;
import java.util.Map; /**
* 微信小程序模板消息
*
*/
public class WMTemplateData implements Serializable { /**
* 模板字段对应的值
*/
private String value; public WMTemplateData(String value) {
this.value = value;
} public WMTemplateData() {
}
}

5.配置具体的发送执行者的Bean

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; /**
* @author wanghong
* @version 2.0.0
* @title
* @date 2018/11/26 9:48
*/
@Configuration
@Component
public class WxMpServiceConfig { @Autowired
private WPConfig wechatConfig; @Bean
public WxMpService getWxMpService(){ WxMpService wxMpService = new WxMpServiceImpl(); WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(wechatConfig.getAppId());
wxMpConfigStorage.setSecret(wechatConfig.getSecret());
wxMpService.setWxMpConfigStorage(wxMpConfigStorage); return wxMpService; } }

6.读取配置文件的实体

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Data
@Component
public class WpFactoryConfig { @Value("wpFactory.ftemplateId")
private String ftemplateId; @Value("wpFactory.otemplateId")
private String otemplateId; @Value("wpFactory.page")
private String page; }

7.使用

 @Autowired
private TemplateMessagePublish templateMessagePublish;
...... //审核失败
if (salerAuth.getAuthStatus().equals("2")) { TemplateMessage templateMessage = new TemplateMessage(salerAuth.getAuthStatus());
templateMessage.setAuthStatus(salerAuth.getAuthStatus());
templateMessage.setRefuseReason(salerAuth.getRefuseReason());
templateMessage.setShortName(salerAuth.getShortName());
templateMessage.setMemName(salerAuth.getMemName());
templateMessage.setShopSalerId(salerAuth.getShopSalerId());
templateMessage.setAuthId(salerAuth.getId());
templateMessagePublish.publish(new TemplateMessageEvent(templateMessage)); } //审核成功
if (salerAuth.getAuthStatus().equals("1")) {
salerAuth.setShopSalerId(salerAuth.getShopSalerId());
TemplateMessage templateMessage = new TemplateMessage(salerAuth.getAuthStatus());
templateMessage.setAuthStatus(salerAuth.getAuthStatus());
templateMessage.setAuthId(salerAuth.getId());
templateMessagePublish.publish(new TemplateMessageEvent(templateMessage));
}

8.使用的第三方的SDK,不要重复造轮子,哈哈哈哈

      <dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>1.3.3</version>
</dependency>

注意小程序发送模板消息必须要前端在执行具体的业务逻辑时传一个form_id给后端,而且这个form_id的具体的有效时长只有七天,哈哈哈,过时不候啊。。。。

微信小程序模板消息后端代码的更多相关文章

  1. 微信小程序模板消息群发解决思路

    基于微信的通知渠道,微信为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.(微信6.5.2及以上版本支持模板功能.低于该版本将无法收到模板消息.) 模板推送位置:服务通 ...

  2. PHP实现推送微信小程序模板消息

    这边只会写如何实现,至于在公众号管理后台添加模板消息可以参考这篇文章: https://www.cnblogs.com/txw1958/p/wechat-template-message.html,当 ...

  3. 微信小程序模板消息

    1 先去微信公众平台,选择现有模板,会有一个模板编号,模板中没有的关键词,可以申请新增. 微信公众平台直达:https://mp.weixin.qq.com 模板消息对应文档直达:https://de ...

  4. 微信小程序上传图片(附后端代码)

    几乎每个程序都需要用到图片. 在小程序中我们可以通过image组件显示图片. 当然小程序也是可以上传图片的,微信小程序文档也写的很清楚. 上传图片 首先选择图片 通过wx.chooseImage(OB ...

  5. 微信小程序模板消息详解

    先放代码 wxml: <form name='pushMsgFm' report-submit bindsubmit='orderSign'> <view> 单号: 0< ...

  6. 记录一次用宝塔部署微信小程序Node.js后端接口代码的详细过程

    一直忙着写毕设,上一次写博客还是元旦,大半年过去了.... 后面会不断分享各种新项目的源码与技术.欢迎关注一起学习哈! 记录一次部署微信小程序Node.js后端接口代码的详细过程,使用宝塔来部署. 我 ...

  7. 微信小程序模板发送,openid获取,以及api.weixin.qq.com不在合法域名内解决方法

    主要内容在标题三,老手可直接跳到标题三. 本文主要解决个人开发者模板消息发送的问题(没有服务器,不能操作服务器的情况) 针对api.weinxin.qq.com不在以下合法域名列表内的问题提出的解决方 ...

  8. 【好好编程-技术博客】微信小程序开发中前后端的交互

    微信小程序开发中前后端的交互 微信小程序的开发有点类似与普通网页的开发,但是也不尽然相同.小程序的主要开发语言是JavaScript,开发同普通的网页开发有很大的相似性,对于前端开发者而言,从网页开发 ...

  9. node配置微信小程序解密消息以及推送消息

    上一篇文章介绍过 微信小程序配置消息推送,没有看过的可以先去查看一下,这里就直接去把那个客服消息接口去解密那个消息了. 在这里我选择的还是json格式的加密. 也就是给小程序客服消息发送的消息都会被微 ...

随机推荐

  1. Linux服务器重启后IP变掉的处理方式

    工作中有一台服务器为物理机,重启后IP就变掉了,影响到了使用,于是将服务器上的IP配置为静态方式,问题得以解决,具体如下: 1.登陆Linux服务器,cd /etc/sysconfig/network ...

  2. 【java基础之异常】死了都要try

    目录 1.异常 1.1 异常概念 1.2 异常体系 1.3 异常分类 1.4 异常的产生过程解析 2. 异常的处理 2.1 抛出异常throw 2.2 Objects非空判断 2.3 声明异常thro ...

  3. 在线运行.NET代码

    https://dotnetfiddle.net/ https://try.dot.net/ C# 发送Http协议 模拟 Post Get请求 1.参数 paramsValue的格式 要和 Requ ...

  4. 干货 | 深入分析 string.intern() 方法

    首先我们来看一段代码: public class InternTest {      public static void main(String[] args) {     String str1 ...

  5. C++中的多重继承(一)

    1,C++ 中是否允许一个类继承自多个父类? 1,可以: 2,这种情况就是多重继承: 3,多重继承的表象就是一个类有多个父类: 4,这是 C++ 非常特别的一个特性,在其他的程序设计语言中比如 C#. ...

  6. ubuntu 个人常用命令

    重启命令 :     1.reboot     2.shutdown -r now 立刻重启    3.shutdown -r 10 过10分钟自动重启    4.shutdown -r 20:35 ...

  7. 树形DP水题系列(1):FAR-FarmCraft [POI2014][luogu P3574]

    题目 大意: 边权为1 使遍历树时到每个节点的时间加上点权的最大值最小 求这个最小的最大值 思路: 最优化问题 一眼树形DP 考虑状态设立 先直接以答案为状态 dp[u] 为遍历完以u为根的子树的答案 ...

  8. Ubuntu下TP5隐藏入口文件

    部分内容是复制其他网友的博文,由于过了一段时间,找不到原文地址,再次表示感谢.以下是自己整理的,目的只是以后方便查阅 1.ubuntu或linux下找不到apache服务器配置文件httpd.conf ...

  9. AWS EC2 搭建 Hadoop 和 Spark 集群

    前言 本篇演示如何使用 AWS EC2 云服务搭建集群.当然在只有一台计算机的情况下搭建完全分布式集群,还有另外几种方法:一种是本地搭建多台虚拟机,好处是免费易操控,坏处是虚拟机对宿主机配置要求较高, ...

  10. Bashed -- hack the box

    Introduction Target: 10.10.10.68 (OS: Linux) Kali linux: 10.10.16.44 Information Enumeration Firstly ...