Browse Source

fix:【平台接口】修改

master
zhangjing 2 weeks ago
parent
commit
9161a9089b
5 changed files with 158 additions and 18 deletions
  1. +28
    -0
      src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java
  2. +3
    -1
      src/main/java/com/fkzy/warn/controller/DataCenterController.java
  3. +2
    -3
      src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java
  4. +124
    -14
      src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java
  5. +1
    -0
      src/main/resources/bootstrap-dev.yml

+ 28
- 0
src/main/java/com/fkzy/warn/common/util/HttpClientConfig.java View File

@ -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();
}
}

+ 3
- 1
src/main/java/com/fkzy/warn/controller/DataCenterController.java View File

@ -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();

+ 2
- 3
src/main/java/com/fkzy/warn/service/impl/AlarmCaseServiceImpl.java View File

@ -76,13 +76,13 @@ public class AlarmCaseServiceImpl extends ServiceImpl
return;
}
List<MonitorUsers> monitorUsersList = new ArrayList<>();
List<AlarmCase> alarmCaseList = arr.toJavaList(AlarmCase.class);
if (alarmCaseList != null && !alarmCaseList.isEmpty()) {
alarmCaseList.forEach(item -> {
MonitorUsers monitorUsers = monitorUsersService.getOne(new LambdaQueryWrapper<MonitorUsers>()
.eq(MonitorUsers::getCompanyName, item.getName())
,false
);
if (monitorUsers != null) {
item.setNAjbs(item.getNAjbs() + ":" + monitorUsers.getCreditCode());
@ -90,13 +90,12 @@ public class AlarmCaseServiceImpl extends ServiceImpl
item.setCreditCode(monitorUsers.getCreditCode());
}
item.setAlarmDate(new Date());
monitorUsersList.add(monitorUsers);
});
}
this.saveBatch(alarmCaseList);
//如果有预警需要再查司法全量数据有待考虑,先存预警信息
monitorUsersList.forEach(item -> {
alarmCaseList.forEach(item -> {
if(item.getCompanyName()!=null){
InvocationRecord lawResult = LawResearchUtil.querySxx(item.getCompanyName(), item.getCreditCode(), null);
invocationRecordService.saveModel(result);

+ 124
- 14
src/main/java/com/fkzy/warn/service/impl/FyApiServiceImpl.java View File

@ -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<List<JudgmentDocument>> resultData = JSON.parseObject(
result,
new TypeReference<ApiResult<List<JudgmentDocument>>>() {
@ -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<JudgmentDocumentFull> resultData = JSON.parseObject(
result,
new TypeReference<ApiResult<JudgmentDocumentFull>>() {
}
);
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<List<JudgmentDocumentFull>> resultData = JSON.parseObject(
result,
new TypeReference<ApiResult<List<JudgmentDocumentFull>>>() {
}
);
String result = postAipOkHttp(caseAdvancedUrl, headers, jsonParams, "公开案例高级检索");
JSONObject object = JSONObject.parseObject(result);
JSONObject dataObj = object.getJSONObject("data");
JSONArray jsonArray = dataObj.getJSONArray("docs");
List<JudgmentDocumentFull> resultData = jsonArray.toJavaList(JudgmentDocumentFull.class);
// ApiResult<List<JudgmentDocumentFull>> resultData = JSON.parseObject(
// result,
// new TypeReference<ApiResult<List<JudgmentDocumentFull>>>() {
// }
// );
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) {
//接口调用日志

+ 1
- 0
src/main/resources/bootstrap-dev.yml View File

@ -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:

Loading…
Cancel
Save