微信公众号发送模版消息

package com.wx.service.message;

import com.alibaba.fastjson.JSON;
import com.common.utils.HttpUtil;
import com.wx.service.utils.GlobalConstant;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuemd
 * @Date 2020/4/1
 * @description: 微信公众号发送模版消息
 *
 *  官方文档:https://mp.weixin.qq.com/debug/cgi-bin/readtmpl?t=tmplmsg/faq_tmpl
 *           https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
 *  微信公众平台接口测试帐号申请:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
 *
 * 通过向微信公众平台申请模板,来获取模板id。需提前申请,审核需要一段时间
 **/
public class TemplateMag {
    




    public static void sendTemMag() throws IOException {
        //获取ACCESSTOKEN
        String url = String.format(GlobalConstant.wechatDeploy.GETACCESSTOKEN, GlobalConstant.wechatDeploy.APPID, GlobalConstant.wechatDeploy.SECRETID);
        String str = HttpUtil.get(url);
        Map accessTokenMap = JSON.parseObject(str, Map.class);
        String accessToken = accessTokenMap.get("access_token").toString();
        String urlPost = String.format(GlobalConstant.wechatDeploy.URLPOST,accessToken);
        String post = HttpUtil.post(urlPost, dataMag());
        System.out.println(post);
    }


    public static void main(String[] args) throws IOException {
        sendTemMag();
    }

    /**
     *  模板内容
     * 	尊敬的{{User.DATA}}先生
     * 	你的尾号为{{CardNumber.DATA}} 的XX信用卡
     * 	交易时间:{{DeadTime.DATA}}
     * 	交易类型:{{Type.DATA}}
     * 	交易金额:{{Money.DATA}}
     *
     **/


    /**
     *  设置post数据
     * @author xuemd
     * @date 2020/4/1 11:24
     **/
    private static String dataMag(){
        Map<String,Object> user = new HashMap<>();
        user.put("value","黄先生");
        user.put("color","#173177");
        Map<String,Object> cardnumber = new HashMap<>();
        cardnumber.put("value","0426");
        cardnumber.put("color","#173177");
        Map<String,Object> type = new HashMap<>();
        type.put("value","消费");
        type.put("color","#173177");
        Map<String,Object> money = new HashMap<>();
        money.put("value","人民币260.00元");
        money.put("color","#173177");
        Map<String,Object> deadtime = new HashMap<>();
        deadtime.put("value","06月07日19时24分");
        deadtime.put("color","#173177");

        Map<String,Object> mapData = new HashMap<>();
        mapData.put("User",user);
        mapData.put("CardNumber",cardnumber);
        mapData.put("Type",type);
        mapData.put("Money",money);
        mapData.put("DeadTime",deadtime);


        Map<String,Object> map = new HashMap<>();
        map.put("touser", GlobalConstant.wechatDeploy.OPENID);
        map.put("template_id","xxxxxxxxxxxxxxx");
        map.put("data",mapData);
        map.put("url","https://www.baidu.com/");

        return JSON.toJSONString(map);
    }
}
package com.wx.service.utils;

/**
 * @author xuemd
 * @Date 2020/4/1
 * @description: 公共参数
 **/
public class GlobalConstant {

    /**
     * 微信参数
     */
    public static class wechatDeploy {
        public static final String APPID = "xxxxxxxxxxxx";
        public static final String SECRETID = "xxxxxxxxxxxx";
        public static final String OPENID = "xxxxxxxxx";
        public static  String GETACCESSTOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
        public static String URLPOST = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
    }
}
APPID,SECRETID,OPENID 需要自己获取

模版设置

返回效果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

丢了尾巴的猴子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值