InvokeHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. ******************************************************************************/
  12. using Newtonsoft.Json.Linq;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using PTMedicalInsurance.Helper;
  22. using Newtonsoft.Json;
  23. using PTMedicalInsurance.Common;
  24. using PTMedicalInsurance.Variables;
  25. namespace PTMedicalInsurance.Helper
  26. {
  27. class InvokeHelper
  28. {
  29. private string serviceURL;
  30. private string authorization;
  31. /// <summary>
  32. /// iris服务调用的封装
  33. /// </summary>
  34. /// <param name="data"></param>
  35. /// <returns></returns>
  36. public JObject invokeIrisService(string data, string serviceDesc)
  37. {
  38. string rtn = "", url = "";
  39. JObject joRtn = new JObject();
  40. try
  41. {
  42. //先根据用户请求的uri构造请求地址
  43. url = serviceURL;
  44. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  45. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  46. //创建Web访问对象
  47. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  48. //把用户传过来的数据转成“UTF-8”的字节流
  49. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  50. //添加头部信息
  51. myRequest.Method = "POST";
  52. myRequest.ContentLength = buf.Length;
  53. myRequest.ContentType = "application/json";
  54. myRequest.Headers.Add("Authorization", authorization);
  55. myRequest.MaximumAutomaticRedirections = 1;
  56. myRequest.AllowAutoRedirect = true;
  57. //发送请求
  58. Stream stream = myRequest.GetRequestStream();
  59. stream.Write(buf, 0, buf.Length);
  60. stream.Close();
  61. //获取接口返回值
  62. //通过Web访问对象获取响应内容
  63. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  64. //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  65. StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  66. //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  67. rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  68. reader.Close();
  69. myResponse.Close();
  70. joRtn = JObject.Parse(rtn);
  71. return joRtn;
  72. }
  73. catch (Exception ex)
  74. {
  75. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  76. rtn = JsonConvert.SerializeObject(joRtn);
  77. return joRtn;
  78. }
  79. }
  80. /// <summary>
  81. /// HIS服务调用的封装
  82. /// </summary>
  83. /// <param name="data"></param>
  84. /// <returns></returns>
  85. public JObject invokeHISService(string data, string serviceDesc)
  86. {
  87. JObject joRtn = new JObject();
  88. try
  89. {
  90. //先根据用户请求的uri构造请求地址
  91. serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
  92. authorization = Global.hisConfig.authorization;
  93. joRtn = invokeIrisService(data, serviceDesc);
  94. return joRtn;
  95. }
  96. catch (Exception ex)
  97. {
  98. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  99. return joRtn;
  100. }
  101. finally
  102. {
  103. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
  104. }
  105. }
  106. /// <summary>
  107. /// 医保平台服务调用的封装
  108. /// </summary>
  109. /// <param name="data"></param>
  110. /// <returns></returns>
  111. public JObject invokeInsuService(string data, string serviceDesc)
  112. {
  113. string rtn = "";
  114. JObject joRtn = new JObject();
  115. try
  116. {
  117. //先根据用户请求的uri构造请求地址
  118. serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
  119. authorization = Global.insuConfig.authorization;
  120. joRtn = invokeIrisService(data, serviceDesc);
  121. rtn = JsonConvert.SerializeObject(joRtn);
  122. return joRtn;
  123. }
  124. catch (Exception ex)
  125. {
  126. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  127. rtn = JsonConvert.SerializeObject(joRtn);
  128. return joRtn;
  129. }
  130. finally
  131. {
  132. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
  133. }
  134. }
  135. private JObject invokeCenterService(string data)
  136. {
  137. string postContent = "";
  138. JObject joRtn = new JObject();
  139. try
  140. {
  141. //创建一个HTTP请求
  142. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  143. //Post请求方式
  144. request.Method = "POST";
  145. //内容类型
  146. request.ContentType = "application/json";
  147. String stamp = TimeStamp.get13().ToString();
  148. string apiName = Global.curEvt.URL.Substring(Global.curEvt.URL.Length - 12);
  149. //南昌增加头部信息
  150. string sTemp = "_api_access_key=" + Global.inf.AK
  151. + "&_api_name=" + apiName
  152. + "&_api_timestamp=" + stamp
  153. + "&_api_version=" + "1.0.0";
  154. string signature = Encrypt.ToBase64hmac(sTemp,Global.inf.SK);
  155. //Global.writeLog(sTemp);
  156. //Global.writeLog(Global.inf.SK);
  157. //Global.writeLog(signature);
  158. request.Headers.Add("_api_version", "1.0.0");
  159. request.Headers.Add("_api_timestamp", stamp);
  160. request.Headers.Add("_api_name",apiName);
  161. request.Headers.Add("_api_signature", signature);
  162. request.Headers.Add("_api_access_key", Global.inf.AK);
  163. //设置参数,并进行URL编码
  164. string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);
  165. byte[] payload;
  166. //将Json字符串转化为字节
  167. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  168. //设置请求的ContentLength
  169. request.ContentLength = payload.Length;
  170. //发送请求,获得请求流
  171. Stream writer;
  172. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  173. //将请求参数写入流
  174. writer.Write(payload, 0, payload.Length);
  175. writer.Close();//关闭请求流
  176. // String strValue = "";//strValue为http响应所返回的字符流
  177. HttpWebResponse response;
  178. try
  179. {
  180. //获得响应流
  181. response = (HttpWebResponse)request.GetResponse();
  182. }
  183. catch (WebException ex)
  184. {
  185. response = ex.Response as HttpWebResponse;
  186. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);
  187. }
  188. Stream s = response.GetResponseStream();
  189. StreamReader sRead = new StreamReader(s);
  190. postContent = sRead.ReadToEnd();
  191. sRead.Close();
  192. joRtn = JObject.Parse(postContent);//返回Json数据
  193. return joRtn;
  194. }
  195. catch (Exception ex)
  196. {
  197. postContent = "调用中心服务异常" + ex.Message;
  198. joRtn.Add("infcode", -1);
  199. joRtn.Add("err_msg", "invokeCenterService(1):" + ex.Message);
  200. return joRtn;
  201. }
  202. }
  203. public JObject invokeCenterService(string funNO, JObject data)
  204. {
  205. JObject joRtn = new JObject();
  206. String outPar = "";
  207. try
  208. {
  209. Global.curEvt.URL = Global.inf.centerURL + "/hsa-fsi-" + funNO;
  210. joRtn = invokeCenterService(data.ToString());
  211. outPar = JsonHelper.Compress(joRtn);
  212. return joRtn;
  213. }
  214. catch (Exception ex)
  215. {
  216. joRtn.Add("infcode", -1);
  217. joRtn.Add("err_msg", "invokeCenterService(2):" + ex.Message);
  218. outPar = JsonHelper.Compress(joRtn);
  219. return joRtn;
  220. }
  221. finally
  222. {
  223. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  224. this.saveCenterLog(JsonHelper.Compress(data), outPar);
  225. }
  226. }
  227. public JObject invokeCenterService(string funNO, string data)
  228. {
  229. JObject joRtn = new JObject();
  230. String outPar = "";
  231. try
  232. {
  233. Global.curEvt.URL = Global.inf.centerURL+ "/hsa-fsi-" + funNO;
  234. joRtn = invokeCenterService(data);
  235. outPar = JsonHelper.Compress(joRtn);
  236. return joRtn;
  237. }
  238. catch (Exception ex)
  239. {
  240. joRtn.Add("infcode", -1);
  241. joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message);
  242. outPar = JsonHelper.Compress(joRtn);
  243. return joRtn;
  244. }
  245. finally
  246. {
  247. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), outPar);
  248. this.saveCenterLog(data, outPar);
  249. }
  250. }
  251. public JObject DownloadCenterFile(string data)
  252. {
  253. string error = string.Empty; int errorCode = 0;
  254. string sRtn = "";
  255. try
  256. {
  257. JObject jsonInParam = JObject.Parse(data);
  258. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  259. string filePath = System.Environment.CurrentDirectory + "\\Download\\" + fileName;
  260. //如果不存在目录,则创建目录
  261. if (!Directory.Exists(System.Environment.CurrentDirectory + "\\Download"))
  262. {
  263. //创建文件夹
  264. DirectoryInfo dirInfo = Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\Download");
  265. }
  266. if (File.Exists(filePath))
  267. {
  268. File.Delete(filePath);
  269. }
  270. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  271. //创建一个HTTP请求
  272. Global.curEvt.URL = Global.inf.centerURL + "/hsa-fsi-9102";
  273. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  274. //Post请求方式
  275. request.Method = "POST";
  276. //内容类型
  277. request.ContentType = "application/json";
  278. String stamp = TimeStamp.get13().ToString();
  279. string apiName = Global.curEvt.URL.Substring(Global.curEvt.URL.Length - 12);
  280. //南昌增加头部信息
  281. string sTemp = "_api_access_key=" + Global.inf.AK
  282. + "&_api_name=" + apiName
  283. + "&_api_timestamp=" + stamp
  284. + "&_api_version=" + "1.0.0";
  285. string signature = Encrypt.ToBase64hmac(sTemp, Global.inf.SK);
  286. //Global.writeLog(sTemp);
  287. //Global.writeLog(Global.inf.SK);
  288. //Global.writeLog(signature);
  289. request.Headers.Add("_api_version", "1.0.0");
  290. request.Headers.Add("_api_timestamp", stamp);
  291. request.Headers.Add("_api_name", apiName);
  292. request.Headers.Add("_api_signature", signature);
  293. request.Headers.Add("_api_access_key", Global.inf.AK);
  294. //设置参数,并进行URL编码
  295. string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);
  296. byte[] payload;
  297. //将Json字符串转化为字节
  298. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  299. //设置请求的ContentLength
  300. request.ContentLength = payload.Length;
  301. Stream writer;
  302. try
  303. {
  304. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  305. }
  306. catch (Exception)
  307. {
  308. writer = null;
  309. errorCode = -100;
  310. error = "连接服务器失败!";
  311. }
  312. //将请求参数写入流
  313. writer.Write(payload, 0, payload.Length);
  314. writer.Close();//关闭请求流
  315. // String strValue = "";//strValue为http响应所返回的字符流
  316. //发送请求并获取相应回应数据
  317. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  318. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  319. Stream responseStream = response.GetResponseStream();
  320. //创建本地文件写入流
  321. byte[] bArr = new byte[1024];
  322. int iTotalSize = 0;
  323. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  324. while (size > 0)
  325. {
  326. iTotalSize += size;
  327. fs.Write(bArr, 0, size);
  328. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  329. }
  330. fs.Close();
  331. responseStream.Close();
  332. dynamic joReturn = new JObject();
  333. joReturn.errorCode = errorCode;
  334. joReturn.error = error;
  335. joReturn.filePath = filePath;
  336. sRtn = joReturn.ToString();
  337. return joReturn;
  338. }
  339. catch (Exception ex)
  340. {
  341. errorCode = -100;
  342. error = ex.Message;
  343. dynamic joReturn = new JObject();
  344. joReturn.errorCode = errorCode;
  345. joReturn.error = error;
  346. sRtn = joReturn.ToString();
  347. return joReturn;
  348. }
  349. finally
  350. {
  351. Global.writeLog("DownloadCenterFile" +"(" + Global.curEvt.URL + ")", data, sRtn);
  352. }
  353. }
  354. /// <summary>
  355. /// 保存中心交易日志到数据库
  356. /// </summary>
  357. /// <param name="inParam"></param>
  358. /// <param name="outParam"></param>
  359. private void saveCenterLog(string inParam, string outParam)
  360. {
  361. dynamic joIris = new JObject();
  362. string sRtn = "";
  363. try
  364. {
  365. //解析postContent,插入医保交易日志表
  366. JObject joIn = new JObject(JObject.Parse(inParam));
  367. JObject joOut = new JObject(JObject.Parse(outParam));
  368. JArray jaParams = new JArray();
  369. JObject joParam = new JObject();
  370. joParam.Add("inParam", JObject.FromObject(joIn));
  371. joParam.Add("outParam", JObject.FromObject(joOut));
  372. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  373. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  374. joParam.Add("updateUserID", Global.user.ID);
  375. joParam.Add("psn_no", Global.pat.psn_no);
  376. jaParams.Add(joParam);
  377. joIris.code = "09010021";
  378. joIris.Add("params", jaParams);
  379. //InvokeHelper invoker = new InvokeHelper();
  380. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  381. }
  382. catch (Exception ex)
  383. {
  384. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  385. }
  386. }
  387. }
  388. }