微信开发(六)--模板消息接口
1 微信开发文档如下图2. 模板消息推送javaBean封装package com.ruoyi.platform.weixin.domainTemplate;import lombok.Data;@Datapublic class Template {//接收者openidprivate String touser;//模板IDprivate...
·
1 微信开发文档如下图
2. 模板消息推送javaBean封装
package com.ruoyi.platform.weixin.domainTemplate;
import lombok.Data;
@Data
public class Template {
//接收者openid
private String touser;
//模板ID
private String template_id;
//模板跳转链接
private String url;
//跳小程序所需数据,不需跳小程序可不用传该数据
private String miniprogram;
private MsgData data;
public Template(String touser, String template_id, String url, String miniprogram, MsgData data) {
this.touser = touser;
this.template_id = template_id;
this.url = url;
this.miniprogram = miniprogram;
this.data = data;
}
}
根据自己选择的模板样式来封装模板数据
package com.ruoyi.platform.weixin.domainTemplate;
import lombok.Data;
@Data
public class MsgData {
private MsgContent first;
//报警类型 1消火栓 2烟感器 3消防水压 4水箱 5安全用电
private MsgContent keyword1;
//设备名称
private MsgContent keyword2;
//报警内容
private MsgContent keyword3;
//报警时间
private MsgContent keyword4;
//地址
private MsgContent remark;
public MsgData(MsgContent first, MsgContent keyword1, MsgContent keyword2, MsgContent keyword3, MsgContent keyword4, MsgContent remark) {
this.first = first;
this.keyword1 = keyword1;
this.keyword2 = keyword2;
this.keyword3 = keyword3;
this.keyword4 = keyword4;
this.remark = remark;
}
}
package com.ruoyi.platform.weixin.domainTemplate;
import lombok.Data;
@Data
public class MsgContent {
private String value;
private String color;
public MsgContent(String value, String color) {
this.value = value;
this.color = color;
}
}
package com.ruoyi.platform.weixin.domainTemplate;
public enum EnumEquipment {
Fire("1", "消火栓"), Smoke("2", "烟感器"), WaterPress("3", "消防水压"),WaterBox("4","消防水箱"),Aqyd("5","安全用电");
private final String code;
private final String name;
EnumEquipment(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
package com.ruoyi.platform.zxhPushMsg.domain;
public enum EnumAlarmReason {
XF_yongshui("110", "用水"),
XF_kaigai("120", "开盖"),
XF_dianyadi("130", "电池电压低"),
XF_shuiyadi("140","水压低"),
XF_shuiweidi("220","水位低"),
XF_huojing("210","火警"),
XF_lixian("200","离线"),
AQYD_shengyudianliu("150","剩余电流"),
AQYD_guoya("160","过压"),
AQYD_guoliu("170","过流"),
AQYD_qianya("180","欠压"),
AQYD_chaowen("190","超温"),
AQYD_duanxian("230","断线"),
AQYD_duanlu("240","短路"),
AQYD_lixian("999","离线");
private final String code;
private final String name;
EnumAlarmReason(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
3. 模板消息推送服务
package com.ruoyi.platform.weixin.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.BdTools;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.HttpsUtil;
import com.ruoyi.platform.weixin.domainTemplate.EnumEquipment;
import com.ruoyi.platform.weixin.domainTemplate.MsgContent;
import com.ruoyi.platform.weixin.domainTemplate.MsgData;
import com.ruoyi.platform.weixin.domainTemplate.Template;
import com.ruoyi.platform.zxhPushMsg.domain.EnumAlarmReason;
import com.ruoyi.platform.zxhPushMsg.domain.ZxhPushMsg;
import com.ruoyi.platform.zxhPushMsg.service.IZxhPushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 模板消息推送服务
*/
@Service
public class TemplateMessageService {
@Autowired
private WeiXinTokenService weiXinPublicService;
@Autowired
private IZxhPushMsgService zxhPushMsgService;
/**
* 定时任务轮询的方法
*/
public void selectPushMsg() {
QueryWrapper<ZxhPushMsg> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("zpm.del_flag", 0).eq("zpm.send_status", 0)
.eq("zpm.msg_type", 1).eq("zpm.msg_status", 0);
//sql语句左关联了用户表且已经按时间升序
List<ZxhPushMsg> zxhPushMsgLsit = zxhPushMsgService.selectZxhPushMsgList(queryWrapper);
if (!zxhPushMsgLsit.isEmpty()) {
for (ZxhPushMsg zxhPushMsg : zxhPushMsgLsit) {
String openId = zxhPushMsg.getOpenId();
if (BdTools.notEmpty(openId)) {
String equipmentTypeName = this.getEquipmentType(zxhPushMsg.getHardwareType());
String hardwareName = zxhPushMsg.getHardwareName();
String alarmReasonInfo = this.alarmReasonInfo(zxhPushMsg.getPlatformType(), zxhPushMsg.getAlarmReason(), zxhPushMsg.getAlarmValue());
String occurrenceTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, zxhPushMsg.getOccurrenceTime());
String address = zxhPushMsg.getAddress();
Boolean aBoolean = this.initTemplateMsg(openId, equipmentTypeName, hardwareName, alarmReasonInfo, occurrenceTime, address);
if (aBoolean) {
//推送微信消息成功
//业务1:修改发送状态为已发送
this.updateSendStatus(zxhPushMsg.getId(), 1);
} else {
//推送微信消息失败--》更新发送状态为发送失败
this.updateSendStatus(zxhPushMsg.getId(), 99);
}
}
}
}
}
/**
* 封装接口所需格式的模板消息推送
*
* @param openId 用户openId
* @param equipmentTypeName 设备类型 消火栓1 烟感器2 水压3 水箱4 安全用电5
* @param equipmentName 设备名称
* @param alarmContent 报警内容
* @param occurrenceTime 报警发生时间
* @param address 发生地址
* @return
*/
public Boolean initTemplateMsg(String openId, String equipmentTypeName, String equipmentName, String alarmContent, String occurrenceTime, String address) {
// 模板ID
String templateId = "jbYukFYPThnqiPSKZQ5MUYQU2AxPSOtAY3WvVi8cbRw";
MsgContent first = new MsgContent("中旭昊智能科技提醒您", "#173177");
// 报警类型 设备类型(1消火栓 2烟感器 3消防水压 4水箱 5安全用电)
MsgContent kw1 = new MsgContent(equipmentTypeName, "#173177");
// 设备名称
MsgContent kw2 = new MsgContent(equipmentName, "#173177");
// 报警内容
MsgContent kw3 = new MsgContent(alarmContent, "#FF0000");
//报警时间
MsgContent kw4 = new MsgContent(occurrenceTime, "#173177");
// 报警地址
MsgContent remark = new MsgContent(address, "#173177");
MsgData msgData = new MsgData(first, kw1, kw2, kw3, kw4, remark);
Template template = new Template(openId, templateId, "http://qh.zxhznkj.com:8070/", "", msgData);
String jsonData = JSONObject.toJSONString(template);
//调用模板消息接口
Boolean aBoolean = this.sendTemplateMsg(jsonData);
return aBoolean;
}
/**
* 调用模板消息接口
*
* @param jsonData
* @return
*/
public Boolean sendTemplateMsg(String jsonData) {
//获取有效accessToken
String accessToken = weiXinPublicService.accessTokenIsValid();
String sendTemplatemsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String jsonResult = null;
try {
/**
* {
* "errcode":0,
* "errmsg":"ok",
* "msgid":200228332
* }
*/
jsonResult = HttpsUtil.post(sendTemplatemsgUrl, null, jsonData);
System.out.println(">>>>>>>>>>>>>>>>模板消息发送完成:" + jsonResult + ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
JSONObject object = JSON.parseObject(jsonResult);
String errmsg = object.getString("errmsg");
if ("ok".equals(errmsg)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
/**
* 获取设备类型编号所对应的名称
*
* @param equipmentType 消火栓1 烟感器2 水压3 水箱4 安全用电5
* @return
*/
public String getEquipmentType(Integer equipmentType) {
switch (equipmentType) {
case 1:
return EnumEquipment.Fire.getName();
case 2:
return EnumEquipment.Smoke.getName();
case 3:
return EnumEquipment.WaterPress.getName();
case 4:
return EnumEquipment.WaterBox.getName();
case 5:
return EnumEquipment.Aqyd.getName();
}
return null;
}
/**
* 重新组装报警原因提示信息
*
* @param platformType 消息来源类型(1 消防 2用电)
* @param alarmReason 报警原因
* @param alarmValue 报警值
* @return
*/
public String alarmReasonInfo(Integer platformType, String alarmReason, String alarmValue) {
String msg = null;
//封装智慧消防平台报警信息
if (platformType == 1) {
if (EnumAlarmReason.XF_yongshui.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_yongshui.getName();
} else if (EnumAlarmReason.XF_kaigai.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_kaigai.getName();
} else if (EnumAlarmReason.XF_dianyadi.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_kaigai.getName();
} else if (EnumAlarmReason.XF_shuiyadi.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_shuiyadi.getName();
} else if (EnumAlarmReason.XF_shuiweidi.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_shuiweidi.getName();
} else if (EnumAlarmReason.XF_huojing.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_huojing.getName();
} else if (EnumAlarmReason.XF_lixian.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.XF_lixian.getName();
}
if (BdTools.notEmpty(alarmValue)) {
return "发生" + msg + "报警,设备报警值" + alarmValue + "请及时查看处理.";
}
return "发生" + msg + "报警,请及时查看处理";
}
//封装安全用电平台报警信息
if (platformType == 2) {
if (EnumAlarmReason.AQYD_shengyudianliu.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_shengyudianliu.getName();
} else if (EnumAlarmReason.AQYD_guoya.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_guoya.getName();
} else if (EnumAlarmReason.AQYD_guoliu.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_guoliu.getName();
} else if (EnumAlarmReason.AQYD_qianya.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_qianya.getName();
} else if (EnumAlarmReason.AQYD_chaowen.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_chaowen.getName();
} else if (EnumAlarmReason.AQYD_duanxian.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_duanxian.getName();
} else if (EnumAlarmReason.AQYD_duanlu.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_duanlu.getName();
} else if (EnumAlarmReason.AQYD_lixian.getCode().equals(alarmReason)) {
msg = EnumAlarmReason.AQYD_lixian.getName();
}
if (BdTools.notEmpty(alarmValue)) {
return "发生" + msg + "报警,设备报警值" + alarmValue + "请及时查看处理.";
}
return "发生" + msg + "报警,请及时查看处理";
}
return null;
}
/**
* 微信消息推送完成后,更新微信消息表中的发送状态
*
* @param pushMsgId
* @param sendStatus 0 未发送 1 已发送 99 发送失败
* @return
*/
public Boolean updateSendStatus(Integer pushMsgId, Integer sendStatus) {
ZxhPushMsg zxhPushMsg = new ZxhPushMsg();
zxhPushMsg.setId(pushMsgId);
zxhPushMsg.setSendStatus(sendStatus);
boolean byId = zxhPushMsgService.updateById(zxhPushMsg);
return byId;
}
}
更多推荐
已为社区贡献20条内容
所有评论(0)