Skip to content

Commit 088a972

Browse files
committed
为WxMpConfigStorage接口增加autoRefreshToken方法,以方便客户端设置是否自动刷新token. for #77
1 parent a37a869 commit 088a972

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ public interface WxMpConfigStorage {
100100
* @return ApacheHttpClientBuilder
101101
*/
102102
ApacheHttpClientBuilder getApacheHttpClientBuilder();
103+
104+
/**
105+
* 是否自动刷新token
106+
*/
107+
boolean autoRefreshToken();
103108
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
270270
return this.apacheHttpClientBuilder;
271271
}
272272

273+
@Override
274+
public boolean autoRefreshToken() {
275+
return true;
276+
}
277+
273278
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
274279
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
275280
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
101101
if (forceRefresh) {
102102
this.configStorage.expireAccessToken();
103103
}
104+
104105
if (this.configStorage.isAccessTokenExpired()) {
105106
synchronized (this.globalAccessTokenRefreshLock) {
106107
if (this.configStorage.isAccessTokenExpired()) {
@@ -411,8 +412,11 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
411412
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
412413
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
413414
this.configStorage.expireAccessToken();
414-
return this.execute(executor, uri, data);
415+
if(this.configStorage.autoRefreshToken()){
416+
return this.execute(executor, uri, data);
417+
}
415418
}
419+
416420
if (error.getErrorCode() != 0) {
417421
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data,
418422
error);

0 commit comments

Comments
 (0)