/******************************************************************************
* 文件名称: InvokeHelper.cs
* 文件说明: 调用助手,调用方法的封装
* 当前版本: V1.0
* 创建日期: 2022-04-12
*
* 2020-04-12: 增加 businessDLLInvoke 方法
* 2020-04-12: 增加 writeLog 方法
* 2020-04-14: 增加 businessDLLInvoke(重载) 方法
* 2020-04-14: 增加 irisServiceInvoke 方法
******************************************************************************/
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PTMedicalInsurance.Helper;
using Newtonsoft.Json;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Variables;
using System.Runtime.InteropServices;
using PTMedicalInsurance.Forms;
using System.Security.Cryptography;
using Org.BouncyCastle.Utilities.Encoders;
namespace PTMedicalInsurance.Helper
{
public class InvokeHelper
{
private string serviceURL;
private string authorization;
public InvokeHelper()
{
LoadCenterURL(false);
}
private void LoadCenterURL(bool reloadFromCfgFile)
{
//IniFile ini = new IniFile(Global.curEvt.path + @"\CenterServiceURL.ini");
//// 默认采用的是后台配置的地址
//if (reloadFromCfgFile)
//{
// Global.inf.centerURL = ini.ReadValue("CENTER", "url");
// Global.inf.uploadURL = ini.ReadValue("CENTER", "upload");
// Global.inf.downURL = ini.ReadValue("CENTER", "download");
// Global.inf.ecURL = ini.ReadValue("CENTER", "ecToken");
//}
//Global.inf.mobilePayURL = ini.ReadValue("CENTER", "mobilePay");
//Global.inf.ecPrescURL = ini.ReadValue("CENTER", "prescription");
//// 移动支付
//if (string.IsNullOrEmpty(Global.inf.mobilePayURL))
//{
// Global.inf.mobilePayURL = "http://10.77.224.18:9001/CSB/hsa-fsi-no";
//}
//// 电子处方
//if (string.IsNullOrEmpty(Global.inf.ecPrescURL))
//{
// Global.inf.ecPrescURL = "http://10.77.245.22:9001/epc/api";
//}
}
/// <summary>
/// API密钥
/// </summary>
private void LoadAPIKey(string prefix)
{
//获取AK,SK(可配置)
if (prefix == "MOBILE")
{
Global.inf.passid = Global.inf.MobilePay.passid;
Global.inf.apiSecretKey = Global.inf.MobilePay.apiSecretKey;
}
else
{
byte[] b = Base64.Decode(Global.inf.centerURL2);
string centerURL2 = System.Text.Encoding.UTF8.GetString(b);
JObject joCenterURL2 = JObject.Parse(centerURL2);
Global.inf.passid = JsonHelper.getDestValue(joCenterURL2, "core.passid");
Global.inf.apiSecretKey = JsonHelper.getDestValue(joCenterURL2, "core.apiSecretKey");
}
}
/// <summary>
/// 核心接口密钥
/// </summary>
private void LoadCoreAPIKey()
{
LoadAPIKey("KEY");
}
/// <summary>
/// 移动支付密钥
/// </summary>
private void LoadMobileAPIKey()
{
LoadAPIKey("MOBILE");
}
#region 内部服务调用
/// <summary>
/// iris服务调用的封装
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public JObject invokeIrisService(string data, string serviceDesc)
{
string rtn = "", url = "";
JObject joRtn = new JObject();
try
{
//先根据用户请求的uri构造请求地址
url = serviceURL;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
//创建Web访问对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
//把用户传过来的数据转成“UTF-8”的字节流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
//添加头部信息
myRequest.Method = "POST";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.Headers.Add("Authorization", authorization);
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//发送请求
Stream stream = myRequest.GetRequestStream();
stream.Write(buf, 0, buf.Length);
stream.Close();
//获取接口返回值
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
rtn = getResponseData(myResponse);
joRtn = JObject.Parse(rtn);
return joRtn;
}
catch (Exception ex)
{
joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
rtn = JsonConvert.SerializeObject(joRtn);
return joRtn;
}
}
/// <summary>
/// HIS服务调用的封装
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
///
public JObject invokeHISService(string data, string serviceDesc)
{
JObject joRtn = new JObject();
try
{
//先根据用户请求的uri构造请求地址
serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
authorization = Global.hisConfig.authorization;
joRtn = invokeIrisService(data, serviceDesc);
return joRtn;
}
catch (Exception ex)
{
joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
return joRtn;
}
finally
{
Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
}
}
/// <summary>
/// 医保平台服务调用的封装
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public JObject invokeInsuService(string data, string serviceDesc)
{
string rtn = "";
JObject joRtn = new JObject();
try
{
//先根据用户请求的uri构造请求地址
serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
authorization = Global.insuConfig.authorization;
joRtn = invokeIrisService(data, serviceDesc);
rtn = JsonConvert.SerializeObject(joRtn);
//if (serviceDesc == "插入签到信息")
//{
// MessageBox.Show("插入签到信息入参:" + data +"|返回值:"+ rtn.ToString()+"|"+ Global.insuConfig.url);
//}
return joRtn;
}
catch (Exception ex)
{
joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
rtn = JsonConvert.SerializeObject(joRtn);
return joRtn;
}
finally
{
Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
}
}
private string getResponseData(HttpWebResponse response)
{
string data = "";
if (response != null)
{
Stream s = response.GetResponseStream();
StreamReader sRead = new StreamReader(s);
data = sRead.ReadToEnd();
sRead.Close();
response.Close();
}
return data;
}
#endregion
#region 医保中心调用
/// <summary>
/// 仅辅助交易、自定义交易使用
/// </summary>
/// <param name="funNo"></param>
/// <param name="joInput"></param>
/// <returns></returns>
public JObject invokeCenterAgentService(string funNo, JObject joInput)
{
if (Global.curEvt.testMode)
{
// 医保测试环境
LoadCenterURL(true);
}
Global.curEvt.URL = Global.inf.centerURL + funNo;
string data = JsonHelper.setCenterInpar(funNo, joInput);
return invokeCenterService(data);
}
private JObject invokeCenterService(string data, ModeEnum mode = ModeEnum.REST)
{
JObject joRtn = new JObject();
try
{
IInvokeCenter center = new InvokeCenterFactory().create(mode);
string outputData = "";
string errMsg = "";
int iInt = center.Init(ref errMsg);
if (iInt == 0)
{
LoadCoreAPIKey();
iInt = center.Business(data, ref outputData, ref errMsg);
if (iInt == 0 && !string.IsNullOrEmpty(outputData))
{
try
{
joRtn = JObject.Parse(outputData);
}
catch (Exception ex)
{
joRtn.Add("infcode", iInt);
joRtn.Add("err_msg", "返回参数异常:" + outputData);
}
}
else
{
joRtn.Add("infcode", iInt);
joRtn.Add("err_msg", outputData);
return joRtn;
}
return joRtn;
}
else
{
joRtn.Add("infcode", -1);
joRtn.Add("err_msg", "医保动态库初始化失败invokeInitByDLL:" + errMsg);
return joRtn;
}
}
finally
{
Global.writeLog(Global.curEvt.URL, JsonHelper.Compress(data), joRtn.ToString());
// 保存到服务器
this.saveCenterLog(JsonHelper.Compress(data),joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
}
}
private void prepareCallURI(TradeEnum trade)
{
string funNo = trade.GetCode() ;
Global.curEvt.funNo = funNo;
if (Global.curEvt.testMode)
{
// 医保测试环境
//LoadCenterURL(true);
}
string prefix = Global.inf.centerURL;
switch (trade)
{
case TradeEnum.FileUpload:
prefix = Global.inf.uploadURL??Global.inf.centerURL;
break;
case TradeEnum.FileDownload:
prefix = Global.inf.centerURL;
break;
default:
prefix = Global.inf.centerURL;
break;
}
// 根据情况确实是否需要加funNo
Global.curEvt.URL = prefix + Global.curEvt.funNo;
}
/// <summary>
/// 通过枚举转换要调用的接口
/// </summary>
/// <param name="trade"></param>
/// <param name="data"></param>
/// <returns></returns>
public JObject invokeCenterService(TradeEnum trade, JObject joInput)
{
string funNo = trade.GetCode();
// 入参统一转换
JObject request = Utils.ConvertRequest<JObject>(trade,joInput);
string data = JsonHelper.toJsonString(request);
// 统一封装请求头
if (trade.GetMode() == ModeEnum.REST)
{
data = JsonHelper.setCenterInpar(funNo, request);
}
JObject joRtn = new JObject();
// 调试模式
if (Global.curEvt.enabledDebug)
{
CenterResult center = new CenterResult();
center.setTradeNo(trade.GetCode(), data);
if (center.ShowDialog() == DialogResult.OK)
{
// 接口实际返回数据
string outPar = center.returnData;
joRtn = JObject.Parse(outPar);
}
}
else
{
prepareCallURI(trade);
joRtn = invokeCenterService(data, trade.GetMode());
}
// 返回结果统一转换
joRtn = Utils.ConvertResponse<JObject>(trade,joRtn);
return joRtn;
}
/// <summary>
/// 医保目录txt文件下载
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public JObject DownloadCenterFile(string data, ModeEnum mode = ModeEnum.REST)
{
//download file
IInvokeCenter center = new InvokeCenterFactory().create(mode);
string outputMsg = "";
JObject joRtn = new JObject();
prepareCallURI(TradeEnum.FileDownload);
int rtnCode = center.DownloadFile(data, ref outputMsg);
if(rtnCode==0)
{
joRtn = JObject.Parse(outputMsg);
}
else
{
joRtn.Add("infcode", -1);
joRtn.Add("err_msg", "下载文件失败DownloadFile:" + outputMsg);
}
return joRtn;
}
#endregion
#region 移动
/// <summary>
/// 移动
/// </summary>
/// <param name="funNO"></param>
/// <param name="data"></param>
/// <returns></returns>
public JObject invokeMPService(string funNO, string data)
{
return invokeMPService(funNO, JObject.Parse(data));
}
public JObject invokeMPService(string funNO, JObject joInput)
{
JObject joRtn = new JObject();
String outPar = "";
string decData = "";
string encInput = "";
string outputData = "", errMsg = "";
try
{
string url = "";
switch (funNO)
{
case "6201":
url = "/org/local/api/hos/uldFeeInfo";
break;
case "6202":
url = "/org/local/api/hos/pay_order";
break;
case "6203":
url = "/org/local/api/hos/refund_Order";
break;
case "6301":
url = "/org/local/api/hos/query_order_info";
break;
case "6401":
url = "/org/local/api/hos/revoke_order";
break;
default:
break;
}
EncryptHelper encrypt = new EncryptHelper(Global.inf.MobilePay.appid, Global.inf.MobilePay.secretKey, Global.inf.MobilePay.publicKey, Global.inf.MobilePay.privateKey);
encInput = JsonHelper.setMPCenterInpar(funNO, joInput);
// 移动支付地址
Global.curEvt.URL = Global.inf.MobilePay.url + url;
// 动态调试模式
if (Global.curEvt.enabledDebug)
{
CenterResult center = new CenterResult();
center.setTradeNo(funNO, encInput);
if (center.ShowDialog() == DialogResult.OK)
{
outPar = center.returnData;
return JObject.Parse(outPar);
}
}
try
{
InvokeRestCenter mobileCenter = new InvokeRestCenter();
LoadMobileAPIKey();
int iInt = mobileCenter.Business(encInput, ref outputData, ref errMsg);
joRtn = JObject.Parse(outputData);
string encData = JsonHelper.getDestValue(joRtn, "encData");
string signData = JsonHelper.getDestValue(joRtn, "signData");
if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
{
joRtn.Remove("encData");
joRtn.Remove("signData");
joRtn.Remove("data");
//解密
decData = encrypt.decrypt(encData);
// 验签
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
FloatParseHandling = FloatParseHandling.Decimal
};
joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
bool rtn = encrypt.verify(joRtn, signData);
if (rtn)
{
joRtn = JObject.Parse(decData);
joRtn.Add("success", "True");
}
else
{
Global.writeLog("验签失败,请核查!");
}
}
return joRtn;
}
finally
{
string plainData = encInput;
this.saveCenterLog(JsonHelper.Compress(encInput), joRtn.ToString(), JsonHelper.Compress(plainData), joRtn.ToString());
}
}
catch (Exception ex)
{
if (joRtn["infcode"] == null)
{ joRtn.Add("infcode", -1); }
if (joRtn["err_msg"] == null)
{ joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
outPar = JsonHelper.Compress(joRtn);
return joRtn;
}
finally
{
Global.writeLog(funNO + "(密文)(" + Global.curEvt.URL + ")", encInput, outputData);
Global.writeLog(funNO + "(明文)(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());
this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());
}
}
/// <summary>
/// 保存中心交易日志到数据库
/// </summary>
/// <param name="inParam"></param>
/// <param name="outParam"></param>
/// <param name="inParamPlain"></param>
/// <param name="outParamPlain"></param>
private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
{
dynamic joIris = new JObject();
string sRtn = "";
try
{
//解析postContent,插入医保交易日志表
JObject joInParam = new JObject(JObject.Parse(inParam));
//解包
JObject joIn = Utils.removeWrapper(joInParam);
JObject joOut = new JObject(JObject.Parse(outParam));
JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
JArray jaParams = new JArray();
JObject joParam = new JObject();
joParam.Add("inParam", JObject.FromObject(joIn));
joParam.Add("outParam", JObject.FromObject(joOut));
joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
joParam.Add("HospitalDr", Global.inf.hospitalDr);
joParam.Add("InterfaceDr", Global.inf.interfaceDr);
joParam.Add("updateUserID", Global.user.ID);
joParam.Add("psn_no", Global.pat.psn_no);
jaParams.Add(joParam);
joIris.code = "09010021";
joIris.Add("params", jaParams);
//InvokeHelper invoker = new InvokeHelper();
sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
}
catch (Exception ex)
{
sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
}
}
#endregion
#region 电子处方
/// </summary>
/// <param name="funNO"></param>
/// <param name="data"></param>
/// <returns></returns>
public JObject invokeEPCenterService(string funNO, string data)
{
JObject joRtn = new JObject();
string outPar = "";
try
{
Global.curEvt.URL = Global.inf.centerURL;
switch (funNO)
{
case "7101":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG001";
break;
}
case "7102":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG002";
break;
}
case "7103":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG003";
break;
}
case "7104":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG004";
break;
}
case "7105":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG005";
break;
}
case "7106":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG006";
break;
}
case "7107":
{
Global.curEvt.URL = Global.curEvt.URL + "CFYLJG007";
break;
}
//case "7108":
// {
// Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxChkInfoCallback";
// break;
// }
//case "7109":
// {
// Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxSetlInfoCallback";
// break;
// }
}
//Global.curEvt.URL = Global.inf.centerURL;
joRtn = invokeCenterService(data);
outPar = JsonHelper.Compress(joRtn);
return joRtn;
}
catch (Exception ex)
{
if (joRtn["infcode"] == null)
{ joRtn.Add("infcode", -1); }
if (joRtn["err_msg"] == null)
{ joRtn.Add("err_msg", "invokeCenterServicePresCir(3):" + ex.Message); }
outPar = JsonHelper.Compress(joRtn);
return joRtn;
}
finally
{
Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
//this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
}
}
/// <summary>
/// 医保电子处方流转调用服务
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
//private JObject invokeEPCenterService(string data)
//{
// string postContent = "";
// JObject joRtn = new JObject();
// try
// {
// string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
// string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
// //内容类型
// //Signer signer = new Signer();
// //signer.Key = Global.inf.privateKey; //应用编码
// //signer.Secret = Global.inf.secretKey; //secretKey 私钥
// //HttpRequest Resquest = new HttpRequest("POST", new Uri(Global.curEvt.URL));
// //Resquest.headers.Add("charset", "UTF-8");
// //Resquest.headers.Add("x-hw-id", signer.Key);
// //Resquest.headers.Add("x-tif-timestamp", timestamp);
// //Resquest.headers.Add("x-tif-passid", signer.Key);
// //Resquest.headers.Add("x-tif-nonce", nonce);
// //Resquest.body = signData;
// //HttpWebRequest req = signer.Sign(Resquest);
// HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
// req.Method = "POST";
// req.ContentType = "application/json;charset=utf8";
// req.Timeout = 5 * 10000;
// try
// {
// var writer = new StreamWriter(req.GetRequestStream());
// writer.Write(data);
// writer.Flush();
// HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// StreamReader reader = new StreamReader(resp.GetResponseStream());
// string RtnStr = reader.ReadToEnd();
// joRtn = JObject.Parse(RtnStr);
// // 解密返回值
// EncryptHelper encrypt = new EncryptHelper(Global.inf.appId, Global.inf.secretKey, Global.inf.publicKey, Global.inf.privateKey);
// string encData = JsonHelper.getDestValue(joRtn, "encData");
// string signData = JsonHelper.getDestValue(joRtn, "signData");
// if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
// {
// joRtn.Remove("encData");
// joRtn.Remove("signData");
// joRtn.Remove("data");
// //解密
// string decData = encrypt.decrypt(encData);
// // 验签
// JsonConvert.DefaultSettings = () => new JsonSerializerSettings
// {
// FloatParseHandling = FloatParseHandling.Decimal
// };
// joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
// bool rtn = encrypt.verify(joRtn, signData);
// if (rtn)
// {
// Global.writeLog("【明文出参】:\r\n" + decData);
// joRtn = JObject.Parse(decData);
// joRtn.Add("code", 0);
// joRtn.Add("message", "成功");
// }
// else
// {
// Global.writeLog("验签失败,请核查!");
// }
// }
// return joRtn;
// }
// catch (WebException e)
// {
// HttpWebResponse resp = (HttpWebResponse)e.Response;
// if (resp != null)
// {
// return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(a)", new StreamReader(resp.GetResponseStream()).ReadToEnd() + "异常内容:" + e.Message);
// }
// else
// {
// return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(b)", e.Message);
// }
// }
// }
// catch (Exception ex)
// {
// postContent = "调用中心服务异常" + ex.Message;
// joRtn.Add("infcode", -1);
// joRtn.Add("err_msg", "invokeCenterService(Exception_Last):" + ex.Message);
// return joRtn;
// }
//}
#endregion
}
}