You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
6.3 KiB
178 lines
6.3 KiB
package com.ynxbd.common.helper.xbd;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ynxbd.common.helper.http.OkHttpHelper;
|
|
import com.ynxbd.common.result.JsonResult;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import okhttp3.*;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Base64;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.zip.GZIPInputStream;
|
|
import java.util.zip.GZIPOutputStream;
|
|
|
|
|
|
/**
|
|
* @author 李进才
|
|
* @ClassName HttpHelper
|
|
* @Description 在线病历打印请求的http请求方法
|
|
* @date 2023/4/20 09:40
|
|
*/
|
|
@Slf4j
|
|
public class XBDHttpHelper {
|
|
/**
|
|
* Get执行后台指定方法
|
|
* @param methodName 方法名
|
|
* @param paramDic 参数
|
|
* @return 返回的数据(Json)
|
|
*/
|
|
public static String ExecMethodGet (String methodName, Object paramDic){
|
|
return UseGet(methodName,paramDic);
|
|
}
|
|
|
|
/**
|
|
* 直接用lambda传参数的方法
|
|
* @param methodName 方法名
|
|
* @param params lambda表达式
|
|
* @return String
|
|
*/
|
|
public static String ExecMethodGet(String methodName, JsonResult.MapParams params){
|
|
Map<String, Object> requestParams = new HashMap<>();
|
|
if (params != null) {
|
|
params.setParams(requestParams);
|
|
}
|
|
return ExecMethodGet(methodName,requestParams);
|
|
}
|
|
|
|
|
|
/**
|
|
* Get执行后台指定方法
|
|
* @param methodName 方法名
|
|
* @return 返回的数据(Json)
|
|
*/
|
|
public static String ExecMethodGet (String methodName){
|
|
return UseGet(methodName,null);
|
|
}
|
|
private static String UseGet(String methodName, Object paramDic) {
|
|
try {
|
|
OkHttpClient httpClient = OkHttpHelper.creatClient();
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(XBDHelper.MEDICAL_RECORD_REMAKE_URL+"/api/SqlContext/ExecMethodByNameEncryption").newBuilder();
|
|
// 加密序列化
|
|
String methodNameEnv = URLEncoder.encode(EncHelper.AES_Encrypt(methodName), StandardCharsets.UTF_8.toString());
|
|
urlBuilder.addQueryParameter("methodNameEnc", methodNameEnv);
|
|
String paramJson = JSONObject.toJSONString(paramDic==null? "" :paramDic);
|
|
// json 加密序列化传入链接
|
|
String paramsStrEnv = URLEncoder.encode(EncHelper.AES_Encrypt(paramJson), StandardCharsets.UTF_8.toString());
|
|
urlBuilder.addQueryParameter("paramsStrEnc", compress(paramsStrEnv));
|
|
String md5Check = URLEncoder.encode(EncHelper.MD5Encrypt64(methodName + paramJson),StandardCharsets.UTF_8.toString());
|
|
urlBuilder.addQueryParameter("modCode", md5Check);
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
.url(urlBuilder.toString())
|
|
.get()
|
|
.build();
|
|
Response response = httpClient.newCall(request).execute();
|
|
// 清除并关闭线程池
|
|
// httpClient.dispatcher().executorService().shutdown();
|
|
// 清除并关闭连接池
|
|
// httpClient.connectionPool().evictAll();
|
|
JSONObject JSON = JSONObject.parseObject(response.body().string());
|
|
if(response.isSuccessful()){
|
|
return JSON.getString("Data").replaceAll(" ","");
|
|
}
|
|
else {
|
|
log.error("[在线病例打印]返回错误:方法名-{},错误-{}",methodName,JSON.getString("Msg"));
|
|
throw new Exception("方法:" + methodName + "错误:" + JSON.getString("Msg") );
|
|
}
|
|
}
|
|
catch (Exception e){
|
|
log.error("[在线病例打印]请求错误-{}",e.getMessage());
|
|
return e.getMessage();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* gzip压缩方法
|
|
* @param primStr 压缩字符串
|
|
* @return 压缩后的字符
|
|
*/
|
|
private static String compress(String primStr) {
|
|
if (primStr == null || primStr.length() == 0) {
|
|
return primStr;
|
|
}
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
GZIPOutputStream gzip = null;
|
|
try {
|
|
gzip = new GZIPOutputStream(out);
|
|
gzip.write(primStr.getBytes());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (gzip != null) {
|
|
try {
|
|
gzip.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
|
|
}
|
|
private static String uncompress(String compressedStr) {
|
|
if (compressedStr == null) {
|
|
return null;
|
|
}
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
ByteArrayInputStream in = null;
|
|
GZIPInputStream ginzip = null;
|
|
byte[] compressed = null;
|
|
String decompressed = null;
|
|
try {
|
|
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
|
|
in = new ByteArrayInputStream(compressed);
|
|
ginzip = new GZIPInputStream(in);
|
|
|
|
byte[] buffer = new byte[1024];
|
|
int offset = -1;
|
|
while ((offset = ginzip.read(buffer)) != -1) {
|
|
out.write(buffer, 0, offset);
|
|
}
|
|
decompressed = out.toString();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (ginzip != null) {
|
|
try {
|
|
ginzip.close();
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
if (in != null) {
|
|
try {
|
|
in.close();
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
try {
|
|
out.close();
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
return decompressed;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Map<String,String> test = new HashMap<>();
|
|
test.put("patientID","10947918");
|
|
System.out.println(ExecMethodGet("BLSM_Appointment_GetBA", test));
|
|
}
|
|
}
|
|
|