InvokeHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /******************************************************************************
  2. * 文件名称: InvokeHelper.cs
  3. * 文件说明: 调用助手,调用方法的封装
  4. * 当前版本: V1.0
  5. * 创建日期: 2022-04-12
  6. *
  7. * 2020-04-12: 增加 businessDLLInvoke 方法
  8. * 2020-04-12: 增加 writeLog 方法
  9. * 2020-04-14: 增加 businessDLLInvoke(重载) 方法
  10. * 2020-04-14: 增加 irisServiceInvoke 方法
  11. * 2022-05-16: 增加 invokeInsuService 方法
  12. * 2022-05-16: 增加 invokeHISService 方法
  13. ******************************************************************************/
  14. using Newtonsoft.Json;
  15. using Newtonsoft.Json.Linq;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Net;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows.Forms;
  24. namespace DirectoryDownload.Helper
  25. {
  26. class InvokeHelper
  27. {
  28. /// <summary>
  29. /// 封装业务DLL的调用,返回整型,带两ref出参
  30. /// </summary>
  31. /// <param name="methodName"></param>
  32. /// <param name="inParam"></param>
  33. /// <param name="outParam"></param>
  34. /// <param name="ErrorMessage"></param>
  35. /// <returns></returns>
  36. public int businessDLLInvoke(string methodName, string inParam, ref string outParam,ref string ErrorMessage)
  37. {
  38. try
  39. {
  40. DllInvoke dllInvoke = new DllInvoke();
  41. string path = GlobalVariables.currentDirectory + "\\" + GlobalVariables.businessDllName;
  42. string[] str = new string[1];
  43. str[0] = inParam;
  44. object o2 = DllInvoke.ManagedInvoke(path, GlobalVariables.businessDllNameSpace, "InsuBusiness", methodName, str);
  45. if (o2.ToString() == "")
  46. {
  47. ErrorMessage = "返回数据为空";
  48. return -1;
  49. }
  50. JObject joRtn = JObject.Parse(o2.ToString());
  51. if (joRtn["errorCode"].ToString() != "0")
  52. {
  53. ErrorMessage = joRtn["errorMessage"].ToString();
  54. return -1;
  55. }
  56. else
  57. {
  58. outParam = o2.ToString();
  59. return 0;
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. ErrorMessage = "调用异常:" + ex.ToString();
  65. return -1;
  66. }
  67. finally
  68. {
  69. writeLog(methodName,inParam,outParam);
  70. }
  71. }
  72. /// <summary>
  73. /// 封装业务DLL的调用,返回JSON对象
  74. /// </summary>
  75. /// <param name="methodName"></param>
  76. /// <param name="inParam"></param>
  77. /// <returns></returns>
  78. public JObject businessDLLInvoke(string methodName, string inParam)
  79. {
  80. string outParam = string.Empty; string path = "";
  81. JObject joRtn = new JObject();
  82. try
  83. {
  84. DllInvoke dllInvoke = new DllInvoke();
  85. path = GlobalVariables.currentDirectory + "\\" + GlobalVariables.businessDllName;
  86. string[] str = new string[1];
  87. str[0] = inParam;
  88. object o2 = DllInvoke.ManagedInvoke(path, GlobalVariables.businessDllNameSpace, "InsuBusiness", methodName, str);
  89. outParam = o2.ToString();
  90. joRtn = JObject.Parse(outParam);
  91. return joRtn;
  92. }
  93. catch (Exception ex)
  94. {
  95. outParam = "businessDLLInvoke:原出参(" + outParam + ")\r\n ,异常:" + ex.Message;
  96. return JsonHelper.getIrisExceptionJson(-1, "businessDLLInvoke:" ,outParam);
  97. }
  98. finally
  99. {
  100. writeLog(methodName + "(" + path + ")", inParam, outParam);
  101. }
  102. }
  103. /// <summary>
  104. /// 调用壳DLL
  105. /// </summary>
  106. /// <param name="methodName"></param>
  107. /// <param name="inParam"></param>
  108. /// <returns></returns>
  109. public JObject invokeShellDll(string methodName, string inParam)
  110. {
  111. string outParam = string.Empty; string path = "";
  112. JObject joRtn = new JObject();
  113. try
  114. {
  115. DllInvoke dllInvoke = new DllInvoke();
  116. path = GlobalVariables.currentDirectory + "\\" + "PTMedInsuInterface.dll";
  117. string[] str = new string[1];
  118. str[0] = inParam;
  119. object o2 = DllInvoke.ManagedInvoke(path, "PTMedInsuInterface", "InsuBusiness", methodName, str);
  120. outParam = o2.ToString();
  121. joRtn = JObject.Parse(outParam);
  122. return joRtn;
  123. }
  124. catch (Exception ex)
  125. {
  126. outParam = "invokeShellDll:原出参(" + outParam + ")\r\n ,异常:" + ex.Message;
  127. return JsonHelper.getIrisExceptionJson(-1, "invokeShellDll:", outParam);
  128. }
  129. finally
  130. {
  131. writeLog(methodName + "(" + path + ")", inParam, outParam);
  132. }
  133. }
  134. /// <summary>
  135. /// iris服务调用的封装
  136. /// </summary>
  137. /// <param name="data"></param>
  138. /// <returns></returns>
  139. public JObject irisServiceInvoke(string data,string serviceName)
  140. {
  141. string rtn = "", url = "";
  142. JObject joRtn = new JObject();
  143. try
  144. {
  145. //先根据用户请求的uri构造请求地址
  146. url = string.Format("{0}/{1}", GlobalVariables.irisServiceIP, GlobalVariables.irisServiceURL);
  147. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  148. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  149. //创建Web访问对象
  150. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  151. //把用户传过来的数据转成“UTF-8”的字节流
  152. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  153. //添加头部信息
  154. myRequest.Method = "POST";
  155. myRequest.ContentLength = buf.Length;
  156. myRequest.ContentType = "application/json";
  157. myRequest.Headers.Add("Authorization", GlobalVariables.irisServiceAuthorization);
  158. myRequest.MaximumAutomaticRedirections = 1;
  159. myRequest.AllowAutoRedirect = true;
  160. //发送请求
  161. Stream stream = myRequest.GetRequestStream();
  162. stream.Write(buf, 0, buf.Length);
  163. stream.Close();
  164. //获取接口返回值
  165. //通过Web访问对象获取响应内容
  166. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  167. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  168. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  169. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  170. rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  171. reader.Close();
  172. myResponse.Close();
  173. joRtn = JObject.Parse(rtn);
  174. return joRtn;
  175. }
  176. catch (Exception ex)
  177. {
  178. joRtn = JsonHelper.getIrisExceptionJson(-1, serviceName,rtn + "\r\n" + ex.Message);
  179. rtn = joRtn.ToString();
  180. return joRtn;
  181. }
  182. finally
  183. {
  184. writeLog(serviceName+ url , data, rtn);
  185. }
  186. }
  187. /// <summary>
  188. /// iris服务调用的封装
  189. /// </summary>
  190. /// <param name="data"></param>
  191. /// <returns></returns>
  192. public JObject invokeHISService(string data, string serviceDesc)
  193. {
  194. string rtn = "", url = "";
  195. JObject joRtn = new JObject();
  196. try
  197. {
  198. //先根据用户请求的uri构造请求地址
  199. url = string.Format("{0}/{1}", GlobalVariables.hisIrisServiceIP, GlobalVariables.hisIrisServiceURL);
  200. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  201. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  202. //创建Web访问对象
  203. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  204. //把用户传过来的数据转成“UTF-8”的字节流
  205. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  206. //添加头部信息
  207. myRequest.Method = "POST";
  208. myRequest.ContentLength = buf.Length;
  209. myRequest.ContentType = "application/json";
  210. myRequest.Headers.Add("Authorization", GlobalVariables.hisIrisServiceAuthorization);
  211. myRequest.MaximumAutomaticRedirections = 1;
  212. myRequest.AllowAutoRedirect = true;
  213. //发送请求
  214. Stream stream = myRequest.GetRequestStream();
  215. stream.Write(buf, 0, buf.Length);
  216. stream.Close();
  217. //获取接口返回值
  218. //通过Web访问对象获取响应内容
  219. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  220. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  221. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  222. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  223. rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  224. reader.Close();
  225. myResponse.Close();
  226. joRtn = JObject.Parse(rtn);
  227. return joRtn;
  228. }
  229. catch (Exception ex)
  230. {
  231. joRtn = JsonHelper.getIrisExceptionJson(-1, serviceDesc, ex.Message);
  232. rtn = JsonConvert.SerializeObject(joRtn);
  233. return joRtn;
  234. }
  235. finally
  236. {
  237. GlobalVariables.writeLog(serviceDesc + "(" + url + ")", data, rtn);
  238. }
  239. }
  240. /// <summary>
  241. /// iris服务调用的封装
  242. /// </summary>
  243. /// <param name="data"></param>
  244. /// <returns></returns>
  245. public JObject invokeInsuService(string data, string serviceDesc)
  246. {
  247. string rtn = "", url = "";
  248. JObject joRtn = new JObject();
  249. try
  250. {
  251. //先根据用户请求的uri构造请求地址
  252. url = string.Format("{0}/{1}", GlobalVariables.insuIrisServiceIP, GlobalVariables.insuIrisServiceURL);
  253. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  254. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  255. //创建Web访问对象
  256. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  257. //把用户传过来的数据转成“UTF-8”的字节流
  258. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  259. //添加头部信息
  260. myRequest.Method = "POST";
  261. myRequest.ContentLength = buf.Length;
  262. myRequest.ContentType = "application/json";
  263. myRequest.Headers.Add("Authorization", GlobalVariables.insuIrisServiceAuthorization);
  264. myRequest.MaximumAutomaticRedirections = 1;
  265. myRequest.AllowAutoRedirect = true;
  266. //发送请求
  267. Stream stream = myRequest.GetRequestStream();
  268. stream.Write(buf, 0, buf.Length);
  269. stream.Close();
  270. //获取接口返回值
  271. //通过Web访问对象获取响应内容
  272. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  273. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  274. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  275. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  276. rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  277. reader.Close();
  278. myResponse.Close();
  279. joRtn = JObject.Parse(rtn);
  280. return joRtn;
  281. }
  282. catch (Exception ex)
  283. {
  284. joRtn = JsonHelper.getIrisExceptionJson(-1, serviceDesc, ex.Message);
  285. rtn = JsonConvert.SerializeObject(joRtn);
  286. return joRtn;
  287. }
  288. finally
  289. {
  290. GlobalVariables.writeLog(serviceDesc + "(" + url + ")", data, rtn);
  291. }
  292. }
  293. /// <summary>
  294. /// 一次交易的完整日志记录,用*****分割每次交易,简洁明了。
  295. /// </summary>
  296. /// <param name="tradeName"></param>
  297. /// <param name="inParam"></param>
  298. /// <param name="outParam"></param>
  299. private void writeLog(string tradeName, string inParam, string outParam)
  300. {
  301. string logDir = GlobalVariables.currentDirectory + "\\Log", logName = DateTime.Now.ToString("yyyy-MM-dd") + "_Demo.Log";
  302. string statrTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff");
  303. if (!Directory.Exists(logDir))
  304. {
  305. //创建文件夹
  306. DirectoryInfo dirInfo = Directory.CreateDirectory(logDir);
  307. }
  308. if (!File.Exists(logDir + "\\" + logName))
  309. {
  310. FileStream fs1 = File.Create(logDir + "\\" + logName);
  311. fs1.Close();
  312. }
  313. FileStream fs = new FileStream(logDir + "\\" + logName, FileMode.Append, FileAccess.Write);
  314. StreamWriter sw = new StreamWriter(fs);
  315. string content = "****************************交易开始(" + statrTime + ")****************************" + "\r\n";
  316. content = content + "交易名称:" + tradeName + "\r\n";
  317. content = content + "交易入参:" + inParam + "\r\n";
  318. content = content + "交易出参:" + outParam + "\r\n";
  319. content = content + "****************************交易结束(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + ")****************************" + "\r\n";
  320. sw.WriteLine(content);
  321. sw.Close();
  322. fs.Close();
  323. }
  324. }
  325. }