微信公众号推送消息格式如下
{
"touser":"OPENID",
"template_id":"****",
"url":"***",
"miniprogram":{
"appid":"***",
"pagepath":"index?foo=bar"
},
"client_msg_id":"MSG_000001",
"data":{
"first": {
"value":"***!",
"color":"#173177"
},
"keyword1":{
"value":"***",
"color":"#173177"
},
"keyword2": {
"value":"***",
"color":"#173177"
},
"keyword3": {
"value":"***",
"color":"#173177"
},
"remark":{
"value":"***",
"color":"#173177"
}
}
}
TemplateParam
公众号消息推送参数信息实体
/**
* 类说明: 公众号消息推送参数信息实体
*
* @author wqf
* @date 2022/10/13 15:27
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TemplateParam {
/**
* 参数值
*/
private String value;
/**
* 参数颜色
*/
private String color;
}
WxTemplate
公众号消息推送数据实体
/**
* 类说明: 公众号消息推送数据实体
*
* @author wqf
* @date 2022/10/13 15:28
*/
@Data
public class WxTemplate implements Serializable {
/**
* 推送用户 openid
*/
@JsonProperty(value = "touser")
String toUser;
/**
* 模板id
*/
@JsonProperty(value = "template_id")
private String templateId;
/**
* 模板消息详情跳转链接
*/
private String url;
/**
* 消息顶部的颜色
*/
@JsonProperty(value = "topcolor")
private String topColor;
/**
* 参数列表
*/
private Map data;
}
MessagePushTemplateAssembly
模板消息数据组装类
/**
* 类说明: 模板消息数据组装类
*
* @author wqf
* @date 2022/10/13 15:28
*/
public class MessagePushTemplateAssembly {
/**
* 方法描述: XXXX推送模板填充
*
* @param data 填充数据
* @return java.util.List
* @author wqf
* @date 2022/10/18 13:56
*/
public static Map XXTemplateAssemble(DisRoomWarnMsgDTO data) {
Map resData = new HashMap<>();
resData.put("first", new TemplateParam(parameter1, "#173177"));
resData.put("keyword1", new TemplateParam(parameter2 "#173177"));
resData.put("keyword2", new TemplateParam(parameter3, "#173177"));
resData.put("keyword3",new TemplateParam(parameter4, "#173177"));
resData.put("keyword4", new TemplateParam(parameter5, "#173177"));
resData.put("remark",new TemplateParam(parameter6, "#173177"));
return resData;
}
}
sendTemplateMsg
发送消息
public static void sendTemplateMsg(WxTemplate templateData) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken();
log.info("发送消息:{}", JsonUtils.objectToJson(templateData));
HttpEntity formEntity = new HttpEntity<>(JSONUtil.toJsonStr(templateData), getHeaders());
restTemplate.postForEntity(url, formEntity, Map.class);
}