|
@ -0,0 +1,330 @@
|
|
|
package com.yihu.jw.entrance.util.zysoft;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.jw.entrance.service.LogService;
|
|
|
import com.zoe.phip.ssp.sdk.ApiException;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 智业接口--基本方法
|
|
|
* Created by yeshijie on 2017/7/17.
|
|
|
*/
|
|
|
@Service
|
|
|
public class ZysoftBaseService {
|
|
|
|
|
|
private org.slf4j.Logger logger= LoggerFactory.getLogger(ZysoftBaseService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private LogService logService;
|
|
|
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
//默认重复次数
|
|
|
private int retryTimes = 3;
|
|
|
|
|
|
private Boolean openCrypto = true;
|
|
|
|
|
|
|
|
|
public String getCode() {
|
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取基础路径
|
|
|
* @param apiType
|
|
|
* @return
|
|
|
*/
|
|
|
private String getBaseApi(Integer apiType){
|
|
|
return "wlw";
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 基卫二次封装Get接口
|
|
|
*/
|
|
|
public String getSecond(String api, String content, Map<String,String> params, Map<String,String> headers,
|
|
|
boolean needRetry,Integer apiType) throws Exception
|
|
|
{
|
|
|
String re = "";
|
|
|
headers.put("INTERFACE",api);
|
|
|
|
|
|
Map<String,String> paramsList = new HashMap<>();
|
|
|
|
|
|
String msgBody = JSONObject.toJSONString(params, SerializerFeature.WriteMapNullValue);
|
|
|
String msgHeader = JSONObject.toJSONString(headers, SerializerFeature.WriteMapNullValue);
|
|
|
paramsList.put("msgHeader",msgHeader);
|
|
|
paramsList.put("msgBody",msgBody);
|
|
|
|
|
|
logger.info("msgHeader:"+msgHeader+"\r\n");
|
|
|
logger.info("msgBody:"+msgBody+"\r\n");
|
|
|
|
|
|
//新增日志
|
|
|
String method = "GET";
|
|
|
Boolean isSuccess = true;
|
|
|
String error = "";
|
|
|
|
|
|
String baseApi = getBaseApi(apiType);
|
|
|
|
|
|
int times = 0;
|
|
|
try {
|
|
|
re = ZysoftApi.getSingleton().get(baseApi, paramsList, null,openCrypto);
|
|
|
if(needRetry)
|
|
|
{
|
|
|
|
|
|
while(retryTimes>0 && re.contains("接口调用传入的参数[msgHeader]格式不正确")) //基卫bug预防,重调接口
|
|
|
{
|
|
|
re = ZysoftApi.getSingleton().post(baseApi, paramsList, null,openCrypto);
|
|
|
retryTimes --;
|
|
|
times++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Map<String,String> map = objectMapper.readValue(re,Map.class);
|
|
|
String code = map.get("CODE");
|
|
|
String message = map.get("MESSAGE");
|
|
|
|
|
|
if(!code.equals("1"))
|
|
|
{
|
|
|
throw new HttpApiException(Integer.valueOf(code),message);
|
|
|
}
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
StringWriter sw = new StringWriter();
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
ex.printStackTrace(pw);
|
|
|
|
|
|
error = sw.toString();
|
|
|
isSuccess = false;
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
|
|
|
if(ex instanceof ApiException)
|
|
|
{
|
|
|
ApiException apiEx = (ApiException) ex;
|
|
|
throw new HttpApiException(apiEx.errorCode(),ex.getMessage());
|
|
|
}
|
|
|
else{
|
|
|
throw new HttpApiException(-1,ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基卫二次封装Post接口
|
|
|
*/
|
|
|
public String postSecond(String api,String content, Map<String,String> params,JSONObject jsonParams, Map<String,String> headers,
|
|
|
boolean needRetry,Integer apiType,String type) throws Exception
|
|
|
{
|
|
|
String re = "";
|
|
|
headers.put("INTERFACE",api);
|
|
|
|
|
|
Map<String,String> paramsList = new HashMap<>();
|
|
|
String msgBody = params==null?JSONObject.toJSONString(jsonParams, SerializerFeature.WriteMapNullValue):
|
|
|
JSONObject.toJSONString(params, SerializerFeature.WriteMapNullValue);
|
|
|
String msgHeader = JSONObject.toJSONString(headers, SerializerFeature.WriteMapNullValue);
|
|
|
paramsList.put("msgHeader",msgHeader);
|
|
|
paramsList.put("msgBody",msgBody);
|
|
|
|
|
|
logger.info("msgHeader:"+msgHeader+"\r\n");
|
|
|
logger.info("msgBody:"+msgBody+"\r\n");
|
|
|
|
|
|
//新增日志
|
|
|
String method = "POST";
|
|
|
Boolean isSuccess = true;
|
|
|
String error = "";
|
|
|
|
|
|
String baseApi = getBaseApi(apiType);
|
|
|
|
|
|
int times = 0;
|
|
|
try {
|
|
|
re = ZysoftApi.getSingleton().post(baseApi, paramsList, null,openCrypto);
|
|
|
if(needRetry)
|
|
|
{
|
|
|
int maxTimes = retryTimes;
|
|
|
while(maxTimes>0 && re.contains("接口调用传入的参数[msgHeader]格式不正确")) //基卫bug预防,重调接口
|
|
|
{
|
|
|
re = ZysoftApi.getSingleton().post(baseApi, paramsList, null,openCrypto);
|
|
|
maxTimes --;
|
|
|
times++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Map<String,String> map = objectMapper.readValue(re,Map.class);
|
|
|
String code = map.get("CODE");
|
|
|
|
|
|
if(!code.equals("1"))
|
|
|
{
|
|
|
throw new HttpApiException(Integer.valueOf(code),map.get("MESSAGE"));
|
|
|
}
|
|
|
|
|
|
// com.alibaba.fastjson.JSONObject j = com.alibaba.fastjson.JSONObject.parseObject(re);
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
StringWriter sw = new StringWriter();
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
ex.printStackTrace(pw);
|
|
|
error = sw.toString();
|
|
|
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
|
|
|
if(ex instanceof ApiException)
|
|
|
{
|
|
|
logger.info(ex.getMessage());
|
|
|
ApiException apiEx = (ApiException) ex;
|
|
|
throw new HttpApiException(apiEx.errorCode(),ex.getMessage());
|
|
|
}
|
|
|
else{
|
|
|
throw new HttpApiException(-1,ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基卫二次封装Post接口
|
|
|
* YG请求 新的公钥和私钥
|
|
|
*/
|
|
|
public String postSecondYg(String api,String content, Map<String,String> params,JSONObject jsonParams, Map<String,String> headers,
|
|
|
boolean needRetry,Integer apiType,String type) throws Exception
|
|
|
{
|
|
|
String re = "";
|
|
|
headers.put("INTERFACE",api);
|
|
|
|
|
|
Map<String,String> paramsList = new HashMap<>();
|
|
|
String msgBody = params==null?JSONObject.toJSONString(jsonParams, SerializerFeature.WriteMapNullValue):
|
|
|
JSONObject.toJSONString(params, SerializerFeature.WriteMapNullValue);
|
|
|
String msgHeader = JSONObject.toJSONString(headers, SerializerFeature.WriteMapNullValue);
|
|
|
paramsList.put("msgHeader",msgHeader);
|
|
|
paramsList.put("msgBody",msgBody);
|
|
|
|
|
|
logger.info("msgHeader:"+msgHeader+"\r\n");
|
|
|
logger.info("msgBody:"+msgBody+"\r\n");
|
|
|
|
|
|
//新增日志
|
|
|
String method = "POST";
|
|
|
Boolean isSuccess = true;
|
|
|
String error = "";
|
|
|
|
|
|
String baseApi = getBaseApi(apiType);
|
|
|
|
|
|
int times = 0;
|
|
|
try {
|
|
|
re = ZysoftApiSeconde.getSingleton().post(baseApi, paramsList, null,openCrypto,"SM2_V2",headers.get("LICENCE").toString());
|
|
|
if(needRetry)
|
|
|
{
|
|
|
int maxTimes = retryTimes;
|
|
|
while(maxTimes>0 && re.contains("接口调用传入的参数[msgHeader]格式不正确")) //基卫bug预防,重调接口
|
|
|
{
|
|
|
re = ZysoftApiSeconde.getSingleton().post(baseApi, paramsList, null,openCrypto,"SM2_V2",headers.get("LICENCE").toString());
|
|
|
maxTimes --;
|
|
|
times++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Map<String,String> map = objectMapper.readValue(re,Map.class);
|
|
|
String code = map.get("CODE");
|
|
|
|
|
|
if(!code.equals("1"))
|
|
|
{
|
|
|
throw new HttpApiException(Integer.valueOf(code),map.get("MESSAGE"));
|
|
|
}
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
//保存http日志
|
|
|
logService.saveHttpLog(isSuccess,api,content,method,msgHeader,msgBody,re,error);
|
|
|
|
|
|
if(ex instanceof ApiException)
|
|
|
{
|
|
|
logger.info(ex.getMessage());
|
|
|
ApiException apiEx = (ApiException) ex;
|
|
|
throw new HttpApiException(apiEx.errorCode(),ex.getMessage());
|
|
|
}
|
|
|
else{
|
|
|
throw new HttpApiException(-1,ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
/*************************** 格式转换 *******************************************************/
|
|
|
/**
|
|
|
* 获取单对象
|
|
|
*/
|
|
|
public Map<String,String> getJwOne(String response) throws Exception
|
|
|
{
|
|
|
Map<String,String> re = new HashMap<>();
|
|
|
Map<String,Object> map = objectMapper.readValue(response,Map.class);
|
|
|
String code = String.valueOf(map.get("CODE"));
|
|
|
if(code.equals("1")) {
|
|
|
List<Map<String, String>> list = (List<Map<String, String>>) map.get("DATA");
|
|
|
if (list != null && list.size() > 0) {
|
|
|
re = list.get(0);
|
|
|
}
|
|
|
}
|
|
|
else{
|
|
|
re.put("MESSAGE",String.valueOf(map.get("MESSAGE")));
|
|
|
}
|
|
|
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取单对象列表
|
|
|
**/
|
|
|
public List<Map<String,String>> getJwList(String response) throws Exception
|
|
|
{
|
|
|
Map<String,Object> map = objectMapper.readValue(response,Map.class);
|
|
|
List<Map<String,String>> list = (List<Map<String,String>>)map.get("DATA");
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param api
|
|
|
* @param hospital
|
|
|
* @param userName
|
|
|
* @param password
|
|
|
* @return
|
|
|
*/
|
|
|
private String getHeaderXml(String api,String hospital,String userName,String password)
|
|
|
{
|
|
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
|
|
|
"<root>" +
|
|
|
" <serverName>"+api+"</serverName>" +
|
|
|
" <orgId>"+hospital+"</orgId>"+
|
|
|
" <userName>"+userName+"</userName>"+
|
|
|
" <password>"+password+"</password>"+
|
|
|
"</root>";
|
|
|
}
|
|
|
|
|
|
}
|