利用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. python学习之模块-模块(一)

    第五章 5.1 自定义模块 模块概念: ​ 把一些常用的函数放在一个py文件中,这个文件就称之为模块. 模块的意义: ​ 1.方便管理.让程序的解构更加清晰,实现功能的重复使用: ​ 2.提升开发效率 ...

  2. 运行RGB-DSLAM的一些报错及处理方法

    part 4 报错‘create’ is not a menber of 'CV::FeatureDetector::create(detector.c_str()); 查看opencv版本 修改Cm ...

  3. vs2015中将复制过来的文件夹显示目录文件

    先将文件夹和文件复制到VS程序所在的位置,点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文件夹,这样你就可以看到这个文件或者文件夹了,这时,这个文件或者文件夹是虚线构成的.你右击这个文件或者 ...

  4. 四、Kubernetes_V1.10集群部署-master-创建kubeconfig

    1.生成配置文件 # 创建 TLS Bootstrapping Token # export BOOTSTRAP_TOKEN=$( /dev/urandom | od -An -t x | tr -d ...

  5. 【转帖】联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s

    联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s https://www.cnbeta.com/articles/tech/855223.htm 国产主控 紫光做国产颗粒 国产器件对 ...

  6. SSM笔记

    Spring Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象.也可以称之为项目中的粘合剂. Spring的核心思想是IoC(控制 ...

  7. 【6.18校内test】T3细胞分裂

    尽管T1T2很简单,但还是阻止不了我T3wa一片 细胞分裂[题目链接] xcg同学有一个80pts的代码 他说他的代码和我的很像,可惜我比较笨,只有30pts 其实这道题考场上是想到要分解质因数了,然 ...

  8. List<HashMap<String,String>> list, 根据hashmap中的某个键的值排序

    来源https://blog.51cto.com/zhaodan/1725249 //可以使用Collections.sort(List list, Comparator c)来实现 这里举例hash ...

  9. Linux免密登陆设置了免密登陆为啥还需要输入密码

    一.设置了免密码登陆但是还是需要输入密码: 权限保证:1.authorized-keys 的权限为 600 2.home.账户所在的目录如hadoop..ssh这三个文件的权限都必须设置为700,缺少 ...

  10. 会引起全表扫描的几种SQL 以及sql优化 (转)

    出处: 查询语句的时候尽量避免全表扫描,使用全扫描,索引扫描!会引起全表扫描的几种SQL如下 1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like ‘ ...