微信公众平台——模板消息接口
2017-01-04 13:44
477 查看
关于接口文档,请注意:
1.service
/**
* 微信自动消息推送
* @author Administrator
*
*/
@Service
public class AutomaticMessagePushServiceImpl implements AutomaticMessageServicePush {
@Autowired
private MyCache cache;
@Override
public String AutomaticMessagePush(JSONObject JsonCustom,String first,String remark,String openId,String templateId,String customUrl,Integer identification) {
JSONObject Json = new JSONObject();
JSONObject Jsonfirst = new JSONObject();
JSONObject Jsonremark = new JSONObject();
//头部
Jsonfirst.put("value", first);
Jsonfirst.put("color", "#000000");
//尾部
Jsonremark.put("value", remark);
Jsonremark.put("color", "#000000");
//自定义内容增加头和尾
JsonCustom.put("first", Jsonfirst);
JsonCustom.put("remark", Jsonremark);
//微信openId
Json.put("touser", openId);
//模板id
Json.put("template_id", templateId);
//详情跳转地址
Json.put("url", customUrl);
//json数据
Json.put("data", JsonCustom);
//根据标识发送对应公众号
String appid;
String appSecret;
String keyName ="";
if(identification==Constants.TALENT_IDENTIFICATION){
keyName = "wechatAccessToken";
appid = ConfigHelper.getInstance("config").getValue("DaRenAppID");
appSecret = ConfigHelper.getInstance("config").getValue("DaRenAppSecret");
}else{
keyName = "wechat_AccessToken";
appid = ConfigHelper.getInstance("config").getValue("WalletAppID");
appSecret = ConfigHelper.getInstance("config").getValue("WalletAppSecret");
}
String url_Template_GetAccessToken ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
String url_Template_GetAccessTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
String accessToken = cache.getString(keyName);
if(accessToken == null){
//获取token
String url_GetAccessToken = String.format(url_Template_GetAccessToken, appid,appSecret);
JSONObject accessTokenMap = sendGetRequest(url_GetAccessToken);
accessToken = accessTokenMap.getString("access_token");
cache.setString(keyName, 6000, accessToken);
}
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
String result = HttpRequestSimple.getInstance().postSendHttp(url, Json.toString());
return result;
}
public static JSONObject sendGetRequest(String url){
HttpClient httpClient = CustomHttpClient.GetHttpClient();
HttpGet get = new HttpGet(url);
get.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
BufferedReader br = null;
try {
// 发送请求,接收响应
HttpResponse resp = httpClient.execute(get);
int ret = resp.getStatusLine().getStatusCode();
if(ret == HttpStatus.SC_OK){
// 响应分析
HttpEntity entity = resp.getEntity();
br = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
StringBuffer responseString = new StringBuffer();
String str = br.readLine();
while (str != null) {
responseString.append(str);
str = br.readLine();
}
return JSON.parseObject(responseString.toString());
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// do nothing
}
}
}
return new JSONObject();
}
}
2.controller调用
/**
* 绑定消息推送公共方法
* @param openId
*/
private void bindingPush(String openId) {
//发送自动消息推送
com.alibaba.fastjson.JSONObject Json = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject name1 = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject name2 = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject time = new com.alibaba.fastjson.JSONObject();
name1.put("value", "通金达人");
name1.put("color", "#000000");
name2.put("value", "微信");
name2.put("color", "#000000");
//绑定时间
time.put("value", DateUtil.dateToString1(new Date()));
time.put("color", "#000000");
Json.put("name1", name1);
Json.put("name2", name2);
Json.put("time", time);
//消息头部
String first = "用户您好,恭喜您账户绑定成功!";
//消息尾部
String remark = "您可以使用下方微信菜单进行更多功能体验,如需解除绑定请回复'qxbd'";
//详情跳转页面
String customUrl = ConfigHelper.getInstance("config").getValue("DaRenPushUrl");
//根据json内容微信自动消息推送
servicePush.AutomaticMessagePush(Json,first,remark,openId,Constants.TALENT_BINDING_SUCCESS,customUrl,Constants.TALENT_IDENTIFICATION);
}
1、模板消息调用时主要需要模板ID和模板中各参数的赋值内容; 2、模板中参数内容必须以".DATA"结尾,否则视为保留字; 3、模板保留符号"{{ }}"。
1.service
/**
* 微信自动消息推送
* @author Administrator
*
*/
@Service
public class AutomaticMessagePushServiceImpl implements AutomaticMessageServicePush {
@Autowired
private MyCache cache;
@Override
public String AutomaticMessagePush(JSONObject JsonCustom,String first,String remark,String openId,String templateId,String customUrl,Integer identification) {
JSONObject Json = new JSONObject();
JSONObject Jsonfirst = new JSONObject();
JSONObject Jsonremark = new JSONObject();
//头部
Jsonfirst.put("value", first);
Jsonfirst.put("color", "#000000");
//尾部
Jsonremark.put("value", remark);
Jsonremark.put("color", "#000000");
//自定义内容增加头和尾
JsonCustom.put("first", Jsonfirst);
JsonCustom.put("remark", Jsonremark);
//微信openId
Json.put("touser", openId);
//模板id
Json.put("template_id", templateId);
//详情跳转地址
Json.put("url", customUrl);
//json数据
Json.put("data", JsonCustom);
//根据标识发送对应公众号
String appid;
String appSecret;
String keyName ="";
if(identification==Constants.TALENT_IDENTIFICATION){
keyName = "wechatAccessToken";
appid = ConfigHelper.getInstance("config").getValue("DaRenAppID");
appSecret = ConfigHelper.getInstance("config").getValue("DaRenAppSecret");
}else{
keyName = "wechat_AccessToken";
appid = ConfigHelper.getInstance("config").getValue("WalletAppID");
appSecret = ConfigHelper.getInstance("config").getValue("WalletAppSecret");
}
String url_Template_GetAccessToken ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
String url_Template_GetAccessTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
String accessToken = cache.getString(keyName);
if(accessToken == null){
//获取token
String url_GetAccessToken = String.format(url_Template_GetAccessToken, appid,appSecret);
JSONObject accessTokenMap = sendGetRequest(url_GetAccessToken);
accessToken = accessTokenMap.getString("access_token");
cache.setString(keyName, 6000, accessToken);
}
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
String result = HttpRequestSimple.getInstance().postSendHttp(url, Json.toString());
return result;
}
public static JSONObject sendGetRequest(String url){
HttpClient httpClient = CustomHttpClient.GetHttpClient();
HttpGet get = new HttpGet(url);
get.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
BufferedReader br = null;
try {
// 发送请求,接收响应
HttpResponse resp = httpClient.execute(get);
int ret = resp.getStatusLine().getStatusCode();
if(ret == HttpStatus.SC_OK){
// 响应分析
HttpEntity entity = resp.getEntity();
br = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
StringBuffer responseString = new StringBuffer();
String str = br.readLine();
while (str != null) {
responseString.append(str);
str = br.readLine();
}
return JSON.parseObject(responseString.toString());
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// do nothing
}
}
}
return new JSONObject();
}
}
2.controller调用
/**
* 绑定消息推送公共方法
* @param openId
*/
private void bindingPush(String openId) {
//发送自动消息推送
com.alibaba.fastjson.JSONObject Json = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject name1 = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject name2 = new com.alibaba.fastjson.JSONObject();
com.alibaba.fastjson.JSONObject time = new com.alibaba.fastjson.JSONObject();
name1.put("value", "通金达人");
name1.put("color", "#000000");
name2.put("value", "微信");
name2.put("color", "#000000");
//绑定时间
time.put("value", DateUtil.dateToString1(new Date()));
time.put("color", "#000000");
Json.put("name1", name1);
Json.put("name2", name2);
Json.put("time", time);
//消息头部
String first = "用户您好,恭喜您账户绑定成功!";
//消息尾部
String remark = "您可以使用下方微信菜单进行更多功能体验,如需解除绑定请回复'qxbd'";
//详情跳转页面
String customUrl = ConfigHelper.getInstance("config").getValue("DaRenPushUrl");
//根据json内容微信自动消息推送
servicePush.AutomaticMessagePush(Json,first,remark,openId,Constants.TALENT_BINDING_SUCCESS,customUrl,Constants.TALENT_IDENTIFICATION);
}
相关文章推荐
- 微信公众平台模板消息发送接口文档
- 微信公众平台模板消息发送接口文档
- 微信公众平台模板消息发送接口文档
- 微信公众平台消息接口校验与消息接口响应实例
- 微信公众平台消息接口开发(21)图片识别之亲子鉴定
- 微信公众平台消息接口PHP版开发教程
- 微信公众平台开发之模板消息(Java)
- 微信公众平台消息接口开发(1)启用接口
- 微信公众平台消息接口开发(9)骑行西藏自定义菜单
- 微信公众平台消息接口开发(23)图片识别之男人味/女人味指数
- 微信公众平台开发模式消息接口开发 从Hello2BizUser文本到subscribe事件
- 微信公众平台消息接口开发(30)大众点评商户信息团购及优惠券查询
- 微信公众平台开发(102) 模板消息
- 微信公众平台申请消息接口--struts2(jsp)实现
- 微信公众平台发送模板消息
- 微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
- 微信公众平台开发-消息管理-发送模板消息
- 微信公众平台消息接口PHP版开发教程
- 微信公众平台消息接口开发(4)天气预报
- 微信公众平台消息接口开发(5)股票查询