微信小プログラム~模版メッセージプッシュ

5814 ワード

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