一.模板推送的案列效果图

二.模板推送的案列模板
2.1 第一次进来的需要添加模板

2.2 标题这里定的"告警通知"

2.3 模板内容
告警内容:{{content.DATA}}
告警发生时间:{{occurtime.DATA}}
{{remark.DATA}}
三.java代码实现
2.1 创建消息推送对象
package com.grand.weichat.entity;
import lombok.Data;
/**
* 消息推送实体对象
* @author Administrator
*
*/
@Data
public class PushMessage {
private String openid; //用户唯一编号
private String passport; //账号
private String id; //告警ID号
private String warnType; //picWarn图片违规,valueWarn浓度告警,areaWarn区域告警
private String title; //标题
private String content; //告警内容
private String time; //告警时间
private String remark; // 备注
}
2.2 接口代码
/**
* 推送消息
* @param openId 用户唯一编号 : 前面有说到获取方法,获取后存入数据库,后续从库中取用
* @param passport 账号 :openid与平台的关联账号,
* @param id 告警ID号
* @param warnType picWarn图片违规,valueWarn浓度告警,areaWarn区域告警
* @param title 标题
* @param content 告警内容
* @param time 告警时间
* @param remark 备注
* 此接口调用地址:http://sthj.xxx.com.cn/weixin/wx/pushMessage
*/
@RequestMapping("pushMessage")
@ResponseBody
private void pushMessage(
HttpServletRequest request,
HttpServletResponse response,
PushMessage pushMessage
) {
String strresult=null;
if(pushMessage.getOpenid()==null) {
strresult="openId不能为空";
}
if(pushMessage.getPassport()==null) {
strresult="账号不能为空";
}
if(pushMessage.getId()==null) {
strresult="告警ID号不能为空";
}
if(pushMessage.getWarnType()==null) {
strresult="违规类别不能为空";
}
if(pushMessage.getTitle()==null) {
strresult="标题不能为空";
}
if(pushMessage.getContent()==null) {
strresult="告警内容不能为空";
}
if(pushMessage.getTime()==null) {
strresult="告警时间不能为空";
}
if(pushMessage.getRemark()==null) {
strresult="备注不能为空";
}
JSONObject jsonrst = new JSONObject();
jsonrst.put("code", 0);
jsonrst.put("msg", "success");
if(strresult!=null) {
jsonrst.put("data", strresult);
try {
response.getWriter().print(jsonrst.toString());
return;
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//更具传入的参数组装详情地址
String detailsUrl="http://weixin.grand-tech.com.cn:22806/static/wxmsg/alarm.html?id=valueid&warnType=valuetype&token=1_grand";
detailsUrl=detailsUrl.replace("valueid", pushMessage.getId()).replace("valuetype", pushMessage.getWarnType());
//获取token
String token=WXUtilsCenter.getAccessToken(appId,appSecret);
// 模板接口
String urlindex = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
String data="{"touser":"openid","template_id":"tmid","url":"urlpath","data":{"first":{"value":"title","color":"#000000"},"content":{"value":"keyword1","color":"#173177"},"occurtime":{"value":"keyword2","color":"#173177"},"remark":{"value":"keyword3","color":"#173177"}}}";
data=data.replace("openid", pushMessage.getOpenid())//openId
.replace("tmid",templateId) //模板Id
.replace("urlpath",detailsUrl)
.replace("title", pushMessage.getTitle())
.replace("keyword1", pushMessage.getContent())
.replace("keyword2", pushMessage.getTime())
.replace("keyword3", pushMessage.getRemark());
String result = HttpUtil.post(urlindex, data);
JSONObject json=JSONObject.fromObject(result);
strresult="信息推送成功";
// 显示推送结果
log.info(strresult);
}catch (Exception e) {
e.printStackTrace();
strresult="信息推送失败,出现异常!";
}
jsonrst.put("data", strresult);
int logRecord = userService.insertWxUserLog(pushMessage.getOpenid(), pushMessage.getPassport(), pushMessage.toString());
log.info("消息推送的日志存储结果"+logRecord);
try {
response.getWriter().print(jsonrst.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
2.3 接口调用测试
这里借助于hutool工具,pom引入如下:
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.0</version> </dependency>
在其他项目中引入如上的pom后编写测试类,具体如下:
/**
* 调用平台接口 推送消息
*/
@Test
public void TestPushMessage() {
Map<String,Object> map=new HashMap<>();
map.put("openid", "oi1pawLJSyLOyBBtpaZafyabLbn0");
map.put("id","5300159");
map.put("warnType","picWarn");
map.put("title", "AI全息监控告警通知");
map.put("passport", "cqshbj");
map.put("content", "pm25浓度超标10%,剩余控制量为10mg。");
map.put("time", "2020年9月22日");
map.put("remark", "图片告警");
String url="http://sthj.xxx.com.cn/weichat/wx/pushMessage";
//链式构建请求
String result = HttpUtil.post(url, map);
System.out.println("======>"+result);
}
这样一个规范的模板推送接口就完成了,当然,这里的实现都是根据官方文档编写实现而来的。