InvokeHelper.cs 20 KB

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