利用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. css换行用省略号代替

    css换行用省略号代替,也可以说是长标题的文章可以使用简单的CSS样式实现省略号控制显示. 一般的文字截断(适用于内联与块): .text-overflow{ display:block;/*内联对象 ...

  2. tensorflow各个版本的CUDA以及Cudnn版本对应关系

    概述,需要注意以下几个问题: (1)NVIDIA的显卡驱动程序和CUDA完全是两个不同的概念哦!CUDA是NVIDIA推出的用于自家GPU的并行计算框架,也就是说CUDA只能在NVIDIA的GPU上运 ...

  3. Junit4 简单使用

    一.环境搭建 对于习惯使用Eclipse开发平台来说,Junit早已是非常通常的插件,在Eclipse开发平台中,可以非常方便地搭建Junit测试环境. 1.在Eclipse上创建工程,任何Java工 ...

  4. C语言I博客作业12

    一.我学到的内容 二.我的收获 作业链接 收获 博客第一次作业:https://www.cnblogs.com/gm12/p/11584148.html 第一次作业收获:第一次作业是我初步接触C语言的 ...

  5. Windows下备份mysql

    ---恢复内容开始---  Windows下备份mysql 第一步 编写脚本 ::设置时间变量 set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%%time ...

  6. 从入门到自闭之python初识

    Day 01 整型: 对比: 在python 2 版本中有整型,长整型long 在python 3 版本中全部都是整型 用于计算和比较 整型和布尔值的转换 二进制转换成十进制: ​print (int ...

  7. python-day15(正式学习)

    目录 递归 函数自我嵌套 调用 直接调用 间接调用 为什么要用递归呢 如何使用递归 内置函数 掌握 了解 面向对象方法 面向过程编程 注册 分层实现功能 递归 递归的本质就是函数调用自身,当然也会有一 ...

  8. python 链接mysql 连接池

    # python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...

  9. pythonWeb框架创建app模块以及虚拟环境管理工具

    在进行项目搭建的时候,如果有多个功能模块,以及多个网页地址时,为了系统的可维护性,以及易读性,我们大多数情况下选择模块化开发 所以我们就要使用app指令来创建不同的功能模块 首先项目框架如下: 接下来 ...

  10. sql认识

    DDL – Data Definition Language数据定义语言DML – Data Manipulation Language数据操作语言DCL – Data Control Languag ...