概述
微信公众号模版消息
肯定很多人都被微信的开放平台折磨,我也一样无一例外,也是根据公司的业务踩的坑,后来花时间研究了几个小时算是搞明白了。
下边不多说直接上详细说明和demo
首先打开微信开发平台
微信开放平台地址:
https://open.weixin.qq.com/
然后去查看发送模版消息微信提供的接口
调用微信的接口地址:
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
Access_Token 的获取方式 我会在后期的文章中说明 以及获取微信用户的OpenId
数据结构
这里可以查看到微信所提供的接口地址和传参结构,可以发现巨恶心人。。。。
首先先了解数据结构
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}
注意:
逐一去了解下每个字短是干什么的,这个一定要了解,包括传参时候一点要保证一摸一样
结构说明
还有一个比较重要的就是微信放回的结构包体:
{
"errcode":0,
"errmsg":"ok",
"msgid":200228332
}
我会在下边把微信的错误状态码附上
接下来就改上代码了,我是写了一个公用的微信调用方法,可以直接拿走
package com.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author: LIJia
* @date: 2021-09-17 16:28
**/
public class WeChetUtil {
private static Logger logger = Logger.getLogger(WeChetUtil.class);
public JSONObject WeChetTemp(String accessToken,String openId,String tempId,String url,String first,
String keyWord1,
String keyWord2,
String keyWord3,
String keyWord4,
String remark){
JSONObject jsonExternal = new JSONObject();
jsonExternal.put("url",url);
jsonExternal.put("touser",openId);
jsonExternal.put("template_id",tempId);
JSONObject jsonFirst =new JSONObject();
JSONObject keyword1 =new JSONObject();
JSONObject keyword2 =new JSONObject();
JSONObject keyword3 =new JSONObject();
JSONObject keyword4 =new JSONObject();
JSONObject jsonRemark =new JSONObject();
// 构建模版发送包体内容
jsonFirst.put("value",first);
// 这几个极端根据业务的需求做了非空的判定,
keyword1.put("value",keyWord1);
keyword2.put("value",keyWord2);
keyword3.put("value",keyWord3);
keyword4.put("value",keyWord4);
jsonRemark.put("value",remark);
JSONObject jsonData =new JSONObject();
jsonData.put("first",jsonFirst);
jsonData.put("keyword1",keyword1);
jsonData.put("keyword2",keyword2);
jsonData.put("keyword3",keyword3);
jsonData.put("keyword4",keyword4);
jsonData.put("remark",jsonRemark);
jsonExternal.put("data",jsonData);
System.out.println(jsonExternal);
// 调用微信模版消息发送的 url 地址
String urlAccessToken = String.from("https://xxxxx?accountToken",accountToken);
// 发起 Http 请求 可以选择使用 apache 的 httpClient jar 中的公用方法
JSONObject res = HttpClientUtil.postJson(jsonExternal, urlAccessToken);
if(res.getInteger("errcode") == 0){
return ResponseUtil.success();
}else{
logger.info("fail to send msg: " + JSONObject.toJSONString(res));
return ResponseUtil.failMsg("fail to send msg: " + JSONObject.toJSONString(res));
}
}
}
这里边写的比较粗潮特别是数据j结构体但是有另外一种的方式
// JSONObject jsonFirst =new JSONObject();
// JSONObject keyword1 =new JSONObject();
// JSONObject keyword2 =new JSONObject();
// JSONObject keyword3 =new JSONObject();
// JSONObject keyword4 =new JSONObject();
// JSONObject jsonRemark =new JSONObject();
// jsonData.put("first",jsonFirst);
// jsonData.put("keyword1",keyword1);
// jsonData.put("keyword2",keyword2);
// jsonData.put("keyword3",keyword3);
// jsonData.put("keyword4",keyword4);
// jsonData.put("remark",jsonRemark);
String[] tempkey = {"first","keyword1","keyword2","keyword3","keyword4","remark"};
String[] tempValue = {first,keyword1,keyword2,keyword3,keyword4,remark};
这样的话这个公用的类就创建好了
调用这个接口的话可以去我的文章列表找到调用微信公众号模版消息的公用接口
微信接口常见错误状态码:
在这里插入图片描述
微信接口错误返回码地址:
https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html
调用这个接口的话可以去我的文章列表找到调用微信公众号模版消息的公用接口
最后
以上就是魔幻萝莉为你收集整理的Java后端发送微信公众号模版消息自创建公用类的全部内容,希望文章能够帮你解决Java后端发送微信公众号模版消息自创建公用类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复