一、配置模版消息
1、在微信公众号测试平台中添加模版消息,注意模版消息有格式要求:
(1)模板中参数内容必须以".DATA"结尾,否则视为保留字;
(2)模板保留符号"{{ }}"

2、获取template_id(非常重要)

二、使用步骤
1、微信消息推送模版参数类(注意:属性名写死的不能改变)
public class WeChatTemplate {
/**
* 发送模版消息的openId
*/
private String touser;
/**
* 消息模版id
*/
private String template_id;
/**
* 跳转的链接
*/
private String url;
/**
* 推送的模板内容
*/
private Map<String, TemplateData> data;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, TemplateData> getData() {
return data;
}
public void setData(Map<String, TemplateData> data) {
this.data = data;
}
public WeChatTemplate(){
}
}
2、微信消息推送模版内容参数类
public class TemplateData<T> {
/**
* 参数内容
*/
private T value;
/**
* 参数字体颜色
*/
private String color;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public TemplateData(T value, String color) {
this.value = value;
this.color = color;
}
public TemplateData() {
}
}
3、编写获取AccessToken工具类。
access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存.access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
接口调用请求说明:
https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数说明:
参数 |
是否必须 |
说明 |
grant_type |
是 |
获取access_token填写client_credential |
appid |
是 |
第三方用户唯一凭证 |
secret |
是 |
第三方用户唯一凭证密钥,即appsecret |
public class WaChatServiceUtil {
private final Logger logger = LoggerFactory.getLogger(WaChatServiceUtil.class);
private static AccessToken at;//token获取的次数有限,有效期也有限,所以需要保存起来
private static String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String APPID="appid";
private static final String APPSECRET="appsecret";
public static WaChatServiceUtil getInstance() {
return WaChatServiceUtil.SingletonWaChatServiceUtil.instance;
}
public static String getAPPID() {
return APPID;
}
public static String getAPPSECRET() {
return APPSECRET;
}
private static class SingletonWaChatServiceUtil {
static final WaChatServiceUtil instance = new WaChatServiceUtil();
}
private WaChatServiceUtil() {
}
/**
* 发送get请求获取AccessToken
*/
private static void getToken() {
String url = GET_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
//调用工具类发get请求
String tokenStr = WeChatUtils.sendGet(url);
System.out.println(tokenStr);
JSONObject jsonObject = JSONObject.parseObject(tokenStr);
String token = jsonObject.getString("access_token");
String expiresIn = jsonObject.getString("expires_in");
at = new AccessToken(token, expiresIn);
}
/**
* 获取AccessToken 向外提供
*/
public static String getAccessToken() {
//过期了或者没有值再去发送请求获取
if(at == null || at.isExpire()) {
getToken();
}
return at.getToken();
}
}
4、编写请求工具类,用于发模版消息和获取AccessToken。
public class WeChatUtils {
private static Logger log = Logger.getLogger(WeChatUtils.class);
// 1.httpClient发送get
public static String sendGet(String url) {
String result = "";
CloseableHttpResponse response = null;
try {
// 根据地址获取请求
HttpGet request = new HttpGet(url);// 这里发送get请求
// 获取当前客户端对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 通过请求对象获取响应对象
response = httpClient.execute(request);
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
// 2.httpClient发送post请求 携带json数据格式
public static String sendPost(String url, String jsonStr) {
CloseableHttpResponse httpResponse = null;
String result = "";
try {
// 1.创建httpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2.创建post请求方式实例
HttpPost httpPost = new HttpPost(url);
// 2.1设置请求头 发送的是json数据格式
httpPost.setHeader("Content-type", "application/json;charset=utf-8");
httpPost.setHeader("Connection", "Close");
// 3.设置参数---设置消息实体 也就是携带的数据
StringEntity entity = new StringEntity(jsonStr.toString(), Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8"); // 设置编码格式
// 发送Json格式的数据请求
entity.setContentType("application/json");
// 把请求消息实体塞进去
httpPost.setEntity(entity);
// 4.执行http的post请求
// 4.执行post请求操作,并拿到结果
httpResponse = httpClient.execute(httpPost);
// 获取结果实体
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity, "UTF-8");
} else {
EntityUtils.consume(httpEntity); 如果httpEntity为空,那么直接消化掉即可
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != httpResponse) {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
5、发送模版消息(注意:Map<String,TemplateDate>中put进去的key必须和模版消息中配置的一致)
public static void followMessage(String openId) {
try {
// 发送消息的时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormat = simpleDateFormat.format(new Date());
// 获取AccessToken的值
String accessToken = WaChatServiceUtil.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
// 获取消息模版id
String Message = MessageTemplateEnum.FOLLOW_MESSAGE_TEMPLATE.getKey();
// 设置模版消息的内容
WeChatTemplate wc = new WeChatTemplate();
wc.setTouser(openId);
wc.setTemplate_id(Message);
wc.setUrl(empowerUrl);
Map<String, TemplateData> m = new HashMap<>();
m.put("first", new TemplateData("欢迎关注mamain,点击下方进行授权", "#000000"));
m.put("time", new TemplateData(dateFormat, "#000000"));
m.put("url", new TemplateData("点击进行授权,获取您的微信名。", "#173177"));
m.put("remark", new TemplateData("有疑问请联系客服!", "#FF0000"));
wc.setData(m);
//post发送授权消息模版
String rString = WeChatUtils.sendPost(url, JSON.toJSONString(wc));
logger.info("发送授权模版消息结果为:{}", rString);
} catch (Exception e) {
logger.error("发送授权模版消息失败!", e);
}
}
6、配置消息模版id枚举(可不配置 直接写消息模版id)
public enum MessageTemplateEnum {
FORM_MESSAGE_TEMPLATE("XB06BH9WC4cy20SQXwpCasS3dOYGQBcCtz1FgzUjsm8", "消息模版1"),
FOLLOW_MESSAGE_TEMPLATE("JrvIj-h0DHXj7iobo-3kN1a4Gn-suAWA3rFAFkVE9Mg", "消息模版2");
private String key;
private String value;
MessageTemplateEnum(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getDesc() {
return value;
}
public void setDesc(String value) {
this.value = value;
}
}