利用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的具体的有效时长只有七天,哈哈哈,过时不候啊。。。。