package com.ynxbd.common.helper.xbd; import com.alibaba.fastjson.JSONObject; import com.ynxbd.common.helper.common.ErrorHelper; 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.*; import java.util.stream.Collectors; 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 requestParams = new HashMap<>(); if (params != null) { params.setParams(requestParams); } return ExecMethodGet(methodName,requestParams); } public static String ExecMethodPost(String methodName,JsonResult.MapParams params) throws Exception { Map requestParams = new HashMap<>(); if (params != null) { params.setParams(requestParams); } log.info("[病案翻拍]未加密前请求-{}",requestParams); RequestBody requestBody; List paramsNameList = new ArrayList<>(requestParams.keySet()); JSONObject jsonObj = new JSONObject(); jsonObj.put("method",compress(URLEncoder.encode(EncHelper.AES_Encrypt(methodName),StandardCharsets.UTF_8.toString()))); for (String paramsName:paramsNameList) { if(requestParams.get(paramsName)==null){ jsonObj.put(paramsName,""); continue; } jsonObj.put(paramsName,compress(URLEncoder.encode(EncHelper.AES_Encrypt(requestParams.get(paramsName).toString()),StandardCharsets.UTF_8.toString()))); } MediaType jsonMedia = MediaType.parse("application/json; charset=utf-8"); RequestBody formBody = RequestBody.create(jsonMedia, jsonObj.toJSONString()); String test = jsonObj.toJSONString(); log.info("[病案翻拍]加密后请求-{}",test); return UsePost(methodName,formBody); } /** * Get执行后台指定方法 * @param methodName 方法名 * @return 返回的数据(Json) */ public static String ExecMethodGet (String methodName){ return UseGet(methodName,null); } private static String UsePost(String methodName, RequestBody requestBody){ OkHttpClient httpClient = OkHttpHelper.creatClient(); Headers.Builder headersBuilder = new Headers.Builder(); HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(XBDHelper.MEDICAL_RECORD_REMAKE_URL + "/api/SqlContext/PostMethodByNameEncryption")).newBuilder(); try (Response response = httpClient.newCall(new Request.Builder().url(urlBuilder.build()).post(requestBody).headers(headersBuilder.build()).build()).execute()) { JSONObject JSON = JSONObject.parseObject(response.body().string()); if(response.isSuccessful()){ log.info("[在线病例打印]返回:方法名-{},内容-{}",methodName,JSON); return JSON.getString("Data"); } else { log.error("[在线病例打印]返回错误:方法名-{},错误-{}",methodName,JSON.toJSONString()); throw new Exception("方法:" + methodName + "错误:" + JSON.getString("Msg") ); } } catch (Exception e) { log.error("[在线病例打印]请求错误-{}",e.toString()); return e.getMessage(); } } private static String UseGet(String methodName, Object paramDic) { try { OkHttpClient httpClient = OkHttpHelper.creatClient(); HttpUrl.Builder urlBuilder = Objects.requireNonNull(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()){ log.info("[在线病例打印]返回:方法名-{},内容-{}",methodName,JSON); if("200".equals(JSON.getString("Code"))){ JSON.getString("Data"); return JSON.getString("Data"); } else { log.error("[[在线病例打印] 请求错误:json-{}",JSON); return JSON.toString(); } } else { log.error("[在线病例打印]返回错误:方法名-{},错误-{}",methodName,JSON.getString("Msg")); throw new Exception("方法:" + methodName + "错误:" + JSON.getString("Msg") ); } } catch (Exception e){ log.error("[在线病例打印]请求错误-{}",e.toString()); return null; } } /** * 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 test = new HashMap<>(); test.put("patientID","10333037"); test.put("BorrowNo",-1); System.out.println(ExecMethodGet("BLSM_Appointment_GetHistoryAppointment", test)); } }