|
|
@ -2,10 +2,12 @@ package com.fkzy.warn.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON; |
|
|
import com.alibaba.fastjson2.JSON; |
|
|
|
|
|
import com.alibaba.fastjson2.JSONArray; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import com.alibaba.fastjson2.TypeReference; |
|
|
import com.alibaba.fastjson2.TypeReference; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.fkzy.warn.common.constants.LawResearchUrlConstants; |
|
|
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.MD5Util; |
|
|
import com.fkzy.warn.common.util.RedisUtil; |
|
|
import com.fkzy.warn.common.util.RedisUtil; |
|
|
import com.fkzy.warn.mapper.LawCaseMapper; |
|
|
import com.fkzy.warn.mapper.LawCaseMapper; |
|
|
@ -16,6 +18,7 @@ import com.fkzy.warn.service.InvocationRecordService; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import okhttp3.*; |
|
|
import okhttp3.*; |
|
|
import okio.BufferedSource; |
|
|
import okio.BufferedSource; |
|
|
|
|
|
import org.apache.commons.text.translate.UnicodeUnescaper; |
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
@ -78,6 +81,8 @@ public class FyApiServiceImpl extends ServiceImpl implem |
|
|
@Resource |
|
|
@Resource |
|
|
RedisUtil redisUtil; |
|
|
RedisUtil redisUtil; |
|
|
@Resource |
|
|
@Resource |
|
|
|
|
|
HttpClientConfig httpClientConfig; |
|
|
|
|
|
@Resource |
|
|
InvocationRecordService invocationRecordService; |
|
|
InvocationRecordService invocationRecordService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -172,7 +177,7 @@ public class FyApiServiceImpl extends ServiceImpl implem |
|
|
|
|
|
|
|
|
String jsonParams = JSON.toJSONString(recommendedCasesParams); |
|
|
String jsonParams = JSON.toJSONString(recommendedCasesParams); |
|
|
HttpHeaders headers = getRequestHeader(jsonParams); |
|
|
HttpHeaders headers = getRequestHeader(jsonParams); |
|
|
String result = postAip(recommendedCasesUrl, headers, jsonParams, "相似案例推荐"); |
|
|
|
|
|
|
|
|
String result = postAipOkHttp(recommendedCasesUrl, headers, jsonParams, "相似案例推荐"); |
|
|
ApiResult<List<JudgmentDocument>> resultData = JSON.parseObject( |
|
|
ApiResult<List<JudgmentDocument>> resultData = JSON.parseObject( |
|
|
result, |
|
|
result, |
|
|
new TypeReference<ApiResult<List<JudgmentDocument>>>() { |
|
|
new TypeReference<ApiResult<List<JudgmentDocument>>>() { |
|
|
@ -184,15 +189,16 @@ public class FyApiServiceImpl extends ServiceImpl implem |
|
|
public void caseById(String docId) { |
|
|
public void caseById(String docId) { |
|
|
|
|
|
|
|
|
JSONObject object = new JSONObject(); |
|
|
JSONObject object = new JSONObject(); |
|
|
object.put("id", docId); |
|
|
|
|
|
|
|
|
object.put("docId", docId); |
|
|
String json = object.toJSONString(); |
|
|
String json = object.toJSONString(); |
|
|
HttpHeaders headers = getRequestHeader(json); |
|
|
HttpHeaders headers = getRequestHeader(json); |
|
|
String result = postAip(caseByIdUrl, headers, json, "法研平台-按文书标识查询公开案例详情"); |
|
|
|
|
|
|
|
|
String result = postAipOkHttp(caseByIdUrl, headers, json, "法研平台-按文书标识查询公开案例详情"); |
|
|
ApiResult<JudgmentDocumentFull> resultData = JSON.parseObject( |
|
|
ApiResult<JudgmentDocumentFull> resultData = JSON.parseObject( |
|
|
result, |
|
|
result, |
|
|
new TypeReference<ApiResult<JudgmentDocumentFull>>() { |
|
|
new TypeReference<ApiResult<JudgmentDocumentFull>>() { |
|
|
} |
|
|
} |
|
|
); |
|
|
); |
|
|
|
|
|
resultData.getCode(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@ -200,14 +206,29 @@ public class FyApiServiceImpl extends ServiceImpl implem |
|
|
if (judgmentSearchParams ==null){ |
|
|
if (judgmentSearchParams ==null){ |
|
|
judgmentSearchParams = new JudgmentSearchParams(); |
|
|
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); |
|
|
String jsonParams = JSON.toJSONString(judgmentSearchParams); |
|
|
HttpHeaders headers = getRequestHeader(jsonParams); |
|
|
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 实例 |
|
|
// 1. 创建 OkHttpClient 实例 |
|
|
OkHttpClient client = new OkHttpClient.Builder() |
|
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS) |
|
|
|
|
|
// SSE是长连接,设置较长的读取超时 |
|
|
|
|
|
.readTimeout(5, TimeUnit.MINUTES) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
OkHttpClient client = httpClientConfig.okHttpClient(); |
|
|
|
|
|
|
|
|
// 2. 构建 Request |
|
|
// 2. 构建 Request |
|
|
okhttp3.MediaType JSON_TYPE = okhttp3.MediaType.parse("application/json; charset=utf-8"); |
|
|
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 + " - 异步请求已发送,正在监听流式响应..."); |
|
|
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 |
|
|
private String postAip(String lawGroupUrl, HttpHeaders headers |
|
|
, String request, String apiName) { |
|
|
, String request, String apiName) { |
|
|
//接口调用日志 |
|
|
//接口调用日志 |
|
|
|