微信模板消息发送采坑
1.申请公众号
2.新增模板(前提:认证通过)
appid和appsecret找到,准备好
3..第三方应用(网站,管理后台系统,h5)【必须要有域名,该域名还必须配置在白名单中】用户数据和微信用户绑定,建立一对一关系,
说白了就是在自己的数据库用户表加字段,来保存一个openid
具体:公众号后台配置自定义菜单
跳转网页这里写:
https://open.weixin.qq.com/connect/oauth2/authorize?
appid=xxxxxxxxx
&redirect_uri=xxxxxxx(这里写域名下的路径(也就是要去绑定用户信息的h5页面)[记得用encode转码])
&response_type=code
&scope=snsapi_base
&state=123#wechat_redirect
4.程序开发,也就是上面说的域名下的路径(其实是一个请求,跳转h5页面)
用户一点击绑定菜单,会调用3中的redirct_uri请求,跳转到h5,【此时步骤3中的就已经返回了code,在代码里直接获取】
用户在h5提交表单后,进入新的接口(处理一些逻辑(获取到openid把第一步中的字段跟新上))
public static JSONObject getAccessTokenAndOpenidByCode(String code) {
String accessToken = "";
String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code";
JSONObject jsonObject = httpRequest(ACCESS_TOKEN_URL, "GET", null);
// 如果请求成功
if (jsonObject != null) {
return jsonObject;
}
return null;
}
返回的json里面获取openid
5.下面就准备发送模板消息了
public static JSONObject sendMessageTemplate(String sendName, String sendMobile, String wxid) {
TemplateData first = new TemplateData();
first.setColor("#000000");
first.setValue("配送成功!");
// TemplateData name = new TemplateData();
// name.setColor("#0d29d0");
// name.setValue("配送员:"+sendName+" 电话:"+sendMobile);
// TemplateData time = new TemplateData();
// time.setColor("#0d29d0");
// time.setValue("配送时间:"+getDateString(new Date()));
TemplateData code = new TemplateData();
code.setColor("#000000");
code.setValue("配送员:" + sendName + " \n" +
"电话:" + sendMobile + " \n" +
"送达时间:" + getDateString(new Date()) + " \n" +
"如有疑问,请致电配送员");
Map<String, TemplateData> templateData = new HashMap<String, TemplateData>();
// templateData.put("keyword2", time);
// templateData.put("keyword1", name);
templateData.put("first", first);//这个是模板关键字,三层结构first,中间data,最后remark
templateData.put("remark", code);
WxTemplate wxTemplate = new WxTemplate();
wxTemplate.setUrl("");
wxTemplate.setTouser(wxid);//消息接受者
wxTemplate.setTopcolor("#000000");
wxTemplate.setTemplate_id(WEIXIN_TEMPLATE_CODE_USER);//消息模板id,步骤1截图中的模板id
wxTemplate.setData(templateData);
String sendMessage = sendMessage(wxTemplate);
JSONObject json = JSONObject.parseObject(sendMessage);
return json;
}
public static String sendMessage(WxTemplate wxTemplate) {
Object accessToken = getAccessToken();
System.out.println("获取到的token:" + accessToken);
String reqURL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
String postJsonRequest = "";
try {
postJsonRequest = postData(reqURL, JSON.toJSONString(wxTemplate));
System.out.println("结果返回:" + postJsonRequest);
} catch (Exception e) {
e.printStackTrace();
}
return postJsonRequest;
}
public static String getAccessToken() {
String accessToken = "";
String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
JSONObject jsonObject = httpRequest(ACCESS_TOKEN_URL, "GET", null);
// 如果请求成功
if (jsonObject != null) {
accessToken = (String) jsonObject.get("access_token");
}
return String.valueOf(accessToken);
}
如果要在本地调试,则把本地的ip也新增到白名单,否则给提示不在白名单