Commit de585692 authored by shiyu's avatar shiyu

小程序订阅消息,获取token

parent 8ea975a9
......@@ -4,7 +4,9 @@ import com.alibaba.fastjson.JSONArray;
import com.wwdz.ch.core.entity.WxCheckMediaRequestDto;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.OkHttpUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -14,6 +16,7 @@ import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class WxAppletApi {
......@@ -26,6 +29,15 @@ public class WxAppletApi {
@Value("${dts.wx.app-secret}")
private String APPLET_APP_SECRET;
@Autowired
RedisUtils redisUtils;
/**
* 测试环境和正式环境的url不同,相互隔离不会冲突
*/
@Value("${dts.wx.get-officialaccount-accesstoken-url}")
private String GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL;
@Value("${dts.wx.auction-msg-url}")
private String AUCTION_MSG_URL;
......@@ -52,8 +64,6 @@ public class WxAppletApi {
}
public String checkMedia(WxCheckMediaRequestDto dto) {
String accessToken = wxLoginManager.getAccessToken();
String url = String.format(MEDIA_CHECK_URL, accessToken);
......@@ -144,5 +154,59 @@ public class WxAppletApi {
return Result.failed();
}
public String getAppletAccessToken() {
//测试环境
if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
String accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, APPLET_APPID, APPLET_APP_SECRET);
String res = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
logger.info("测试环境获取小程序token返回结果: {}", res);
String token = JsonUtils.getValueByPath(res, "access_token");
return token;
}
//正式环境先从缓存中获取,没有再刷新token
//根据商户的appid从redis中获取商户的token
String token = redisUtils.hasKey(APPLET_APPID)? redisUtils.get(APPLET_APP_SECRET).toString() : null;
if (StringUtils.isEmpty(token)) {
token = refreshAppletAccessToken();
}
return token;
}
public String refreshAppletAccessToken() {
try {
String accessTokenUrl = null;
String responseStr = null;
//正式环境的url中包含了"stable_token", 要使用post请求, 测试环境是get请求
if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, APPLET_APPID, APPLET_APP_SECRET);
logger.info("测试环境获取小程序access_token url :{}", accessTokenUrl);
responseStr = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
} else {
accessTokenUrl = GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL;
logger.info("正式环境获取小程序access_token url :{}", accessTokenUrl);
Map<String, Object> map = new HashMap<>();
map.put("grant_type", "client_credential");
map.put("appid", APPLET_APPID);
map.put("secret", APPLET_APP_SECRET);
responseStr = OkHttpUtil.builder().addParams(map).url(accessTokenUrl)
.post(true).sync();
}
logger.info("小程序刷新access_token response :{}", responseStr);
if (!responseStr.contains("access_token")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
logger.error("appid : {}, 小程序获取token出错, errorCode : {}, errmsg : {} ", APPLET_APPID, errorCode, errmsg);
return null;
}
String token = JsonUtils.getValueByPath(responseStr, "access_token");
String expires_in = JsonUtils.getValueByPath(responseStr, "expires_in");
redisUtils.set(APPLET_APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS);
return token;
} catch (Exception e) {
logger.error("获取小程序access_token失败", e);
return null;
}
}
}
package com.wwdz.ch.core.impl;
import com.wwdz.ch.core.api.OfficialAccountApi;
import com.wwdz.ch.core.api.WxAppletApi;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.entity.*;
import com.wwdz.ch.core.type.Result;
......@@ -61,6 +62,9 @@ public class SendMsgServiceImpl implements SendMsgService {
@Autowired
UserDao userDao;
@Autowired
WxAppletApi wxAppletApi;
@Autowired
OfficialAccountSubscribeRecordDao officialAccountSubscribeRecordDao;
......@@ -614,7 +618,7 @@ public class SendMsgServiceImpl implements SendMsgService {
*/
public Result sendSpecialPerformanceStartMsg(AuctionOfferNoticeMsg auctionOfferNoticeMsg) {
String openId = auctionOfferNoticeMsg.getOpenId();
String token = officialAccountApi.getAccessToken();
String token = wxAppletApi.getAppletAccessToken();
String url = SEND_SUBSCRIBE_MSG_URL + "?access_token=" + token;
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
......@@ -657,7 +661,7 @@ public class SendMsgServiceImpl implements SendMsgService {
*/
public Result sendSpecialPerformanceEndMsg(AuctionOfferNoticeMsg auctionOfferNoticeMsg) {
String openId = auctionOfferNoticeMsg.getOpenId();
String token = officialAccountApi.getAccessToken();
String token = wxAppletApi.getAppletAccessToken();
String url = SEND_SUBSCRIBE_MSG_URL + "?access_token=" + token;
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment