From 9161a9089b42c190ab44fee9d8b848a03abc7ee3 Mon Sep 17 00:00:00 2001 From: zhangjing <924187658@qq.com> Date: Thu, 2 Apr 2026 15:09:53 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E3=80=90=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E3=80=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fkzy/warn/common/util/HttpClientConfig.java | 28 +++++ .../fkzy/warn/controller/DataCenterController.java | 4 +- .../warn/service/impl/AlarmCaseServiceImpl.java | 5 +- .../fkzy/warn/service/impl/FyApiServiceImpl.java | 138 ++++++++++++++++++--- src/main/resources/bootstrap-dev.yml | 1 + 5 files changed, 158 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java diff --git a/src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java b/src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java new file mode 100644 index 0000000..fd07e98 --- /dev/null +++ b/src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java @@ -0,0 +1,28 @@ +package com.fkzy.warn.common.util; + +import okhttp3.OkHttpClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.TimeUnit; + +/** + * @author zhangjing + * @date 2026/04/02 10:47 + * @description + */ +@Configuration +public class HttpClientConfig { + @Bean + public OkHttpClient okHttpClient() { + return new OkHttpClient.Builder() + // 设置各种超时时间 + .connectTimeout(60, TimeUnit.SECONDS) + .writeTimeout(60, TimeUnit.SECONDS) + .readTimeout(5, TimeUnit.MINUTES) + // 可以添加更多配置,例如拦截器、缓存等 + // .addInterceptor(new LoggingInterceptor()) + // .cache(new Cache(cacheDirectory, cacheSize)) + .build(); + } +} diff --git a/src/main/java/com/fkzy/warn/controller/DataCenterController.java b/src/main/java/com/fkzy/warn/controller/DataCenterController.java index 57625bf..559477a 100644 --- a/src/main/java/com/fkzy/warn/controller/DataCenterController.java +++ b/src/main/java/com/fkzy/warn/controller/DataCenterController.java @@ -68,7 +68,9 @@ public class DataCenterController { // fyApiService.getLawGroup(lawGroupParam); // fyApiService.getLawGroupInfo("9f9d4094-d788-52f6-804d-0fc2a5dd462f"); - fyApiService.recommendedCases(null); +// fyApiService.recommendedCases(null); +// fyApiService.caseById("e2db5619-1476-4cae-a461-ad2c0035de71"); +// fyApiService.caseAdvanced(null); } catch (Exception e) { log.error("查询错误,errMsg==={}", e.getMessage()); e.printStackTrace(); diff --git a/src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java b/src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java index 0d1250d..c161fd7 100644 --- a/src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java +++ b/src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java @@ -76,13 +76,13 @@ public class AlarmCaseServiceImpl extends ServiceImpl monitorUsersList = new ArrayList<>(); List alarmCaseList = arr.toJavaList(AlarmCase.class); if (alarmCaseList != null && !alarmCaseList.isEmpty()) { alarmCaseList.forEach(item -> { MonitorUsers monitorUsers = monitorUsersService.getOne(new LambdaQueryWrapper() .eq(MonitorUsers::getCompanyName, item.getName()) + ,false ); if (monitorUsers != null) { item.setNAjbs(item.getNAjbs() + ":" + monitorUsers.getCreditCode()); @@ -90,13 +90,12 @@ public class AlarmCaseServiceImpl extends ServiceImpl { + alarmCaseList.forEach(item -> { if(item.getCompanyName()!=null){ InvocationRecord lawResult = LawResearchUtil.querySxx(item.getCompanyName(), item.getCreditCode(), null); invocationRecordService.saveModel(result); diff --git a/src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java b/src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java index 31f41f7..4015bf3 100644 --- a/src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java +++ b/src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java @@ -2,10 +2,12 @@ package com.fkzy.warn.service.impl; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fkzy.warn.common.constants.LawResearchUrlConstants; +import com.fkzy.warn.common.util.HttpClientConfig; import com.fkzy.warn.common.util.MD5Util; import com.fkzy.warn.common.util.RedisUtil; import com.fkzy.warn.mapper.LawCaseMapper; @@ -16,6 +18,7 @@ import com.fkzy.warn.service.InvocationRecordService; import lombok.extern.slf4j.Slf4j; import okhttp3.*; import okio.BufferedSource; +import org.apache.commons.text.translate.UnicodeUnescaper; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; @@ -78,6 +81,8 @@ public class FyApiServiceImpl extends ServiceImpl implem @Resource RedisUtil redisUtil; @Resource + HttpClientConfig httpClientConfig; + @Resource InvocationRecordService invocationRecordService; @@ -172,7 +177,7 @@ public class FyApiServiceImpl extends ServiceImpl implem String jsonParams = JSON.toJSONString(recommendedCasesParams); HttpHeaders headers = getRequestHeader(jsonParams); - String result = postAip(recommendedCasesUrl, headers, jsonParams, "相似案例推荐"); + String result = postAipOkHttp(recommendedCasesUrl, headers, jsonParams, "相似案例推荐"); ApiResult> resultData = JSON.parseObject( result, new TypeReference>>() { @@ -184,15 +189,16 @@ public class FyApiServiceImpl extends ServiceImpl implem public void caseById(String docId) { JSONObject object = new JSONObject(); - object.put("id", docId); + object.put("docId", docId); String json = object.toJSONString(); HttpHeaders headers = getRequestHeader(json); - String result = postAip(caseByIdUrl, headers, json, "法研平台-按文书标识查询公开案例详情"); + String result = postAipOkHttp(caseByIdUrl, headers, json, "法研平台-按文书标识查询公开案例详情"); ApiResult resultData = JSON.parseObject( result, new TypeReference>() { } ); + resultData.getCode(); } @Override @@ -200,14 +206,29 @@ public class FyApiServiceImpl extends ServiceImpl implem if (judgmentSearchParams ==null){ judgmentSearchParams = new JudgmentSearchParams(); } + judgmentSearchParams.setPageNum(1); + judgmentSearchParams.setPageSize(10); + JudgmentSearchParams.BasicCondition basicCondition = new JudgmentSearchParams.BasicCondition(); + basicCondition.setCasecause(Arrays.asList("盗窃罪","抢劫罪")); + judgmentSearchParams.setBasicCondition(basicCondition); + + JudgmentSearchParams.CaseInfo caseInfo = new JudgmentSearchParams.CaseInfo(); + caseInfo.setEnterprises(Arrays.asList("杨志彬")); + judgmentSearchParams.setCaseInfo(caseInfo); + String jsonParams = JSON.toJSONString(judgmentSearchParams); HttpHeaders headers = getRequestHeader(jsonParams); - String result = postAip(caseAdvancedUrl, headers, jsonParams, "公开案例高级检索"); - ApiResult> resultData = JSON.parseObject( - result, - new TypeReference>>() { - } - ); + String result = postAipOkHttp(caseAdvancedUrl, headers, jsonParams, "公开案例高级检索"); + JSONObject object = JSONObject.parseObject(result); + JSONObject dataObj = object.getJSONObject("data"); + JSONArray jsonArray = dataObj.getJSONArray("docs"); + List resultData = jsonArray.toJavaList(JudgmentDocumentFull.class); +// ApiResult> resultData = JSON.parseObject( +// result, +// new TypeReference>>() { +// } +// ); + resultData.size(); } /** @@ -222,11 +243,7 @@ public class FyApiServiceImpl extends ServiceImpl implem // 1. 创建 OkHttpClient 实例 - OkHttpClient client = new OkHttpClient.Builder() - .connectTimeout(30, TimeUnit.SECONDS) - // SSE是长连接,设置较长的读取超时 - .readTimeout(5, TimeUnit.MINUTES) - .build(); + OkHttpClient client = httpClientConfig.okHttpClient(); // 2. 构建 Request okhttp3.MediaType JSON_TYPE = okhttp3.MediaType.parse("application/json; charset=utf-8"); @@ -322,6 +339,99 @@ public class FyApiServiceImpl extends ServiceImpl implem System.out.println(logDesc + " - 异步请求已发送,正在监听流式响应..."); } + + private String postAipOkHttp(String lawGroupUrl, HttpHeaders headers, String jsonParams, String apiName) { + // 接口调用日志 + InvocationRecord invocationRecord = new InvocationRecord(); + + // 创建 OkHttpClient 实例,配置超时 + // 注意:通常在应用启动时创建一个单例的 OkHttpClient,而不是每次都创建 + // 这里为了演示与原方法结构相似,暂时在此创建 + OkHttpClient client = httpClientConfig.okHttpClient(); + + + // 将 HttpHeaders 转换为 OkHttp 的 Headers + // 注意:HttpHeaders 可能包含多个同名的值,这里仅取第一个值 + Headers.Builder okHeadersBuilder = new Headers.Builder(); + if (headers != null) { + for (String headerName : headers.keySet()) { + String headerValue = headers.getFirst(headerName); // 取第一个值 + if (headerValue != null) { + okHeadersBuilder.add(headerName, headerValue); + } + } + } + Headers okHeaders = okHeadersBuilder.build(); + + // 构建请求体 + okhttp3.MediaType JSON_TYPE = okhttp3.MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(JSON_TYPE, jsonParams); + // 构建请求 + Request okRequest = new Request.Builder() + .url(lawGroupUrl) + .post(body) // 使用 POST 方法 + .headers(okHeaders) // 添加请求头 + .build(); + + try (Response response = client.newCall(okRequest).execute()) { + // 检查响应是否成功 + if (!response.isSuccessful()) { + throw new IOException("HTTP error code: " + response.code()); + } + + // 获取响应体字符串 + ResponseBody responseBody = response.body(); + if (responseBody == null) { + // 记录失败情况 + invocationRecord.setIsSuccess(0); + invocationRecord.setApiResult("Error: Response body is null"); + return null; + } + + String rawDataWithEscapes = responseBody.string(); // 注意:这里会消耗掉 body 流,不能再次读取 + UnicodeUnescaper unescaper = new UnicodeUnescaper(); + String resultData = unescaper.translate(rawDataWithEscapes); // 将转义序列转换为实际字符 + + // 日志 + invocationRecord.setApiUrl(lawGroupUrl); + if (jsonParams != null) { + invocationRecord.setInputObj(jsonParams); + } + invocationRecord.setApiName(apiName); + // 假设初始状态为失败,直到解析 JSON 成功且 code 为 1000 + invocationRecord.setIsSuccess(0); + invocationRecord.setApiResult(resultData); + + // 解析 JSON 并检查 code + try { + JSONObject jsonObject = JSONObject.parseObject(resultData); + if (jsonObject != null && "1000".equals(jsonObject.getString("code"))) { + invocationRecord.setIsSuccess(1); + return resultData; + } else { + // JSON 解析成功但 code 不为 1000,记录为失败 + invocationRecord.setIsSuccess(0); + invocationRecord.setApiResult("API returned non-success code: " + (jsonObject != null ? jsonObject.getString("code") : "null")); + } + } catch (Exception jsonException) { + // JSON 解析失败,记录错误 + invocationRecord.setIsSuccess(0); + invocationRecord.setApiResult("Error parsing JSON response: " + jsonException.getMessage()); + jsonException.printStackTrace(); // 或使用日志框架 + } + + + } catch (IOException e) { + // 记录失败情况 + invocationRecord.setIsSuccess(0); + invocationRecord.setApiResult("Error: " + e.getMessage()); + e.printStackTrace(); // 或者使用更合适的日志框架 + } finally { + // 无论成功还是失败,都确保记录被保存 + invocationRecordService.save(invocationRecord); + } + return null; + } private String postAip(String lawGroupUrl, HttpHeaders headers , String request, String apiName) { //接口调用日志 diff --git a/src/main/resources/bootstrap-dev.yml b/src/main/resources/bootstrap-dev.yml index f82f1b4..ca7b7a7 100644 --- a/src/main/resources/bootstrap-dev.yml +++ b/src/main/resources/bootstrap-dev.yml @@ -53,6 +53,7 @@ minio: ali: accessKeyId: http://182.151.8.209:8081/gas_apportal/publicApi/auth/logout secret: http://182.151.8.209:8081/gas_apportal/menu/getMenuByUserId + secret: SMS_504820123 file: