Bean:
@Data
@ApiModel("微信消息推送信息接收")
public class NoticeVo {
@ApiModelProperty
public static final String PREFIX = "|";
@ApiModelProperty(name = "用户ID", hidden = true)
private Long userId;
@NotNull(message = "模版ID不可为空")
@ApiModelProperty("所需下发的模板消息的id")
private String templateId;
@ApiModelProperty("非必填 点击模板卡片后的跳转页面,仅限本小程序内的页面。
支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。")
private String page;
@NotNull(message = "表单或支付ID不可为空")
@ApiModelProperty("非必填(推送时若不填则自动查找可用formId),表单提交场景下,
为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id")
private String formId;
@ApiModelProperty("模板内容,不填则下发空模板,例如:339208499|2018年7月8日|喜来登酒店|广州市天河区天河路208号")
private String data;
@ApiModelProperty("非必填 模板内容字体的颜色,不填默认黑色 【废弃】")
private String color;
@ApiModelProperty("非必填 模板需要放大的关键词,不填则默认无放大,例如:keyword1.DATA")
private String emphasisKeyword;
public NoticeVo() {
}
public NoticeVo(Long userId, String templateId, String page, String formId, String data, String emphasisKeyword) {
this.userId = userId;
this.templateId = templateId;
this.page = page;
this.formId = formId;
this.data = data;
this.emphasisKeyword = emphasisKeyword;
}
public NoticeVo(Long userId, String templateId, String data, String emphasisKeyword) {
this.userId = userId;
this.templateId = templateId;
this.data = data;
this.emphasisKeyword = emphasisKeyword;
}
} 调用方法: String key = "AccessToken";
@Override
public String sendNotice(NoticeVo noticeVo) throws IOException, URISyntaxException {
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
JSONObject params = new JSONObject();
//获取access_token
String access_token;
if (RedisCacheUtil.exists(key) && StringUtils.isNotBlank(RedisCacheUtil.getString(key))) {
access_token = RedisCacheUtil.getString(key);
} else {
access_token = getAccessToken();
}
url += "?access_token=" + access_token;
params.put("access_token", access_token);
//获取用户openId
String openId = dictRepository.findOpenIdByUserId(noticeVo.getUserId());
params.put("touser", openId);
params.put("template_id", noticeVo.getTemplateId());
params.put("page", noticeVo.getPage());
Notice notice = null;
if (StringUtils.isBlank(noticeVo.getFormId())) {
//查询可用的FormId
Optional noticeOptional = noticeRepository.findUsableFormId(noticeVo.getUserId());
if (noticeOptional.isPresent()) {
notice = noticeOptional.get();
noticeVo.setFormId(notice.getFormId());
} else {
log.error(" WeChat ERROR => No usable FormId/PayId");
return "-1001";
}
}
params.put("form_id", noticeVo.getFormId());
params.put("emphasis_keyword", noticeVo.getEmphasisKeyword());
if (StringUtils.isNotBlank(noticeVo.getData())) {
JSONObject ks = new JSONObject();
String [] datas = noticeVo.getData().split("\\|");
Iterables.forEach(Arrays.asList(datas), (i, t) -> {
JSONObject val = new JSONObject();
val.put("value", t.trim());
ks.put("keyword" + (i + 1), val);
});
params.put("data", ks);
}
// System.out.println(params);
String b = HttpUtil.httpsRequest(url, "GET", params.toString());
JSONObject res = JSONObject.parseObject(b);
if (res != null && res.size() > 0 && res.containsKey("errcode")) {
String recode = res.getString("errcode");
//修改notice状态
if (notice != null) {
if ("0".equals(recode)) {
notice.setDelStatus(1l);
log.info(" WeChat notice succeed! ");
} else if ("40001".equals(recode)) {
getAccessToken();
log.error(" WeChat ERROR notice succeed! => msg: AccessToken in Redis has been updated! ");
} else {
log.error(" WeChat ERROR notice succeed! => userId:" + noticeVo.getUserId() + " errorCode:" + recode);
}
notice.setTemplateId(noticeVo.getTemplateId());
notice.setNoticeDate(LocalDateTime.now().withNano(0));
notice.setNoticeStatus(recode);
noticeRepository.save(notice);
}
return recode;
}
return null;
} 测试: @Test
public void testSendNotice() throws IOException, URISyntaxException {// 1527140636499 1527140637082 1527140637468
// Long userId, String templateId, String page, String formId, String data, String emphasisKeyword
String notice = noticeService.sendNotice(new NoticeVo(15l, "*************************", "**", "",
"订单取消|超时未付款|1289kjdasdjalk56|进口金枕头榴莲", "keyword1.DATA"));
System.out.println("notice " + notice);
}