微信订阅消息 - yiyanglij/yiyanglij.github.io GitHub Wiki

注意:每个模板ID都不一样,不一样的订阅消息有不一样的模板

Controller

@ApiOperation("发送订阅消息")
@ApiResponse(code = 200, message = "success", response = Page.class)
@ApiImplicitParams({
        @ApiImplicitParam(name = "touser", value = "用户openid", required = true, dataType = "String"),
        @ApiImplicitParam(name = "template_id", value = "订阅消息模版id", required = true, dataType = "String"),
        @ApiImplicitParam(name = "page", value = "默认跳到小程序首页", required = false, dataType = "String"),
        @ApiImplicitParam(name = "v1", value = "内容相关信息", required = true, dataType = "String"),
        @ApiImplicitParam(name = "v2", value = "内容相关信息", required = true, dataType = "String"),
        @ApiImplicitParam(name = "v3", value = "内容相关信息", required = true, dataType = "String"),
        @ApiImplicitParam(name = "v4", value = "内容相关信息", required = true, dataType = "String"),
        @ApiImplicitParam(name = "v5", value = "内容相关信息", required = true, dataType = "String"),
})
@PostMapping("sendMessage")
String sendMessage(@RequestBody Message message);

ServiceImpl

@Override
public String sendMessage(Message message) {

    cn.hutool.json.JSONObject jsonObject=new cn.hutool.json.JSONObject();
    jsonObject.set("touser",message.getTouser());
    jsonObject.set("template_id",message.getTemplate_id());
    jsonObject.set("page","pages/home/myTheOrder/index");

    cn.hutool.json.JSONObject json1=new cn.hutool.json.JSONObject();
    json1.set("character_string6",new cn.hutool.json.JSONObject().set("value",message.getV1()));
    json1.set("amount10",new cn.hutool.json.JSONObject().set("value",message.getV2()));
    json1.set("thing8",new cn.hutool.json.JSONObject().set("value",message.getV3()));
    json1.set("time11",new cn.hutool.json.JSONObject().set("value",message.getV4()));
    json1.set("name12",new cn.hutool.json.JSONObject().set("value",message.getV5()));
    jsonObject.set("data",json1);

    //发送
    String aa = cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send? 
    access_token=58_WCzYZcKjpnakHHr2bGHhsU6rXlrmLvrq02z8WJ2-Ks3uqcIh5-EDQNLHZz5vFEU1_lAbnD3Jimrue5kCSKqK_1-2HDfz7FT2fW0kvs0IH2- 
                 3oexI8OWri-hIfdiXvYZG28cD-h-jF-rTvGSVRPFjAAAVTJ",jsonObject.toString());
    log.info("请求发送信息接口返回:{}",aa);
    System.out.println(aa);
    return "ok";
}