|
1 | 1 | package me.chanjar.weixin.cp.api.impl; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.IOException; |
5 | | -import java.io.InputStream; |
6 | | -import java.util.List; |
7 | | -import java.util.UUID; |
8 | | - |
9 | | -import org.apache.commons.lang3.StringUtils; |
10 | | -import org.slf4j.Logger; |
11 | | -import org.slf4j.LoggerFactory; |
12 | | - |
13 | 3 | import com.google.gson.*; |
14 | 4 | import com.google.gson.reflect.TypeToken; |
15 | | - |
16 | 5 | import me.chanjar.weixin.common.bean.WxJsapiSignature; |
17 | 6 | import me.chanjar.weixin.common.bean.menu.WxMenu; |
18 | 7 | import me.chanjar.weixin.common.bean.result.WxError; |
|
33 | 22 | import me.chanjar.weixin.cp.bean.WxCpTag; |
34 | 23 | import me.chanjar.weixin.cp.bean.WxCpUser; |
35 | 24 | import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.util.List; |
| 33 | +import java.util.UUID; |
36 | 34 |
|
37 | | -public abstract class AbstractWxCpService<H, P> implements WxCpService, RequestHttp<H, P> { |
| 35 | +public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, RequestHttp<H, P> { |
38 | 36 |
|
39 | | - protected final Logger log = LoggerFactory.getLogger(AbstractWxCpService.class); |
| 37 | + protected final Logger log = LoggerFactory.getLogger(AbstractWxCpServiceImpl.class); |
40 | 38 |
|
41 | 39 | /** |
42 | 40 | * 全局的是否正在刷新access token的锁 |
@@ -493,9 +491,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro |
493 | 491 | int retryTimes = 0; |
494 | 492 | do { |
495 | 493 | try { |
496 | | - T result = this.executeInternal(executor, uri, data); |
497 | | - this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",uri, data, result); |
498 | | - return result; |
| 494 | + return this.executeInternal(executor, uri, data); |
499 | 495 | } catch (WxErrorException e) { |
500 | 496 | if (retryTimes + 1 > this.maxRetryTimes) { |
501 | 497 | this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes); |
@@ -525,37 +521,39 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro |
525 | 521 | throw new RuntimeException("微信服务端异常,超出重试次数"); |
526 | 522 | } |
527 | 523 |
|
528 | | - public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { |
| 524 | + private synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { |
529 | 525 | if (uri.contains("access_token=")) { |
530 | 526 | throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); |
531 | 527 | } |
532 | 528 | String accessToken = getAccessToken(false); |
533 | 529 |
|
534 | | - String uriWithAccessToken = uri; |
535 | | - uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; |
| 530 | + String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken; |
536 | 531 |
|
537 | 532 | try { |
538 | | - return executor.execute(this, uriWithAccessToken, data); |
| 533 | + T result = executor.execute(this, uriWithAccessToken, data); |
| 534 | + this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result); |
| 535 | + return result; |
539 | 536 | } catch (WxErrorException e) { |
540 | 537 | WxError error = e.getError(); |
541 | 538 | /* |
542 | 539 | * 发生以下情况时尝试刷新access_token |
543 | 540 | * 40001 获取access_token时AppSecret错误,或者access_token无效 |
544 | 541 | * 42001 access_token超时 |
| 542 | + * 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口 |
545 | 543 | */ |
546 | | - if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { |
| 544 | + if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) { |
547 | 545 | // 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token |
548 | 546 | this.configStorage.expireAccessToken(); |
549 | 547 | return execute(executor, uri, data); |
550 | 548 | } |
551 | 549 |
|
552 | 550 | if (error.getErrorCode() != 0) { |
553 | | - this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error); |
| 551 | + this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, error); |
554 | 552 | throw new WxErrorException(error); |
555 | 553 | } |
556 | 554 | return null; |
557 | 555 | } catch (IOException e) { |
558 | | - this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage()); |
| 556 | + this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uriWithAccessToken, data, e.getMessage()); |
559 | 557 | throw new RuntimeException(e); |
560 | 558 | } |
561 | 559 | } |
|
0 commit comments