|
@@ -1,5 +1,6 @@
|
|
package com.yami.shop.mp.component;
|
|
package com.yami.shop.mp.component;
|
|
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
import com.yami.shop.common.exception.YamiShopBindException;
|
|
import com.yami.shop.common.exception.YamiShopBindException;
|
|
import me.chanjar.weixin.common.WxType;
|
|
import me.chanjar.weixin.common.WxType;
|
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
|
@@ -7,14 +8,9 @@ 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.mp.api.WxMpService;
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
|
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
|
-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.redisson.api.RLock;
|
|
import org.redisson.api.RLock;
|
|
import org.redisson.api.RedissonClient;
|
|
import org.redisson.api.RedissonClient;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -40,7 +36,6 @@ public class WxMpServiceClusterImpl extends WxMpServiceImpl {
|
|
|
|
|
|
RLock rLock = redissonClient.getLock(REDISSON_LOCK_PREFIX + ":WxMpServiceCluster:getAccessToken");
|
|
RLock rLock = redissonClient.getLock(REDISSON_LOCK_PREFIX + ":WxMpServiceCluster:getAccessToken");
|
|
|
|
|
|
- Object result = null;
|
|
|
|
try {
|
|
try {
|
|
boolean doingUpdateAccessToken;
|
|
boolean doingUpdateAccessToken;
|
|
try {
|
|
try {
|
|
@@ -56,29 +51,18 @@ public class WxMpServiceClusterImpl extends WxMpServiceImpl {
|
|
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
|
|
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
|
|
return this.getWxMpConfigStorage().getAccessToken();
|
|
return this.getWxMpConfigStorage().getAccessToken();
|
|
}
|
|
}
|
|
|
|
+
|
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
|
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
|
|
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
|
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());
|
|
|
|
- return this.getWxMpConfigStorage().getAccessToken();
|
|
|
|
- } finally {
|
|
|
|
- httpGet.releaseConnection();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
|
+ String resultContent = HttpUtil.get(url);
|
|
|
|
+
|
|
|
|
+ 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 {
|
|
rLock.unlock();
|
|
rLock.unlock();
|