编程语言
首页 > 编程语言> > 微信小程序(公众号消息推送)

微信小程序(公众号消息推送)

作者:互联网

 /**
     * 公众号消息推送测试
     *
     * @param
     * @return
     */
    @RequestMapping("/test2")
    @ResponseBody
    public Map test2(String str) {
        String tplId = "ILexWDTU0srCw5VfZfBLpU86XMbJiMyCsDpE4N4ucwo";
        Template tem = new Template();
        tem.setTemplateId(tplId);
        tem.setTopColor("#333");

        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        paras.add(new TemplateParam("first", "测试数据", "#333"));
        paras.add(new TemplateParam("keyword1", "测试数据", "#333"));
        paras.add(new TemplateParam("keyword2", "测试数据", "#333"));
        paras.add(new TemplateParam("keyword3", "测试数据", "#333"));
        paras.add(new TemplateParam("keyword4", "测试数据", "#333"));
        tem.setTemplateParamList(paras);
        tem.setToUser(str);//用户openid
        //设置超链接
        tem.setUrl("www.baidu.com");
        Map<String, Object> stringObjectMap = SendTemplateMsg.sendTemplateMsg(tem);
        return stringObjectMap;
    }
 public static Map<String, Object> sendTemplateMsg(Template template){

        //获取token
        String token = GetAccessToken.getAccessToken();

        String requestUrl ="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

        requestUrl=requestUrl.replace("ACCESS_TOKEN", token);

        Map<String, Object> stringObjectMap = HttpUtils.doPost(requestUrl, template.toJSON());

        return stringObjectMap;


    }
@Data
public class Template {

    // 消息接收方
    private String toUser;
    // 模板id
    private String templateId;
    // 模板消息详情链接
    private String url;
    // 消息顶部的颜色
    private String topColor;
    // 参数列表
    private List<TemplateParam> templateParamList;
    //省略getter、setter方法

    //按微信接口要求格式化模板
    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.toUser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.templateId)).append(",");
        buffer.append(String.format("\"url\":\"%s\"", this.url)).append(",");
        buffer.append(String.format("\"topcolor\":\"%s\"", this.topColor)).append(",");
        buffer.append("\"data\":{");
        TemplateParam param = null;
        for (int i = 0; i < this.templateParamList.size(); i++) {
            param = templateParamList.get(i);
            // 判断是否追加逗号
            if (i < this.templateParamList.size() - 1) {

                buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", param.getName(), param.getValue(), param.getColor()));
            } else {
                buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", param.getName(), param.getValue(), param.getColor()));
            }

        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }

}
public class TemplateParam {

    String name;
    String value;
    String color;
}

标签:String,buffer,微信,param,paras,公众,new,推送,append
来源: https://blog.csdn.net/weixin_48691883/article/details/121010862