2025-08-01 17:19:23 +08:00

149 lines
5.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.czlis.interfaceCommon.utils;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.czlis.common.annotation.Async;
import com.czlis.common.annotation.Log;
import com.czlis.common.config.RuoYiConfig;
import com.czlis.common.enums.BusinessType;
import com.czlis.interfaceCommon.constants.LisinterfaceNameConstants;
import com.czlis.common.core.domain.Result;
import com.czlis.interfaceCommon.mapper.LisInterfaceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class LisInterfaceUtil {
@Resource
LisInterfaceMapper lisInterfaceMapper;
@Resource
CommonUtil commonUtil;
/**
* 接口调用
* @param type
* @param inValue
* @return
*/
@Log(title = "接口调用",businessType = BusinessType.INTERFACE_INVOKE)
public Result invokeLisInterface(String type,String inValue) {
String interfaceType = commonUtil.getComOptValue("HISOPT","SP_INTER");
log.info("开始调用{}接口,入参:{}",type,interfaceType);
switch (interfaceType){
case "2": //存储过程
return invokeSP(type, inValue);
case "3": //http方式
return invokeHttp(type, inValue);
}
return new Result("-1","无效的接口方式,请检查参数设置!!");
}
/**
* 存储过程方式调用接口
* @param type
* @param inValue
* @return
*/
//@Transactional(rollbackFor = Exception.class)
private Result invokeSP(String type,String inValue){
Map<String,Object> map = new HashMap<>();
//为了兼容老接口,存储过程方式使用的是xml,这里需要把入参转成xml格式
String xmlStr = changeXml(inValue);
map.put("invalue",xmlStr);
map.put("retcode","");
map.put("retmsg","");
switch (type){
case LisinterfaceNameConstants.GETREQINTERFACE:
lisInterfaceMapper.sp_lis_getreqinterface(map);
break;
case LisinterfaceNameConstants.SETDICT:
lisInterfaceMapper.sp_lis_setdict(map);
break;
case LisinterfaceNameConstants.SETREPORT:
lisInterfaceMapper.sp_lis_setreport(map);
break;
case LisinterfaceNameConstants.SETREQSTATUS:
lisInterfaceMapper.sp_lis_setreqstatus(map);
break;
case LisinterfaceNameConstants.DAYMESSAGE:
lisInterfaceMapper.sp_lis_daymessage(map);
break;
}
String retCode = String.valueOf(map.get("retcode"));
String retMsg = String.valueOf(map.get("retmsg"));
log.info("调用存储过程{}返回值:{},{}",type,retCode,retMsg);
if(retCode.equals("0")){
return new Result("0","调用接口成功!",retMsg);
}else{
return new Result(retCode,"调用接口失败!",retMsg);
}
}
/**
* http方式调用接口
* @param transCode
* @param jsonStr
* @return
*/
private Result invokeHttp(String transCode,String jsonStr){
String interfaceUrl = RuoYiConfig.getInterfaceUrl();
if(interfaceUrl == null || "".equals(interfaceUrl)) return new Result("-1","没有配置地址,请检查interfaceUrl配置项!!!");
interfaceUrl = interfaceUrl+"/"+transCode;
String retMsg = "";
try {
HttpRequest httpRequest = HttpRequest.post(interfaceUrl);
httpRequest.header("Content-Type","application/json");
HttpResponse httpResponse = httpRequest.body(jsonStr).execute();
int status = httpResponse.getStatus();
retMsg = httpResponse.body();
if (status != 200) {
log.info("调用http接口失败:"+retMsg);
return new Result("-1","调用http接口失败:"+retMsg);
}
}catch (Exception e){
e.printStackTrace();
log.error("调用lis的http接口出错:",e);
return new Result("-1","调用http接口失败:"+e.getMessage());
}
log.info("调用http接口成功类型:{},入参:{}/r/n,出参:{}",transCode,jsonStr,retMsg);
String message="";
String resultCode="";
// 检查输入是否为空
if (JSONUtil.isJson(retMsg)) {
JSONObject json = JSONUtil.parseObj(retMsg);
// 使用安全的方式获取值
message = json.getStr("message");
resultCode= json.getStr("resultCode");
} else {
// 处理非JSON格式的情况
}
return new Result(resultCode,"调用接口成功!",message);
}
/**
* 把json格式转换成xml格式
* @param inValue
* @return
*/
public String changeXml(String inValue){
JSONObject jsonObj = JSONUtil.parseObj(inValue);
Map<String,Object> map = jsonObj.toBean(Map.class);
return XmlUtil.mapToXmlStr(map, "Request");
}
}