/* * Copyright ©2023 CJB-CNIT Team. All rights reserved */ package com.openhis.controller; import com.alibaba.fastjson2.JSONObject; import com.core.common.utils.StringUtils; import com.fasterxml.jackson.databind.ObjectMapper; import com.openhis.vo.EleInvioceBillDto; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; import com.core.common.annotation.Anonymous; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * TODO:医保控制层 * * @author SunJQ * @date 2025-03-18 */ @RestController @RequestMapping("/eleInvoice") public class YbController { Logger logger = LoggerFactory.getLogger(YbController.class); // logger.debug("这是专用日志测试"); @PostMapping(value = "/forward") @Anonymous public JSONObject signIn(@RequestBody EleInvioceBillDto eleInvioceBillDto) { JSONObject result = new JSONObject(); // 获取当前租户的option信息 //JSONObject optionJson = SecurityUtils.getLoginUser().getOptionJson(); String baseUrl = eleInvioceBillDto.getBaseUrl(); // 拼接成完整 URL(作为路径) String cleanUrl = baseUrl + "/" + eleInvioceBillDto.getEndpoint(); // 确保用 "/" 分隔 String url = cleanUrl.trim().replaceAll("^\"|\"$", "") // 去除首尾引号 .replaceAll("\\s+", "")// 去除首尾引号 .replaceAll("\"", ""); // 去除中间引号 //String appID = optionJson.getString(CommonConstants.Option.APP_ID); String appID = eleInvioceBillDto.getAppID(); //String appKey = optionJson.getString(CommonConstants.Option.KEY); String appKey = eleInvioceBillDto.getAppKey(); String data = eleInvioceBillDto.getJsonObject().toJSONString(); String version = "1.0"; // 请求随机标识 noise String noise = UUID.randomUUID().toString(); data = Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8)); StringBuilder str = new StringBuilder(); str.append("appid=").append(appID); str.append("&data=").append(data); str.append("&noise=").append(noise); str.append("&key=").append(appKey); str.append("&version=").append(version); String sign = DigestUtils.md5Hex(str.toString().getBytes(Charset.forName("UTF-8"))).toUpperCase(); Map map = new HashMap<>(); map.put("appid", appID); map.put("data", data); map.put("noise", noise); map.put("sign", sign); map.put("version", version); try { HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); String respContent = null; // 请求参数转JOSN字符串 StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(map), "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { String rev = EntityUtils.toString(resp.getEntity()); // System.out.println("返回串--》"+rev); Map resultData = new ObjectMapper().readValue(rev, Map.class); String rdata = resultData.get("data").toString(); String rnoise = resultData.get("noise").toString(); // 1、拼接返回验签参数 StringBuilder str1 = new StringBuilder(); str1.append("appid=").append(appID); str1.append("&data=").append(rdata); str1.append("&noise=").append(rnoise); str1.append("&key=").append(appKey); str1.append("&version=").append(version); // 3.MD5加密 生成sign String rmd5 = DigestUtils.md5Hex(str1.toString().getBytes(Charset.forName("UTF-8"))).toUpperCase(); String rsign = resultData.get("sign").toString(); System.out.println("验签-》" + (StringUtils.equals(rsign, rmd5))); String busData = new String(Base64.getDecoder().decode(resultData.get("data").toString()), StandardCharsets.UTF_8); System.out.println("返回业务数据--》" + busData); Map busDataMap = new ObjectMapper().readValue(busData, Map.class); System.out .println("业务信息解密--》" + new String(Base64.getDecoder().decode(busDataMap.get("message").toString()), StandardCharsets.UTF_8)); JSONObject resobj = JSONObject.parseObject(busData); result.put("success", true); result.put("result", resobj); } else { result.put("msg", "web响应失败!"); result.put("success", false); } } catch (Exception e) { result.put("msg", e.getMessage()); result.put("success", false); } return result; } }