前言:微信公众平台为了识别用户,每个用户针对每个公众号会产生一个安全的OpenID,一个用户虽然对多个公众号和应用有多个不同的OpenID,但他对所有这些同一开放平台账号下的公众号和应用,只有一个UnionID。
微信发送模板消息接口调用请求说明
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
POST数据示例如下:
{
"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"
}
}
}
参数说明:
有关access_token介绍一下,access_token为接口调用凭据,来调用接口,所有接口的调用需要先获取access_token,access_token在2小时内有效,过期需要重新获取
摘自微信公众号:在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID。开发者可通过OpenID来获取用户基本信息。特别需要注意的是,如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号,用户的unionid是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,unionid是相同的。
代码案例:
微信用户存放openID相关表
CREATE TABLE `register` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`openid` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`tel` varchar(11) COLLATE utf8_bin DEFAULT NULL,
`createtime` varchar(20) COLLATE utf8_bin DEFAULT NULL COMMENT '创建时间',
`defaultno` varchar(15) COLLATE utf8_bin DEFAULT NULL,
`subtel` varchar(11) COLLATE utf8_bin DEFAULT NULL,
`type` varchar(5) COLLATE utf8_bin DEFAULT NULL,
`unionid` varchar(100) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12767 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
请求参数封装
Map<String, Object> params = new HashMap<String, Object>();
params.put("touser", register.getOpenid());
params.put("template_id", "QeZkNDh96uM4********VXhidaXfSiDPP_pE");
Map<String, Object> miniprogram = new HashMap<String, Object>();
miniprogram.put("appid", "wx0e*****8bc2a16384");
//丈量单详情页
String measureURL = URLUTF8Encoder.encode("/pages/***/zlDetail?measureId=" + vo.getId());
miniprogram.put("pagepath", "pages/****/decoration?unionid=" + unionid +
"&clientno=" + client.getClientno() + "&redirectUrl=" + measureURL);
params.put("miniprogram", miniprogram);
Map<String, Object> tempdata = new HashMap<>();
Map<String, Object> first = new HashMap<>();
first.put("value", "您有一份丈量单待审批");
tempdata.put("first", first);
Map<String, Object> keyword1 = new HashMap<>();
keyword1.put("value", client.getAddr());
tempdata.put("keyword1", keyword1);
Map<String, Object> keyword2 = new HashMap<>();
keyword2.put("value", client.getClientname());
tempdata.put("keyword2", keyword2);
Map<String, Object> keyword3 = new HashMap<>();
keyword3.put("value", "无");
tempdata.put("keyword3", keyword3);
Map<String, Object> keyword4 = new HashMap<>();
keyword4.put("value", "无");
tempdata.put("keyword4", keyword4);
Map<String, Object> keyword5 = new HashMap<>();
keyword5.put("value", "请及时查看");
tempdata.put("keyword5", keyword5);
params.put("data", tempdata);
//发送模板消息
FWWXUtil.sendTemplateMessage(params);
private static final String apiUrl = "https://api.weixin.qq.com/cgi-bin";
/**
* 发送服务号模板消息
*
* @param parameters
*/
public static void sendTemplateMessage(Map<String, Object> parameters) {
sendTemplateMessage(parameters, "UTF-8");
}
/**
* 发送服务号模板消息
*
* @param parameters
* @param recvEncoding
*/
public static void sendTemplateMessage(Map<String, Object> parameters, String recvEncoding) {
String token = getAccessToken();
if (token != null) {
post(apiUrl + "/message/template/send?access_token=" + token, parameters, recvEncoding);
}
}
post请求方法
private static String post(String reqUrl, Map<String, Object> parameters, String recvEncoding) {
Gson gson = new Gson();
String params = gson.toJson(parameters);//json.toString();// JsonRpcUtil.ObjToJson(parameters);
log.debug("请求参数:" + params);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(reqUrl);
String response = null;
try {
StringEntity s = new StringEntity(params, recvEncoding);
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
String charset = recvEncoding;// EntityUtils.getContentCharSet(entity);
response = EntityUtils.toString(entity);
}
log.info("{}", response);
return response;
} catch (Exception e) {
log.error("", e);
}
return null;
}
用户成功在微信收到消息