利用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. MYSQL连接一段时间不操作后出现异常的解决方案

    最近做的网站使用的是MYSQL数据库 发现 果超过8小时应用程序不去访问数据库,数据库就断掉连接 .这时再次访问就会抛出异常,如下所示: com.mysql.jdbc.exceptions.jdbc4 ...

  2. 简单的servlet下载

    <servlet> <servlet-name>servletTest</servlet-name> <servlet-class>com.shangs ...

  3. CSS 设计指南(第3版) 初读笔记

    第1章 HTML标记与文档结构 关于<title>标签:搜索引擎会给<title>标签中的文字内容赋予很高的权重.而且这些文字也会作为网页标题出现在搜索结果列表中. 无论你想了 ...

  4. 【HTML】---HTML语义化

    1.什么是HTML语义化? <基本上都是围绕着几个主要的标签,像标题(H1~H6).列表(li).强调(strong em)等等> 根据内容的结构化(内容语义化),选择合适的标签(代码语义 ...

  5. eclipse 引用静态库设置选项

    环境说明: 静态库文件项目:engine C++ 项目:server 在server项目中引用静态库的库文件libEngine.a 需要设置如图选项,才能引用静态库项目里的文件 主要设置: 1.inc ...

  6. Centos(64位)安装Hbase详细步骤

    HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件 ...

  7. idea运行时 Process finished with exit code -1073741819 (0xC0000005)

    问题描述:        idea中启动项目报   Process finished with exit code -1073741819 (0xC0000005) ,如图所示: 问题解决:      ...

  8. 来自 Vue 3.0 的 Composition API 尝鲜

    来自 Vue 3.0 的 Composition API 尝鲜:https://segmentfault.com/a/1190000020205747

  9. 20 亿的 URL 集合,如何快速判断其中一个?

    假设遇到这样一个问题:一个网站有 20 亿 url 存在一个黑名单中,这个黑名单要怎么存?若此时随便输入一个 url,你如何快速判断该 url 是否在这个黑名单中?并且需在给定内存空间(比如:500M ...

  10. redis 列表 数据类型

    列表 rpush dname  技术部  后勤部 售后部 lpush  dname   秘书部 lset dname 2  销售部     修改 lrange dname 0 -1   打印所有列表 ...