Jelajahi Sumber

微信上传数据 强制刷新token

yuhongqi 1 bulan lalu
induk
melakukan
6ac21c0249

+ 23 - 1
fs-service/src/main/java/com/fs/wx/order/service/ShippingService.java

@@ -24,6 +24,7 @@ public class ShippingService {
 
     private final WeChatApiConfig weChatApiConfig;
 
+
     public ShippingService(WeChatApiConfig weChatApiConfig) {
         this.weChatApiConfig = weChatApiConfig;
     }
@@ -83,8 +84,29 @@ public class ShippingService {
                 if (!weChatApiResponse.isSuccess()) {
                     log.warn("微信接口返回业务错误: code={}, message={}", weChatApiResponse.getErrcode(), weChatApiResponse.getErrmsg());
                     if(ObjectUtil.equal(weChatApiResponse.getErrcode(),40001)) {
+                        log.info("token缓存失效,强制刷新token并重试...");
                         accessToken = weChatAuthService.getAccessToken(true);
-                        log.info("token缓存失效,清除token,等待下次执行...");
+                        if (accessToken != null) {
+                            String retryUrl = UriComponentsBuilder.fromHttpUrl(weChatApiConfig.getUploadShippingInfoUrl())
+                                    .queryParam("access_token", accessToken)
+                                    .toUriString();
+                            try {
+                                HttpResponse retryResponse = HttpUtil.createPost(retryUrl)
+                                        .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
+                                        .body(requestBodyJson)
+                                        .timeout(10000)
+                                        .execute();
+                                try {
+                                    WeChatApiResponse retryResult = JSONUtil.toBean(retryResponse.body(), WeChatApiResponse.class);
+                                    log.info("重试微信接口响应: errcode={}, errmsg={}", retryResult.getErrcode(), retryResult.getErrmsg());
+                                    return retryResult;
+                                } finally {
+                                    retryResponse.close();
+                                }
+                            } catch (Exception retryEx) {
+                                log.error("重试调用微信接口失败", retryEx);
+                            }
+                        }
                     }
                 }
                 return weChatApiResponse;