和之前微信扫码支付和JSAPI支付一样,我将模板消息推送整合到了守望者这个项目
首先要开通要登录微信公众号开启模板消息然后选择模板类型获得模板id即可,接下来看代码把
这个是推送主要代码
package com.watchmen.wechat.news;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.collect.Maps;
import com.watchmen.wechat.config.WeChatConfig;
import com.watchmen.wechat.domain.WechatTemplateMsg;
import com.watchmen.wechat.token.WechatVoucher;
import com.watchmen.wechat.util.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author kk
* 模板消息推送
*/
@Slf4j
@Component
public class TemplateMessage {
@Autowired
WeChatConfig weChatConfig;
@Autowired
WechatVoucher wechatVoucher;
@Autowired
HttpRequest httpRequest;
/**
* 用于群发消息
* @param params 根据具体模板参数组装
* @param openIdList 微信Openid
* @param url 跳转的url
* @return
* @throws Exception
*/
public String sendOut(TreeMap<String,TreeMap<String,String>> params,
List<String> openIdList,
String url) throws Exception {
String industry = "";
// 获取openid
for (int i = 0; i < openIdList.size(); i++) {
WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
String openId = openIdList.get(i);
wechatTemplateMsg.setTouser(openId);
wechatTemplateMsg.setUrl(url);
wechatTemplateMsg.setTemplate_id(weChatConfig.getTemplateId());
wechatTemplateMsg.setData(params);
// 推送消息
industry = this.getIndustry(wechatTemplateMsg);
}
return industry;
}
/**
* @param msg 消息内容
* @return
* @throws Exception
*/
public String getIndustry(WechatTemplateMsg msg) throws Exception{
log.info("推送消息{}",msg);
String token = "";
try {
token = wechatVoucher.caches.get("access_token");
// 获取access_token,没有则重新请求获取
token =WechatVoucher.caches.get("access_token");
} catch (Exception e) {
try {
// 重新请求获取
wechatVoucher.voucher();
token =wechatVoucher.caches.get("access_token");
} catch (Exception e2) {
e2.printStackTrace();
}
}
log.info("token:"+token);
// 推送消息
String industryUrl = weChatConfig.getIndustryUrl().replace("TOKEN", token);
String objJsonObject = JSONUtil.toJsonStr(msg);
log.info("构造的json:{}",objJsonObject);
String industry = httpRequest.doHttpsJson_HttpClient(industryUrl, "POST", objJsonObject);
JSONObject parseObj = JSONUtil.parseObj(industry);
String errmsg = parseObj.getStr("errmsg");
if(errmsg.contains("ok")){
log.info("微信返回........................"+errmsg);
log.info("发送成功........................");
}
System.out.println(industry);
return industry;
}
}
推送要得到access_token所以下面代码时获取access_token的
package com.watchmen.wechat.token;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import com.watchmen.wechat.config.WeChatConfig;
import com.watchmen.wechat.util.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author kk
* 微信JS SDK, access_token, ticket
*/
@Slf4j
@Component
public class WechatVoucher {
@Autowired
WeChatConfig weChatConfig;
@Autowired
HttpRequest httpRequest;
// 本地缓存
public static LoadingCache<String, String> caches = CacheBuilder
.newBuilder()
// 设置过期时间
.expireAfterWrite(7180000, TimeUnit.SECONDS)
.build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
return null;
}
});
public Map<String, String> voucher(){
Map<String, String> dataMap = Maps.newLinkedHashMap();
// 缓存access_token
String cacheAccess = "";
// 缓存jsapi_ticket
String cacheTicket = "";
String accessTokenUrl = weChatConfig.getAccessTokenUrl().replace("APPID", weChatConfig.getAppID())
.replace("APPSECRET", weChatConfig.getAppSecret());
log.info("accessTokenUrl是:{}",accessTokenUrl);
// 获取access_token,并放入缓存
try {
// 有则赋值,没有则调用微信接口获取access_token在赋值
cacheAccess = caches.get("access_token");
} catch (Exception e) {
try {
// 发送请求,获取access_token
String accessData = httpRequest.doHttpsJson_HttpClient(accessTokenUrl,"POST",null);
JSONObject parseObj = JSONUtil.parseObj(accessData);
String accessToken = parseObj.getStr("access_token");
// 放入缓存
caches.put("access_token", accessToken);
cacheAccess = caches.get("access_token");
} catch (Exception e2) {
e2.printStackTrace();
}
}
log.info("cache_access值是:{}",cacheAccess);
//拿到了access_token 使用access_token 获取到jsapi_ticket
String ticketUrl = weChatConfig.getTicketUrl().replace("ACCESS", cacheAccess);
// 获取jsapi_ticket,并放入缓存
try {
// 有则赋值,没有则调用微信接口获取access_token在赋值
cacheTicket = caches.get("jsapi_ticket");
} catch (Exception e) {
try {
//调用jsapi_ticket_url 请求拿到jsapi_ticket
String ticketData = httpRequest.doHttpsJson_HttpClient(ticketUrl,"POST",null);
JSONObject parseObj = JSONUtil.parseObj(ticketData);
String jsapiTicket = parseObj.getStr("ticket");
// 放入缓存
caches.put("jsapi_ticket", jsapiTicket);
cacheTicket = caches.get("jsapi_ticket");
} catch (Exception e2) {
e2.printStackTrace();
}
}
log.info("cache_ticket{值是:{}",cacheTicket);
// 返回结果
dataMap.put("ticket", cacheTicket);
dataMap.put("accessTtoken", cacheAccess);
return dataMap;
}
}
完整代码进入码云看吧https://gitee.com/yankangkk/watchmen