微信小程序~模版消息推送

            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<Notice> 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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cpongo11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值