123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- /******************************************************************************
- * 文件名称: InvokeHelper.cs
- * 文件说明: 调用助手,调用方法的封装
- * 当前版本: V1.0
- * 创建日期: 2022-04-12
- *
- * 2020-04-12: 增加 businessDLLInvoke 方法
- * 2020-04-12: 增加 writeLog 方法
- * 2020-04-14: 增加 businessDLLInvoke(重载) 方法
- * 2020-04-14: 增加 irisServiceInvoke 方法
- * 2022-05-16: 增加 invokeInsuService 方法
- * 2022-05-16: 增加 invokeHISService 方法
- ******************************************************************************/
- using Newtonsoft.Json;
- 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;
- namespace DirectoryDownload.Helper
- {
- class InvokeHelper
- {
- /// <summary>
- /// 封装业务DLL的调用,返回整型,带两ref出参
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="inParam"></param>
- /// <param name="outParam"></param>
- /// <param name="ErrorMessage"></param>
- /// <returns></returns>
- public int businessDLLInvoke(string methodName, string inParam, ref string outParam,ref string ErrorMessage)
- {
- try
- {
- DllInvoke dllInvoke = new DllInvoke();
- string path = GlobalVariables.currentDirectory + "\\" + GlobalVariables.businessDllName;
- string[] str = new string[1];
- str[0] = inParam;
- object o2 = DllInvoke.ManagedInvoke(path, GlobalVariables.businessDllNameSpace, "InsuBusiness", methodName, str);
- if (o2.ToString() == "")
- {
- ErrorMessage = "返回数据为空";
- return -1;
- }
- JObject joRtn = JObject.Parse(o2.ToString());
-
- if (joRtn["errorCode"].ToString() != "0")
- {
- ErrorMessage = joRtn["errorMessage"].ToString();
- return -1;
- }
- else
- {
- outParam = o2.ToString();
- return 0;
- }
-
- }
- catch (Exception ex)
- {
- ErrorMessage = "调用异常:" + ex.ToString();
- return -1;
- }
- finally
- {
- writeLog(methodName,inParam,outParam);
- }
- }
- /// <summary>
- /// 封装业务DLL的调用,返回JSON对象
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="inParam"></param>
- /// <returns></returns>
- public JObject businessDLLInvoke(string methodName, string inParam)
- {
- string outParam = string.Empty; string path = "";
- JObject joRtn = new JObject();
- try
- {
- DllInvoke dllInvoke = new DllInvoke();
- path = GlobalVariables.currentDirectory + "\\" + GlobalVariables.businessDllName;
- string[] str = new string[1];
- str[0] = inParam;
- object o2 = DllInvoke.ManagedInvoke(path, GlobalVariables.businessDllNameSpace, "InsuBusiness", methodName, str);
- outParam = o2.ToString();
- joRtn = JObject.Parse(outParam);
-
- return joRtn;
- }
- catch (Exception ex)
- {
- outParam = "businessDLLInvoke:原出参(" + outParam + ")\r\n ,异常:" + ex.Message;
-
- return JsonHelper.getIrisExceptionJson(-1, "businessDLLInvoke:" ,outParam);
- }
- finally
- {
- writeLog(methodName + "(" + path + ")", inParam, outParam);
- }
- }
- /// <summary>
- /// 调用壳DLL
- /// </summary>
- /// <param name="methodName"></param>
- /// <param name="inParam"></param>
- /// <returns></returns>
- public JObject invokeShellDll(string methodName, string inParam)
- {
- string outParam = string.Empty; string path = "";
- JObject joRtn = new JObject();
- try
- {
- DllInvoke dllInvoke = new DllInvoke();
- path = GlobalVariables.currentDirectory + "\\" + "PTMedInsuInterface.dll";
- string[] str = new string[1];
- str[0] = inParam;
- object o2 = DllInvoke.ManagedInvoke(path, "PTMedInsuInterface", "InsuBusiness", methodName, str);
- outParam = o2.ToString();
- joRtn = JObject.Parse(outParam);
- return joRtn;
- }
- catch (Exception ex)
- {
- outParam = "invokeShellDll:原出参(" + outParam + ")\r\n ,异常:" + ex.Message;
- return JsonHelper.getIrisExceptionJson(-1, "invokeShellDll:", outParam);
- }
- finally
- {
- writeLog(methodName + "(" + path + ")", inParam, outParam);
- }
- }
- /// <summary>
- /// iris服务调用的封装
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public JObject irisServiceInvoke(string data,string serviceName)
- {
- string rtn = "", url = "";
- JObject joRtn = new JObject();
- try
- {
- //先根据用户请求的uri构造请求地址
- url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
- 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", GlobalVariables.irisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
- //发送请求
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
- //获取接口返回值
- //通过Web访问对象获取响应内容
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
- //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
- rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
- reader.Close();
- myResponse.Close();
- joRtn = JObject.Parse(rtn);
- return joRtn;
- }
- catch (Exception ex)
- {
- joRtn = JsonHelper.getIrisExceptionJson(-1, serviceName,rtn + "\r\n" + ex.Message);
- rtn = joRtn.ToString();
- return joRtn;
- }
- finally
- {
- writeLog(serviceName+ url , data, rtn);
- }
- }
- /// <summary>
- /// iris服务调用的封装
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public JObject invokeHISService(string data, string serviceDesc)
- {
- string rtn = "", url = "";
- JObject joRtn = new JObject();
- try
- {
- //先根据用户请求的uri构造请求地址
- url = string.Format("{0}/{1}", GlobalVariables.hisIrisServiceIP, GlobalVariables.hisIrisServiceURL);
- 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", GlobalVariables.hisIrisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
- //发送请求
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
- //获取接口返回值
- //通过Web访问对象获取响应内容
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
- //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
- rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
- reader.Close();
- myResponse.Close();
- joRtn = JObject.Parse(rtn);
- return joRtn;
- }
- catch (Exception ex)
- {
- joRtn = JsonHelper.getIrisExceptionJson(-1, serviceDesc, ex.Message);
- rtn = JsonConvert.SerializeObject(joRtn);
- return joRtn;
- }
- finally
- {
- GlobalVariables.writeLog(serviceDesc + "(" + url + ")", data, rtn);
- }
- }
- /// <summary>
- /// iris服务调用的封装
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public JObject invokeInsuService(string data, string serviceDesc)
- {
- string rtn = "", url = "";
- JObject joRtn = new JObject();
- try
- {
- //先根据用户请求的uri构造请求地址
- url = string.Format("{0}/{1}", GlobalVariables.insuIrisServiceIP, GlobalVariables.insuIrisServiceURL);
- 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", GlobalVariables.insuIrisServiceAuthorization);
- myRequest.MaximumAutomaticRedirections = 1;
- myRequest.AllowAutoRedirect = true;
- //发送请求
- Stream stream = myRequest.GetRequestStream();
- stream.Write(buf, 0, buf.Length);
- stream.Close();
- //获取接口返回值
- //通过Web访问对象获取响应内容
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
- //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
- rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
- reader.Close();
- myResponse.Close();
- joRtn = JObject.Parse(rtn);
- return joRtn;
- }
- catch (Exception ex)
- {
- joRtn = JsonHelper.getIrisExceptionJson(-1, serviceDesc, ex.Message);
- rtn = JsonConvert.SerializeObject(joRtn);
- return joRtn;
- }
- finally
- {
- GlobalVariables.writeLog(serviceDesc + "(" + url + ")", data, rtn);
- }
- }
- /// <summary>
- /// 一次交易的完整日志记录,用*****分割每次交易,简洁明了。
- /// </summary>
- /// <param name="tradeName"></param>
- /// <param name="inParam"></param>
- /// <param name="outParam"></param>
- private void writeLog(string tradeName, string inParam, string outParam)
- {
- string logDir = GlobalVariables.currentDirectory + "\\Log", logName = DateTime.Now.ToString("yyyy-MM-dd") + "_Demo.Log";
- string statrTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff");
- if (!Directory.Exists(logDir))
- {
- //创建文件夹
- DirectoryInfo dirInfo = Directory.CreateDirectory(logDir);
- }
- if (!File.Exists(logDir + "\\" + logName))
- {
- FileStream fs1 = File.Create(logDir + "\\" + logName);
- fs1.Close();
- }
- FileStream fs = new FileStream(logDir + "\\" + logName, FileMode.Append, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs);
- string content = "****************************交易开始(" + statrTime + ")****************************" + "\r\n";
- content = content + "交易名称:" + tradeName + "\r\n";
- content = content + "交易入参:" + inParam + "\r\n";
- content = content + "交易出参:" + outParam + "\r\n";
- content = content + "****************************交易结束(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + ")****************************" + "\r\n";
- sw.WriteLine(content);
- sw.Close();
- fs.Close();
- }
- }
- }
|