mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 04:29:48 +08:00
🎨 优化部分代码,重构OAuth2网页授权、网页登录等相关接口,方便接入open模块
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@ -30,28 +31,28 @@ import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.GET_JSAPI_TICKET;
|
||||
* Updated by yuanqixun on 2020-05-13
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
|
||||
return getWxCpConfigStorage().getAccessToken();
|
||||
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
|
||||
if (!configStorage.isAccessTokenExpired() && !forceRefresh) {
|
||||
return configStorage.getAccessToken();
|
||||
}
|
||||
Lock lock = getWxCpConfigStorage().getAccessTokenLock();
|
||||
Lock lock = configStorage.getAccessTokenLock();
|
||||
lock.lock();
|
||||
try {
|
||||
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
|
||||
if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
|
||||
return getWxCpConfigStorage().getAccessToken();
|
||||
if (!configStorage.isAccessTokenExpired() && !forceRefresh) {
|
||||
return configStorage.getAccessToken();
|
||||
}
|
||||
String url = String.format(getWxCpConfigStorage().getApiUrl(WxCpApiPathConsts.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
|
||||
String url = String.format(configStorage.getApiUrl(WxCpApiPathConsts.GET_TOKEN),
|
||||
this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
if (getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom()
|
||||
.setProxy(getRequestHttpProxy()).build();
|
||||
RequestConfig config = RequestConfig.custom().setProxy(getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
}
|
||||
String resultContent;
|
||||
@ -67,60 +68,62 @@ public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
|
||||
}
|
||||
|
||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||
getWxCpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
} catch (IOException e) {
|
||||
throw new WxRuntimeException(e);
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return getWxCpConfigStorage().getAccessToken();
|
||||
return configStorage.getAccessToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException {
|
||||
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
|
||||
if (forceRefresh) {
|
||||
getWxCpConfigStorage().expireAgentJsapiTicket();
|
||||
configStorage.expireAgentJsapiTicket();
|
||||
}
|
||||
if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) {
|
||||
Lock lock = getWxCpConfigStorage().getAgentJsapiTicketLock();
|
||||
if (configStorage.isAgentJsapiTicketExpired()) {
|
||||
Lock lock = configStorage.getAgentJsapiTicketLock();
|
||||
lock.lock();
|
||||
try {
|
||||
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
|
||||
if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) {
|
||||
String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_AGENT_CONFIG_TICKET), null);
|
||||
if (configStorage.isAgentJsapiTicketExpired()) {
|
||||
String responseContent = this.get(configStorage.getApiUrl(GET_AGENT_CONFIG_TICKET), null);
|
||||
JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
getWxCpConfigStorage().updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
|
||||
configStorage.updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
|
||||
jsonObject.get("expires_in").getAsInt());
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return getWxCpConfigStorage().getAgentJsapiTicket();
|
||||
return configStorage.getAgentJsapiTicket();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
|
||||
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
|
||||
if (forceRefresh) {
|
||||
getWxCpConfigStorage().expireJsapiTicket();
|
||||
configStorage.expireJsapiTicket();
|
||||
}
|
||||
|
||||
if (getWxCpConfigStorage().isJsapiTicketExpired()) {
|
||||
Lock lock = getWxCpConfigStorage().getJsapiTicketLock();
|
||||
if (configStorage.isJsapiTicketExpired()) {
|
||||
Lock lock = configStorage.getJsapiTicketLock();
|
||||
lock.lock();
|
||||
try {
|
||||
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
|
||||
if (getWxCpConfigStorage().isJsapiTicketExpired()) {
|
||||
String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_JSAPI_TICKET), null);
|
||||
if (configStorage.isJsapiTicketExpired()) {
|
||||
String responseContent = this.get(configStorage.getApiUrl(GET_JSAPI_TICKET), null);
|
||||
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
|
||||
getWxCpConfigStorage().updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
|
||||
configStorage.updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
|
||||
tmpJsonObject.get("expires_in").getAsInt());
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
return getWxCpConfigStorage().getJsapiTicket();
|
||||
return configStorage.getJsapiTicket();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user