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 需要自己获取
模版设置
返回效果