| @@ -39,37 +39,37 @@ public class WxCpServiceApacheHttpClientImpl extends BaseWxCpServiceImpl<Closeab | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| if (this.configStorage.isAccessTokenExpired() || forceRefresh) { | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| if (this.configStorage.isAccessTokenExpired()) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.httpProxy != null) { | |||||
| RequestConfig config = RequestConfig.custom() | |||||
| .setProxy(this.httpProxy).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| String resultContent = null; | |||||
| try (CloseableHttpClient httpclient = getRequestHttpClient(); | |||||
| CloseableHttpResponse response = httpclient.execute(httpGet)) { | |||||
| resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| } | |||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken( | |||||
| accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | |||||
| if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.configStorage.getAccessToken(); | |||||
| } | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.httpProxy != null) { | |||||
| RequestConfig config = RequestConfig.custom() | |||||
| .setProxy(this.httpProxy).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| String resultContent; | |||||
| try (CloseableHttpClient httpclient = getRequestHttpClient(); | |||||
| CloseableHttpResponse response = httpclient.execute(httpGet)) { | |||||
| resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| } | } | ||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | } | ||||
| } | } | ||||
| return this.configStorage.getAccessToken(); | return this.configStorage.getAccessToken(); | ||||
| @@ -30,30 +30,29 @@ public class WxCpServiceJoddHttpImpl extends BaseWxCpServiceImpl<HttpConnectionP | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| if (this.configStorage.isAccessTokenExpired() || forceRefresh) { | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| if (this.configStorage.isAccessTokenExpired()) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.configStorage.getAccessToken(); | |||||
| } | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| HttpRequest request = HttpRequest.get(url); | |||||
| if (this.httpProxy != null) { | |||||
| httpClient.useProxy(this.httpProxy); | |||||
| } | |||||
| request.withConnectionProvider(httpClient); | |||||
| HttpResponse response = request.send(); | |||||
| HttpRequest request = HttpRequest.get(url); | |||||
| if (this.httpProxy != null) { | |||||
| httpClient.useProxy(this.httpProxy); | |||||
| } | |||||
| request.withConnectionProvider(httpClient); | |||||
| HttpResponse response = request.send(); | |||||
| String resultContent = response.bodyText(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken( | |||||
| accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| } | |||||
| String resultContent = response.bodyText(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| } | } | ||||
| return this.configStorage.getAccessToken(); | return this.configStorage.getAccessToken(); | ||||
| } | } | ||||
| @@ -33,34 +33,33 @@ public class WxCpServiceOkHttpImpl extends BaseWxCpServiceImpl<OkHttpClient, OkH | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| this.log.debug("WxCpServiceOkHttpImpl is running"); | |||||
| if (this.configStorage.isAccessTokenExpired() || forceRefresh) { | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| if (this.configStorage.isAccessTokenExpired()) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| //得到httpClient | |||||
| OkHttpClient client = getRequestHttpClient(); | |||||
| //请求的request | |||||
| Request request = new Request.Builder().url(url).get().build(); | |||||
| String resultContent = null; | |||||
| try { | |||||
| Response response = client.newCall(request).execute(); | |||||
| resultContent = response.body().string(); | |||||
| } catch (IOException e) { | |||||
| this.log.error(e.getMessage(), e); | |||||
| } | |||||
| if (!this.configStorage.isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.configStorage.getAccessToken(); | |||||
| } | |||||
| synchronized (this.globalAccessTokenRefreshLock) { | |||||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?" | |||||
| + "&corpid=" + this.configStorage.getCorpId() | |||||
| + "&corpsecret=" + this.configStorage.getCorpSecret(); | |||||
| //得到httpClient | |||||
| OkHttpClient client = getRequestHttpClient(); | |||||
| //请求的request | |||||
| Request request = new Request.Builder().url(url).get().build(); | |||||
| String resultContent = null; | |||||
| try { | |||||
| Response response = client.newCall(request).execute(); | |||||
| resultContent = response.body().string(); | |||||
| } catch (IOException e) { | |||||
| this.log.error(e.getMessage(), e); | |||||
| } | |||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.CP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.configStorage.updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| } | } | ||||
| return this.configStorage.getAccessToken(); | return this.configStorage.getAccessToken(); | ||||
| } | } | ||||
| @@ -1,31 +1,6 @@ | |||||
| package cn.binarywang.wx.miniapp.api.impl; | package cn.binarywang.wx.miniapp.api.impl; | ||||
| import java.io.IOException; | |||||
| import java.util.HashMap; | |||||
| import java.util.Map; | |||||
| import java.util.concurrent.locks.Lock; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.http.HttpHost; | |||||
| import org.apache.http.client.config.RequestConfig; | |||||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||||
| import org.apache.http.client.methods.HttpGet; | |||||
| import org.apache.http.impl.client.BasicResponseHandler; | |||||
| import org.apache.http.impl.client.CloseableHttpClient; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaAnalysisService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaCodeService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaJsapiService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaMediaService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaMsgService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaRunService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaSecCheckService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaSettingService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaShareService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaTemplateService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaUserService; | |||||
| import cn.binarywang.wx.miniapp.api.*; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | ||||
| import cn.binarywang.wx.miniapp.config.WxMaConfig; | import cn.binarywang.wx.miniapp.config.WxMaConfig; | ||||
| import com.google.common.base.Joiner; | import com.google.common.base.Joiner; | ||||
| @@ -38,13 +13,21 @@ import me.chanjar.weixin.common.error.WxError; | |||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||
| import me.chanjar.weixin.common.util.DataUtils; | import me.chanjar.weixin.common.util.DataUtils; | ||||
| import me.chanjar.weixin.common.util.crypto.SHA1; | import me.chanjar.weixin.common.util.crypto.SHA1; | ||||
| import me.chanjar.weixin.common.util.http.HttpType; | |||||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||||
| import me.chanjar.weixin.common.util.http.RequestHttp; | |||||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||||
| import me.chanjar.weixin.common.util.http.*; | |||||
| import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | ||||
| import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | ||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.apache.http.HttpHost; | |||||
| import org.apache.http.client.config.RequestConfig; | |||||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||||
| import org.apache.http.client.methods.HttpGet; | |||||
| import org.apache.http.impl.client.BasicResponseHandler; | |||||
| import org.apache.http.impl.client.CloseableHttpClient; | |||||
| import java.io.IOException; | |||||
| import java.util.HashMap; | |||||
| import java.util.Map; | |||||
| import java.util.concurrent.locks.Lock; | |||||
| import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*; | import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*; | ||||
| @@ -118,40 +101,41 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| if (!this.getWxMaConfig().isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.getWxMaConfig().getAccessToken(); | |||||
| } | |||||
| Lock lock = this.getWxMaConfig().getAccessTokenLock(); | Lock lock = this.getWxMaConfig().getAccessTokenLock(); | ||||
| lock.lock(); | |||||
| try { | try { | ||||
| lock.lock(); | |||||
| if (this.getWxMaConfig().isAccessTokenExpired() || forceRefresh) { | |||||
| String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), | |||||
| this.getWxMaConfig().getSecret()); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { | |||||
| String resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| WxError error = WxError.fromJson(resultContent); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| String url = String.format(WxMaService.GET_ACCESS_TOKEN_URL, this.getWxMaConfig().getAppid(), | |||||
| this.getWxMaConfig().getSecret()); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { | |||||
| String resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| WxError error = WxError.fromJson(resultContent); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMaConfig().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| return this.getWxMaConfig().getAccessToken(); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| } | } | ||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | } | ||||
| } finally { | } finally { | ||||
| lock.unlock(); | lock.unlock(); | ||||
| } | } | ||||
| return this.getWxMaConfig().getAccessToken(); | |||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -63,37 +63,38 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | |||||
| Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | ||||
| lock.lock(); | |||||
| try { | try { | ||||
| lock.lock(); | |||||
| if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { | |||||
| String resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| try { | |||||
| HttpGet httpGet = new HttpGet(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build(); | |||||
| httpGet.setConfig(config); | |||||
| } | |||||
| try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { | |||||
| String resultContent = new BasicResponseHandler().handleResponse(response); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } finally { | |||||
| httpGet.releaseConnection(); | |||||
| } | } | ||||
| } catch (IOException e) { | |||||
| throw new RuntimeException(e); | |||||
| } | } | ||||
| } finally { | } finally { | ||||
| lock.unlock(); | lock.unlock(); | ||||
| } | } | ||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | } | ||||
| } | } | ||||
| @@ -48,36 +48,37 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | |||||
| Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | ||||
| lock.lock(); | |||||
| try { | try { | ||||
| lock.lock(); | |||||
| if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| HttpRequest request = HttpRequest.get(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); | |||||
| provider.useProxy(getRequestHttpProxy()); | |||||
| request.withConnectionProvider(provider); | |||||
| } | |||||
| HttpResponse response = request.send(); | |||||
| String resultContent = response.bodyText(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| HttpRequest request = HttpRequest.get(url); | |||||
| if (this.getRequestHttpProxy() != null) { | |||||
| SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); | |||||
| provider.useProxy(getRequestHttpProxy()); | |||||
| request.withConnectionProvider(provider); | |||||
| } | |||||
| HttpResponse response = request.send(); | |||||
| String resultContent = response.bodyText(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } finally { | } finally { | ||||
| lock.unlock(); | lock.unlock(); | ||||
| } | } | ||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | } | ||||
| } | } | ||||
| @@ -36,38 +36,36 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | public String getAccessToken(boolean forceRefresh) throws WxErrorException { | ||||
| this.log.debug("WxMpServiceOkHttpImpl is running"); | |||||
| if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) { | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | |||||
| Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); | ||||
| lock.lock(); | |||||
| try { | try { | ||||
| lock.lock(); | |||||
| if (this.getWxMpConfigStorage().isAccessTokenExpired() || forceRefresh) { | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL, | |||||
| this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret()); | |||||
| Request request = new Request.Builder().url(url).get().build(); | |||||
| Response response = getRequestHttpClient().newCall(request).execute(); | |||||
| String resultContent = response.body().string(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | |||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), | |||||
| accessToken.getExpiresIn()); | |||||
| Request request = new Request.Builder().url(url).get().build(); | |||||
| Response response = getRequestHttpClient().newCall(request).execute(); | |||||
| String resultContent = response.body().string(); | |||||
| WxError error = WxError.fromJson(resultContent, WxType.MP); | |||||
| if (error.getErrorCode() != 0) { | |||||
| throw new WxErrorException(error); | |||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||||
| this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| this.log.error(e.getMessage(), e); | |||||
| throw new RuntimeException(e); | |||||
| } finally { | } finally { | ||||
| lock.unlock(); | lock.unlock(); | ||||
| } | } | ||||
| return this.getWxMpConfigStorage().getAccessToken(); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public void initHttp() { | public void initHttp() { | ||||
| this.log.debug("WxMpServiceOkHttpImpl initHttp"); | |||||
| //设置代理 | //设置代理 | ||||
| if (wxMpConfigStorage.getHttpProxyHost() != null && wxMpConfigStorage.getHttpProxyPort() > 0) { | if (wxMpConfigStorage.getHttpProxyHost() != null && wxMpConfigStorage.getHttpProxyPort() > 0) { | ||||
| httpProxy = OkHttpProxyInfo.httpProxy(wxMpConfigStorage.getHttpProxyHost(), | httpProxy = OkHttpProxyInfo.httpProxy(wxMpConfigStorage.getHttpProxyHost(), | ||||