yjwang 1 viikko sitten
vanhempi
commit
48df7003d1

+ 122 - 43
fs-service/src/main/java/com/fs/qwApi/service/impl/QwApiServiceImpl.java

@@ -1460,63 +1460,142 @@ public class QwApiServiceImpl implements QwApiService {
         }
         return null;
     }
+//    public String sendPost(String url, Object body, String corpId, boolean bookSecret) {
+//        QwCompany qwCompany = iQwCompanyService.selectQwCompanyByCorpId(corpId);
+//        log.info("获取请求打印---------------------》{}",qwCompany);
+//        if(StringUtils.isNotEmpty(qwCompany.getQwApiUrl())){
+//            QwApiParam param = new QwApiParam();
+//            param.setUrl(url);
+//            param.setCorpId(corpId);
+//            param.setBody(JSON.parseObject(JSON.toJSONString(body)));
+//            param.setBookSecret(bookSecret);
+//            ResponseEntity<QwApiResult> response = new RestTemplate().postForEntity(qwCompany.getQwApiUrl() + "/post", param, QwApiResult.class);
+//            QwApiResult result = response.getBody();
+//            log.info("打印结果---------------------》{}",result);
+//            if (result == null) {
+//                log.error("返回错误,返回数据为空!");
+//                return null;
+//            }
+//            log.info("请求结果:{}", JSON.toJSONString(result));
+//            if (result.getCode() != 200) {
+//                log.error("接口请求错误:{}", result.getMsg());
+//                return null;
+//            }
+//            return result.getData();
+//        }
+//        int maxRetries = 3; // 最大重试次数
+//        int retryCount = 0; // 当前重试次数
+//        // 1. 获取配置信息(可放在循环外,但要确保每次都能获取到最新的token)
+//        String appSecret = getAppSecret(corpId);
+//        while (retryCount < maxRetries) {
+//            try {
+//                // 2. 构造并发送请求
+//                HttpClient httpClient = HttpClients.createDefault();
+//                URIBuilder builder = new URIBuilder(url);
+//                builder.setParameter("access_token", getToken(corpId, appSecret));
+//                URI uri = builder.build();
+//                HttpPost httpPost = new HttpPost(uri);
+//                httpPost.setEntity(new StringEntity(JSON.toJSONString(body), StandardCharsets.UTF_8));
+//                HttpResponse response = httpClient.execute(httpPost);
+//                // 3. 解析结果
+//                String reJson = EntityUtils.toString(response.getEntity());
+//                QwResult qwResult = JSON.parseObject(reJson, QwResult.class);
+//                // 4. 判断错误码
+//                if (qwResult.getErrcode() == 45035) {
+//                    retryCount++;
+//                    Thread.sleep(2000);
+//                } else {
+//                    return reJson;
+//                }
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//                break;
+//            }
+//        }
+//
+//        return null;
+//    }
+
     public String sendPost(String url, Object body, String corpId, boolean bookSecret) {
         QwCompany qwCompany = iQwCompanyService.selectQwCompanyByCorpId(corpId);
-        log.info("获取请求打印---------------------》{}",qwCompany);
-        if(StringUtils.isNotEmpty(qwCompany.getQwApiUrl())){
-            QwApiParam param = new QwApiParam();
-            param.setUrl(url);
-            param.setCorpId(corpId);
-            param.setBody(JSON.parseObject(JSON.toJSONString(body)));
-            param.setBookSecret(bookSecret);
-            ResponseEntity<QwApiResult> response = new RestTemplate().postForEntity(qwCompany.getQwApiUrl() + "/post", param, QwApiResult.class);
-            QwApiResult result = response.getBody();
-            log.info("打印结果---------------------》{}",result);
-            if (result == null) {
-                log.error("返回错误,返回数据为空!");
-                return null;
-            }
-            log.info("请求结果:{}", JSON.toJSONString(result));
-            if (result.getCode() != 200) {
-                log.error("接口请求错误:{}", result.getMsg());
-                return null;
-            }
-            return result.getData();
-        }
+        log.info("获取请求打印---------------------》{}", qwCompany);
+
         int maxRetries = 3; // 最大重试次数
         int retryCount = 0; // 当前重试次数
-        // 1. 获取配置信息(可放在循环外,但要确保每次都能获取到最新的token)
-        String appSecret = getAppSecret(corpId);
-        while (retryCount < maxRetries) {
+        while (retryCount <= maxRetries) {
             try {
-                // 2. 构造并发送请求
-                HttpClient httpClient = HttpClients.createDefault();
-                URIBuilder builder = new URIBuilder(url);
-                builder.setParameter("access_token", getToken(corpId, appSecret));
-                URI uri = builder.build();
-                HttpPost httpPost = new HttpPost(uri);
-                httpPost.setEntity(new StringEntity(JSON.toJSONString(body), StandardCharsets.UTF_8));
-                HttpResponse response = httpClient.execute(httpPost);
-                // 3. 解析结果
-                String reJson = EntityUtils.toString(response.getEntity());
-                QwResult qwResult = JSON.parseObject(reJson, QwResult.class);
-                // 4. 判断错误码
-                if (qwResult.getErrcode() == 45035) {
-                    retryCount++;
-                    Thread.sleep(2000);
+                if (StringUtils.isNotEmpty(qwCompany.getQwApiUrl())) {
+                    QwApiParam param = new QwApiParam();
+                    param.setUrl(url);
+                    param.setCorpId(corpId);
+                    param.setBody(JSON.parseObject(JSON.toJSONString(body)));
+                    param.setBookSecret(bookSecret);
+
+                    ResponseEntity<QwApiResult> response = new RestTemplate().postForEntity(
+                            qwCompany.getQwApiUrl() + "/post", param, QwApiResult.class);
+                    QwApiResult result = response.getBody();
+
+                    log.info("打印结果---------------------》{}", result);
+                    if (result == null) {
+                        log.error("返回错误,返回数据为空!");
+                        return null;
+                    }
+
+                    log.info("请求结果:{}", JSON.toJSONString(result));
+                    if (result.getCode() != 200) {
+                        if (result.getCode() == 45035 && retryCount < maxRetries) {
+                            retryCount++;
+                            log.warn("代理接口遇到45035频率限制,第{}次重试,等待2秒, corpId: {}", retryCount, corpId);
+                            Thread.sleep(2000);
+                            continue;
+                        } else {
+                            log.error("代理接口请求错误:{}", result.getMsg());
+                            return null;
+                        }
+                    }
+                    return result.getData();
                 } else {
-                    return reJson;
+                    String appSecret = getAppSecret(corpId);
+                    HttpClient httpClient = HttpClients.createDefault();
+                    URIBuilder builder = new URIBuilder(url);
+                    builder.setParameter("access_token", getToken(corpId, appSecret));
+                    URI uri = builder.build();
+                    HttpPost httpPost = new HttpPost(uri);
+                    httpPost.setEntity(new StringEntity(JSON.toJSONString(body), StandardCharsets.UTF_8));
+
+                    HttpResponse response = httpClient.execute(httpPost);
+                    String reJson = EntityUtils.toString(response.getEntity());
+                    QwResult qwResult = JSON.parseObject(reJson, QwResult.class);
+
+                    if (qwResult.getErrcode() == 45035 && retryCount < maxRetries) {
+                        retryCount++;
+                        log.warn("企业微信接口遇到45035频率限制,第{}次重试,等待2秒, corpId: {}", retryCount, corpId);
+                        Thread.sleep(2000);
+                        continue;
+                    } else {
+                        return reJson;
+                    }
                 }
             } catch (Exception e) {
                 e.printStackTrace();
-                break;
+                if (retryCount < maxRetries) {
+                    retryCount++;
+                    try {
+                        log.warn("请求异常,第{}次重试,等待2秒, corpId: {}", retryCount, corpId);
+                        Thread.sleep(2000);
+                    } catch (InterruptedException ie) {
+                        Thread.currentThread().interrupt();
+                        break;
+                    }
+                    continue;
+                } else {
+                    break;
+                }
             }
         }
-
         return null;
     }
 
-
 //    public String sendPost(String url,Object param,String corpId){
 //
 //        QwCompany qwCompany = iQwCompanyService.selectQwCompanyByCorpId(corpId);