InvokeHelper.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. using PTMedicalInsurance.APIGATEWAY_SDK;
  27. using System.Net.Http;
  28. using System.Globalization;
  29. using PTMedicalInsurance.Forms;
  30. namespace PTMedicalInsurance.Helper
  31. {
  32. class InvokeHelper
  33. {
  34. private string serviceURL;
  35. private string authorization;
  36. ComputerInfo comp = new ComputerInfo();
  37. CardReader_FJ InsuInvoke = new CardReader_FJ();
  38. /// <summary>
  39. /// iris服务调用的封装
  40. /// </summary>
  41. /// <param name="data"></param>
  42. /// <returns></returns>
  43. //public JObject invokeIrisService(string data, string serviceDesc)
  44. //{
  45. // string rtn = "", url = "";
  46. // JObject joRtn = new JObject();
  47. // try
  48. // {
  49. // //先根据用户请求的uri构造请求地址
  50. // url = serviceURL;
  51. // ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  52. // ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  53. // //创建Web访问对象
  54. // HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  55. // //把用户传过来的数据转成“UTF-8”的字节流
  56. // byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  57. // //添加头部信息
  58. // myRequest.Method = "POST";
  59. // myRequest.ContentLength = buf.Length;
  60. // myRequest.ContentType = "application/json";
  61. // myRequest.Headers.Add("Authorization", authorization);
  62. // myRequest.MaximumAutomaticRedirections = 1;
  63. // myRequest.AllowAutoRedirect = true;
  64. // //发送请求
  65. // Stream stream = myRequest.GetRequestStream();
  66. // stream.Write(buf, 0, buf.Length);
  67. // stream.Close();
  68. // //获取接口返回值
  69. // //通过Web访问对象获取响应内容
  70. // HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  71. // //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快
  72. // StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  73. // //string rtn = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法
  74. // rtn = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
  75. // reader.Close();
  76. // myResponse.Close();
  77. // joRtn = JObject.Parse(rtn);
  78. // return joRtn;
  79. // }
  80. // catch (Exception ex)
  81. // {
  82. // joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  83. // rtn = JsonConvert.SerializeObject(joRtn);
  84. // return joRtn;
  85. // }
  86. //}
  87. public JObject invokeIrisService(string data, string serviceDesc)
  88. {
  89. JObject joRtn = new JObject();
  90. try
  91. {
  92. // 1. 设置 SSL 和 TLS 协议
  93. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  94. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  95. // 2. 创建请求并使用 using 确保资源释放
  96. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceURL);
  97. myRequest.Method = "POST";
  98. myRequest.ContentType = "application/json";
  99. myRequest.Headers.Add("Authorization", authorization);
  100. myRequest.MaximumAutomaticRedirections = 1;
  101. myRequest.AllowAutoRedirect = true;
  102. // 【关键】设置合理的超时时间(例如 30 秒),防止无限等待
  103. myRequest.Timeout = 30000;
  104. myRequest.ReadWriteTimeout = 30000;
  105. // 3. 发送请求体
  106. byte[] buf = System.Text.Encoding.UTF8.GetBytes(data);
  107. myRequest.ContentLength = buf.Length;
  108. using (Stream stream = myRequest.GetRequestStream())
  109. {
  110. stream.Write(buf, 0, buf.Length);
  111. }
  112. // 4. 获取响应并处理业务报错
  113. string rtn = "";
  114. using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
  115. using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
  116. {
  117. rtn = reader.ReadToEnd();
  118. }
  119. joRtn = JObject.Parse(rtn);
  120. }
  121. catch (WebException webEx)
  122. {
  123. // 【核心修复】专门处理 WebException,区分超时和业务报错
  124. if (webEx.Status == WebExceptionStatus.Timeout)
  125. {
  126. // 明确返回超时错误
  127. joRtn = JsonHelper.setExceptionJson(-2, serviceDesc, "请求服务超时,请稍后重试");
  128. }
  129. else if (webEx.Response != null)
  130. {
  131. // 服务端返回了 4xx/5xx 等业务错误,尝试读取响应体中的真实报错
  132. try
  133. {
  134. using (StreamReader errorReader = new StreamReader(webEx.Response.GetResponseStream(), Encoding.UTF8))
  135. {
  136. string errorBody = errorReader.ReadToEnd();
  137. // 尝试将业务报错解析为 JSON,如果解析失败则直接返回原文
  138. joRtn = JObject.Parse(errorBody);
  139. }
  140. }
  141. catch
  142. {
  143. // 如果业务报错不是 JSON 格式,包装成标准格式返回
  144. joRtn = JsonHelper.setExceptionJson(-3, serviceDesc, $"服务端异常: {webEx.Message}");
  145. }
  146. }
  147. else
  148. {
  149. // 其他网络级错误(如 DNS 解析失败、连接被拒绝等)
  150. joRtn = JsonHelper.setExceptionJson(-4, serviceDesc, $"网络异常: {webEx.Message}");
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. // 捕获非网络请求类的异常(如 JSON 解析失败等)
  156. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  157. }
  158. return joRtn;
  159. }
  160. /// <summary>
  161. /// HIS服务调用的封装
  162. /// </summary>
  163. /// <param name="data"></param>
  164. /// <returns></returns>
  165. public JObject invokeHISService(string data, string serviceDesc)
  166. {
  167. JObject joRtn = new JObject();
  168. try
  169. {
  170. //先根据用户请求的uri构造请求地址
  171. serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
  172. authorization = Global.hisConfig.authorization;
  173. joRtn = invokeIrisService(data, serviceDesc);
  174. return joRtn;
  175. }
  176. catch (Exception ex)
  177. {
  178. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  179. return joRtn;
  180. }
  181. finally
  182. {
  183. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
  184. }
  185. }
  186. /// <summary>
  187. /// 医保平台服务调用的封装
  188. /// </summary>
  189. /// <param name="data"></param>
  190. /// <returns></returns>
  191. public JObject invokeInsuService(string data, string serviceDesc)
  192. {
  193. string rtn = "";
  194. JObject joRtn = new JObject();
  195. try
  196. {
  197. //先根据用户请求的uri构造请求地址
  198. serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
  199. authorization = Global.insuConfig.authorization;
  200. joRtn = invokeIrisService(data, serviceDesc);
  201. rtn = JsonConvert.SerializeObject(joRtn);
  202. //if (serviceDesc == "插入签到信息")
  203. //{
  204. // MessageBox.Show("插入签到信息入参:" + data +"|返回值:"+ rtn.ToString()+"|"+ Global.insuConfig.url);
  205. //}
  206. return joRtn;
  207. }
  208. catch (Exception ex)
  209. {
  210. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  211. rtn = JsonConvert.SerializeObject(joRtn);
  212. return joRtn;
  213. }
  214. finally
  215. {
  216. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
  217. }
  218. }
  219. /// <summary>
  220. /// 医保中心Post服务调用封装
  221. /// </summary>
  222. /// <param name="data"></param>
  223. /// <returns></returns>
  224. private JObject invokeCenterService(string data)
  225. {
  226. string postContent = "";
  227. JObject joRtn = new JObject();
  228. try
  229. {
  230. //创建一个HTTP请求
  231. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  232. //Post请求方式
  233. request.Method = "POST";
  234. //内容类型
  235. request.ContentType = "application/json";
  236. request.Headers.Add("apikey", "fa8kQMIF6LB0D7c6UGQoZc6TCcZO2G6O");
  237. request.Accept = "text/html, application/xhtml+xml, */*";
  238. //昆明增加头部信息
  239. /*
  240. string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  241. string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  242. string BusinessID = Global.inf.BusinessID; //服务商ID
  243. string InsuHosID = Global.inf.hospitalNO; //医疗机构ID
  244. string CreditID = Global.inf.CreditID; //服务商统一社会信用代码
  245. string sTemp = timestamp + BusinessID + nonce;
  246. //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)
  247. string signature = Encrypt.SHA256EncryptStr(sTemp);
  248. request.Headers.Add("hsf_signature", signature);
  249. request.Headers.Add("hsf_timestamp", timestamp);
  250. request.Headers.Add("hsf_nonce", nonce);
  251. request.Headers.Add("fixmedins_code", InsuHosID);
  252. request.Headers.Add("infosyscode", CreditID);
  253. */
  254. //设置参数,并进行URL编码
  255. string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);
  256. byte[] payload;
  257. //将Json字符串转化为字节
  258. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  259. //设置请求的ContentLength
  260. request.ContentLength = payload.Length;
  261. //发送请求,获得请求流
  262. Stream writer;
  263. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  264. //将请求参数写入流
  265. writer.Write(payload, 0, payload.Length);
  266. writer.Close();//关闭请求流
  267. // String strValue = "";//strValue为http响应所返回的字符流
  268. HttpWebResponse response;
  269. try
  270. {
  271. //获得响应流
  272. response = (HttpWebResponse)request.GetResponse();
  273. }
  274. catch (WebException ex)
  275. {
  276. response = ex.Response as HttpWebResponse;
  277. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);
  278. }
  279. Stream s = response.GetResponseStream();
  280. StreamReader sRead = new StreamReader(s);
  281. postContent = sRead.ReadToEnd();
  282. sRead.Close();
  283. joRtn = JObject.Parse(postContent);//返回Json数据
  284. return joRtn;
  285. }
  286. catch (Exception ex)
  287. {
  288. postContent = "调用中心服务异常" + ex.Message;
  289. joRtn.Add("infcode", -1);
  290. joRtn.Add("err_msg", "invokeCenterService(1):" + ex.Message);
  291. return joRtn;
  292. }
  293. }
  294. /// <summary>
  295. /// 医保中心Post服务保存文件流调用封装
  296. /// </summary>
  297. /// <param name="data"></param>
  298. /// <returns></returns>
  299. private JObject invokeCenterServiceSavePDF(string data)
  300. {
  301. string postContent = "";
  302. JObject joRtn = new JObject();
  303. try
  304. {
  305. JObject jsonInParam = JObject.Parse(data);
  306. string name = (string)jsonInParam["input"]["data"]["name"];
  307. string setlID = (string)jsonInParam["input"]["data"]["setl_id"];
  308. string fileName = name + "_" + setlID + ".pdf";
  309. string filePath = Global.curEvt.path + "\\JSD\\" + fileName;
  310. Global.Set.filePath = filePath;
  311. //如果不存在目录,则创建目录
  312. if (!Directory.Exists(Global.curEvt.path + "\\JSD"))
  313. {
  314. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\JSD"); //创建文件夹
  315. }
  316. if (File.Exists(filePath))
  317. {
  318. File.Delete(filePath);
  319. }
  320. //创建一个HTTP请求
  321. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  322. //Post请求方式
  323. request.Method = "POST";
  324. //内容类型
  325. request.ContentType = "application/json";
  326. request.Headers.Add("apikey", "fa8kQMIF6LB0D7c6UGQoZc6TCcZO2G6O");
  327. request.Accept = "text/html, application/xhtml+xml, */*";
  328. //设置参数,并进行URL编码
  329. string paraUrlCoded = data;
  330. byte[] payload;
  331. //将Json字符串转化为字节
  332. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  333. //设置请求的ContentLength
  334. request.ContentLength = payload.Length;
  335. //发送请求,获得请求流
  336. Stream writer;
  337. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  338. //将请求参数写入流
  339. writer.Write(payload, 0, payload.Length);
  340. writer.Close();//关闭请求流
  341. HttpWebResponse response;
  342. try
  343. {
  344. response = (HttpWebResponse)request.GetResponse(); //获得响应流
  345. }
  346. catch (WebException ex)
  347. {
  348. response = ex.Response as HttpWebResponse;
  349. return JsonHelper.setExceptionJson(-99, "invokeCenterServiceSavePDF中获得响应流异常", ex.Message);
  350. }
  351. Stream s = response.GetResponseStream();
  352. //创建本地文件写入流
  353. Stream fs = new FileStream(filePath, FileMode.Create);
  354. byte[] bArr = new byte[1024];
  355. int size = s.Read(bArr, 0, (int)bArr.Length);
  356. while (size > 0)
  357. {
  358. fs.Write(bArr, 0, size);
  359. size = s.Read(bArr, 0, (int)bArr.Length);
  360. }
  361. fs.Close();
  362. s.Close();
  363. //MessageBox.Show("结算单下载成功!存放路径:" + filePath);
  364. joRtn.Add("infcode", 0);
  365. joRtn.Add("err_msg", filePath);
  366. return joRtn;
  367. }
  368. catch (Exception ex)
  369. {
  370. postContent = "调用中心服务异常" + ex.Message;
  371. joRtn.Add("infcode", -1);
  372. joRtn.Add("err_msg", "invokeCenterServiceSavePDF(1):" + ex.Message);
  373. return joRtn;
  374. }
  375. }
  376. /// <summary>
  377. /// 这个是调用业务服务的invokeCenterService
  378. /// </summary>
  379. /// <param name="funNO"></param>
  380. /// <param name="data"></param>
  381. /// <returns></returns>
  382. public JObject invokeCenterService(string funNO, JObject data)
  383. {
  384. JObject joRtn = new JObject();
  385. string outPar = "", errMsg = "";
  386. int iRes = 0;
  387. try
  388. {
  389. //Global.curEvt.URL = Global.inf.centerURL;
  390. //joRtn = invokeCenterService(data.ToString());
  391. //outPar = JsonHelper.Compress(joRtn);
  392. //return joRtn;
  393. iRes = InsuInvoke.BusinessHandle(data.ToString(), ref outPar, ref errMsg);
  394. if (iRes != 0)
  395. {
  396. joRtn.Add("infcode", -1);
  397. joRtn.Add("err_msg", errMsg + "---" + outPar);
  398. return joRtn;
  399. }
  400. //outPar = JsonHelper.Compress(joRtn);
  401. return JObject.Parse(outPar);
  402. }
  403. catch (Exception ex)
  404. {
  405. if (joRtn["infcode"] == null)
  406. { joRtn.Add("infcode", -1); }
  407. if (joRtn["err_msg"] == null)
  408. { joRtn.Add("err_msg", "invokeCenterService(2):" + ex.Message); }
  409. outPar = JsonHelper.Compress(joRtn);
  410. return joRtn;
  411. }
  412. finally
  413. {
  414. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  415. this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
  416. }
  417. }
  418. /// <summary>
  419. /// 这个是下载目录用的invokeCenterService
  420. /// </summary>
  421. /// <param name="funNO"></param>
  422. /// <param name="data"></param>
  423. /// <returns></returns>
  424. public JObject invokeCenterService(string funNO, string data)
  425. {
  426. JObject joRtn = new JObject();
  427. string outPar = "";
  428. string errMsg = "";
  429. int iRes = -1;
  430. try
  431. {
  432. Global.curEvt.URL = Global.inf.centerURL;
  433. //调用结算单打印交易输出PDF文件流
  434. //iRes = InsuInvoke.Authorization(Global.insuConfig.id, Global.insuConfig.password, ref outPar);
  435. //if (iRes != 0)
  436. //{
  437. // joRtn.Add("infcode", -1);
  438. // joRtn.Add("err_msg", "医保两定平台登录失败:" + outPar);
  439. // return joRtn;
  440. //}
  441. //else
  442. //{
  443. // InsuInvoke.BusinessHandle(data, ref outPar, ref errMsg);
  444. //}
  445. iRes = InsuInvoke.BusinessHandle(data, ref outPar, ref errMsg);
  446. if (iRes != 0)
  447. {
  448. if ((errMsg.Contains("机构授权用户名或密码错误") == true)|| (errMsg.Contains("传入参数机构编码与登录机构不一样传入机构") == true))
  449. {
  450. int iLogRtn = InsuInvoke.Authorization(Global.insuConfig.id, Global.insuConfig.password, ref outPar);
  451. if (iLogRtn != 0)
  452. {
  453. joRtn.Add("infcode", -1);
  454. joRtn.Add("err_msg", "医保两定平台登录失败:" + outPar);
  455. return joRtn;
  456. }
  457. else
  458. {
  459. iRes = InsuInvoke.BusinessHandle(data, ref outPar, ref errMsg);
  460. if (iRes != 0)
  461. {
  462. joRtn.Add("infcode", -1);
  463. joRtn.Add("err_msg", errMsg + "---" + outPar);
  464. return joRtn;
  465. }
  466. else
  467. {
  468. return JObject.Parse(outPar);
  469. }
  470. }
  471. }
  472. joRtn.Add("infcode",-1);
  473. joRtn.Add("err_msg", errMsg + "---" + outPar);
  474. return joRtn;
  475. }
  476. //outPar = JsonHelper.Compress(joRtn);
  477. return JObject.Parse(outPar);
  478. }
  479. catch (Exception ex)
  480. {
  481. if (joRtn["infcode"] == null)
  482. { joRtn.Add("infcode", -1); }
  483. if (joRtn["err_msg"] == null)
  484. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  485. outPar = JsonHelper.Compress(joRtn);
  486. return joRtn;
  487. }
  488. finally
  489. {
  490. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), outPar);
  491. this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
  492. }
  493. }
  494. /// <summary>
  495. /// 医保登录接口调用
  496. /// </summary>
  497. /// <param name="funNO"></param>
  498. /// <param name="data"></param>
  499. /// <returns></returns>
  500. public JObject invokeCenterServiceLogn(JObject data)
  501. {
  502. JObject joRtn = new JObject();
  503. string outPar = "";
  504. int iRes = -1;
  505. try
  506. {
  507. Global.curEvt.URL = Global.inf.centerURL;
  508. string id = JsonHelper.getDestValue(data, "ID");
  509. string password = JsonHelper.getDestValue(data, "PassWord");
  510. iRes = InsuInvoke.Authorization(id, password, ref outPar);
  511. if (iRes != 0)
  512. {
  513. joRtn.Add("infcode", -1);
  514. joRtn.Add("err_msg", "医保两定平台登录失败:" + outPar);
  515. }
  516. else
  517. {
  518. joRtn.Add("infcode", iRes);
  519. joRtn.Add("err_msg", "医保两定平台登录成功:" + outPar);
  520. }
  521. //MessageBox.Show(outPar);
  522. return joRtn;
  523. }
  524. catch (Exception ex)
  525. {
  526. if (joRtn["infcode"] == null)
  527. { joRtn.Add("infcode", -1); }
  528. if (joRtn["err_msg"] == null)
  529. { joRtn.Add("err_msg", "invokeCenterServiceLogn(4):" + ex.Message); }
  530. outPar = JsonHelper.Compress(joRtn);
  531. return joRtn;
  532. }
  533. finally
  534. {
  535. Global.writeLog("authorization:(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  536. this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
  537. }
  538. }
  539. /// <summary>
  540. /// 医保目录txt文件下载
  541. /// </summary>
  542. /// <param name="data"></param>
  543. /// <returns></returns>
  544. public JObject DownloadCenterFile(string funNO,string data)
  545. {
  546. string error = string.Empty; int errorCode = 0;
  547. string sRtn = "";
  548. try
  549. {
  550. JObject jsonInParam = JObject.Parse(data);
  551. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  552. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  553. //MessageBox.Show("9102下载文件入参:"+jsonInParam.ToString());
  554. //如果不存在目录,则创建目录
  555. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  556. {
  557. //创建文件夹
  558. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  559. }
  560. if (File.Exists(filePath))
  561. {
  562. File.Delete(filePath);
  563. }
  564. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  565. //创建一个HTTP请求
  566. //Global.curEvt.URL = Global.inf.centerURL + "/hsa-fsi-9102";
  567. //Global.curEvt.URL = Global.inf.centerURL;
  568. Global.curEvt.URL = comp.getFunNoInsuURL(funNO);
  569. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  570. //Post请求方式
  571. request.Method = "POST";
  572. //内容类型
  573. request.ContentType = "application/json";
  574. request.Headers.Add("apikey", "fa8kQMIF6LB0D7c6UGQoZc6TCcZO2G6O");
  575. request.Accept = "text/html, application/xhtml+xml, */*";
  576. //设置参数,并进行URL编码
  577. string paraUrlCoded = data;//System.Web.HttpUtility.UrlEncode(jsonParas);
  578. byte[] payload;
  579. //将Json字符串转化为字节
  580. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  581. //设置请求的ContentLength
  582. request.ContentLength = payload.Length;
  583. Stream writer;
  584. try
  585. {
  586. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  587. }
  588. catch (Exception)
  589. {
  590. writer = null;
  591. errorCode = -100;
  592. error = "连接服务器失败!";
  593. }
  594. //将请求参数写入流
  595. writer.Write(payload, 0, payload.Length);
  596. writer.Close();//关闭请求流
  597. // String strValue = "";//strValue为http响应所返回的字符流
  598. //发送请求并获取相应回应数据
  599. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  600. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  601. Stream responseStream = response.GetResponseStream();
  602. //创建本地文件写入流
  603. byte[] bArr = new byte[1024];
  604. int iTotalSize = 0;
  605. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  606. while (size > 0)
  607. {
  608. iTotalSize += size;
  609. fs.Write(bArr, 0, size);
  610. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  611. }
  612. fs.Close();
  613. responseStream.Close();
  614. dynamic joReturn = new JObject();
  615. joReturn.errorCode = errorCode;
  616. joReturn.errorMessage = error;
  617. joReturn.filePath = filePath;
  618. sRtn = joReturn.ToString();
  619. return joReturn;
  620. }
  621. catch (Exception ex)
  622. {
  623. errorCode = -100;
  624. error = ex.Message;
  625. dynamic joReturn = new JObject();
  626. joReturn.errorCode = errorCode;
  627. joReturn.errorMessage = error;
  628. sRtn = joReturn.ToString();
  629. return joReturn;
  630. }
  631. finally
  632. {
  633. Global.writeLog("DownloadCenterFile" +"(" + Global.curEvt.URL + ")", data, sRtn);
  634. }
  635. }
  636. /// <summary>
  637. /// 保存中心交易日志到数据库
  638. /// </summary>
  639. /// <param name="inParam"></param>
  640. /// <param name="outParam"></param>
  641. /// <param name="inParamPlain"></param>
  642. /// <param name="outParamPlain"></param>
  643. private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
  644. {
  645. dynamic joIris = new JObject();
  646. string sRtn = "";
  647. try
  648. {
  649. //解析postContent,插入医保交易日志表
  650. JObject joIn = new JObject(JObject.Parse(inParam));
  651. JObject joOut = new JObject(JObject.Parse(outParam));
  652. JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
  653. JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
  654. JArray jaParams = new JArray();
  655. JObject joParam = new JObject();
  656. joParam.Add("inParam", JObject.FromObject(joIn));
  657. joParam.Add("outParam", JObject.FromObject(joOut));
  658. joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
  659. joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
  660. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  661. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  662. joParam.Add("updateUserID", Global.user.ID);
  663. joParam.Add("psn_no", Global.pat.psn_no);
  664. jaParams.Add(joParam);
  665. joIris.code = "09010021";
  666. joIris.Add("params", jaParams);
  667. //InvokeHelper invoker = new InvokeHelper();
  668. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  669. }
  670. catch (Exception ex)
  671. {
  672. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  673. Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
  674. }
  675. }
  676. /// <summary>
  677. /// 医保电子处方流转调用服务
  678. /// </summary>
  679. /// <param name="data"></param>
  680. /// <returns></returns>
  681. private JObject invokeCenterServicePresCir(string data)
  682. {
  683. string postContent = "";
  684. JObject joRtn = new JObject();
  685. try
  686. {
  687. //内容类型
  688. Signer signer = new Signer();
  689. signer.Key = Global.inf.privateKey; //应用编码
  690. signer.Secret = Global.inf.Secret; //secretKey 私钥
  691. string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  692. string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  693. HttpRequest Resquest = new HttpRequest("POST", new Uri(Global.curEvt.URL));
  694. Resquest.headers.Add("charset", "UTF-8");
  695. //Resquest.headers.Add("x-hw-id", signer.Key);
  696. //Resquest.headers.Add("x-tif-timestamp", timestamp);
  697. //Resquest.headers.Add("x-tif-passid", signer.Key);
  698. //Resquest.headers.Add("x-tif-nonce", nonce);
  699. Resquest.body = data;
  700. string RtnStr;
  701. HttpWebRequest req = signer.Sign(Resquest);
  702. req.ContentType = "application/json;charset=utf8";
  703. req.Timeout = 5 * 10000;
  704. try
  705. {
  706. var writer = new StreamWriter(req.GetRequestStream());
  707. writer.Write(Resquest.body);
  708. writer.Flush();
  709. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  710. StreamReader reader = new StreamReader(resp.GetResponseStream());
  711. RtnStr = reader.ReadToEnd();
  712. joRtn = JObject.Parse(RtnStr);//返回Json数据
  713. return joRtn;
  714. }
  715. catch (WebException e)
  716. {
  717. HttpWebResponse resp = (HttpWebResponse)e.Response;
  718. if (resp != null)
  719. {
  720. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(a)", new StreamReader(resp.GetResponseStream()).ReadToEnd() + "异常内容:" + e.Message);
  721. }
  722. else
  723. {
  724. RtnStr = "异常:" + e.Message;
  725. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(b)", e.Message);
  726. }
  727. }
  728. }
  729. catch (Exception ex)
  730. {
  731. postContent = "调用中心服务异常" + ex.Message;
  732. joRtn.Add("infcode", -1);
  733. joRtn.Add("err_msg", "invokeCenterService(Exception_Last):" + ex.Message);
  734. return joRtn;
  735. }
  736. }
  737. /// </summary>
  738. /// <param name="funNO"></param>
  739. /// <param name="data"></param>
  740. /// <returns></returns>
  741. public JObject invokeCenterServicePresCir(string funNO, string data)
  742. {
  743. JObject joRtn = new JObject();
  744. string outPar = "";
  745. try
  746. {
  747. if (funNO == "7101")
  748. {
  749. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/uploadChk";
  750. }
  751. else if (funNO == "7102")
  752. {
  753. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxFixmedinsSign";
  754. }
  755. else if (funNO == "7103")
  756. {
  757. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxFileUpld";
  758. }
  759. else if (funNO == "7104")
  760. {
  761. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxUndo";
  762. }
  763. else if (funNO == "7105")
  764. {
  765. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/hospRxDetlQuery";
  766. }
  767. else if (funNO == "7106")
  768. {
  769. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxChkInfoQuery";
  770. }
  771. else if (funNO == "7107")
  772. {
  773. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxSetlInfoQuery";
  774. }
  775. else if (funNO == "7108")
  776. {
  777. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxChkInfoCallback";
  778. }
  779. else if (funNO == "7109")
  780. {
  781. Global.curEvt.URL = Global.inf.presCenterURL + "/fixmedins/rxSetlInfoCallback";
  782. }
  783. else
  784. {
  785. Global.curEvt.URL = Global.inf.centerURL;
  786. }
  787. //Global.curEvt.URL = Global.inf.centerURL;
  788. joRtn = invokeCenterServicePresCir(data);
  789. outPar = JsonHelper.Compress(joRtn);
  790. return joRtn;
  791. }
  792. catch (Exception ex)
  793. {
  794. if (joRtn["infcode"] == null)
  795. { joRtn.Add("infcode", -1); }
  796. if (joRtn["err_msg"] == null)
  797. { joRtn.Add("err_msg", "invokeCenterServicePresCir(3):" + ex.Message); }
  798. outPar = JsonHelper.Compress(joRtn);
  799. return joRtn;
  800. }
  801. finally
  802. {
  803. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  804. //this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
  805. }
  806. }
  807. #region 【医保移动支付】
  808. /// <summary>
  809. /// 移动
  810. /// </summary>
  811. /// <param name="funNO"></param>
  812. /// <param name="data"></param>
  813. /// <returns></returns>
  814. public JObject invokeMPService(string funNO, string data)
  815. {
  816. return invokeMPService(funNO, JObject.Parse(data));
  817. }
  818. public JObject invokeMPService(string funNO, JObject joInput)
  819. {
  820. JObject joRtn = new JObject();
  821. String outPar = "";
  822. try
  823. {
  824. string url = "";
  825. switch (funNO)
  826. {
  827. case "6201":
  828. url = "/org/local/api/hos/uldFeeInfo";
  829. break;
  830. case "6202":
  831. url = "/org/local/api/hos/pay_order";
  832. break;
  833. case "6203":
  834. url = "/org/local/api/hos/refund_Order";
  835. break;
  836. case "6301":
  837. url = "/org/local/api/hos/query_order_info";
  838. break;
  839. case "6401":
  840. url = "/org/local/api/hos/revoke_order";
  841. break;
  842. default:
  843. break;
  844. }
  845. EncryptHelper encrypt = new EncryptHelper();
  846. //西安医保移动支付测试环境
  847. Global.inf.mobilePayURL = @"http:\\10.37.128.40:8086";
  848. //西安医保移动支付正式环境
  849. //Global.inf.mobilePayURL = @"http:\\10.37.128.41:8086";
  850. string data = JsonHelper.setMPCenterInpar(funNO, joInput);
  851. // 移动支付地址
  852. Global.curEvt.URL = Global.inf.mobilePayURL + url;
  853. Global.inf.centerURL = Global.curEvt.URL;
  854. string outputData = "", errMsg = "";
  855. // 动态调试模式
  856. if (Global.curEvt.enabledDebug)
  857. {
  858. CenterResult center = new CenterResult();
  859. center.setTradeNo(funNO);
  860. if (center.ShowDialog() == DialogResult.OK)
  861. {
  862. outPar = center.returnData;
  863. return JObject.Parse(outPar);
  864. }
  865. }
  866. try
  867. {
  868. InvokeRestCenter mobileCenter = new InvokeRestCenter();
  869. int iInt = mobileCenter.Business(data, ref outputData, ref errMsg);
  870. joRtn = JObject.Parse(outputData);
  871. string encData = JsonHelper.getDestValue(joRtn, "encData");
  872. string signData = JsonHelper.getDestValue(joRtn, "signData");
  873. if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
  874. {
  875. joRtn.Remove("encData");
  876. joRtn.Remove("signData");
  877. joRtn.Remove("data");
  878. //解密
  879. string decData = encrypt.decrypt00(encData);
  880. // 验签
  881. JsonConvert.DefaultSettings = () => new JsonSerializerSettings
  882. {
  883. FloatParseHandling = FloatParseHandling.Decimal
  884. };
  885. joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
  886. bool rtn = encrypt.verify(joRtn, signData);
  887. if (rtn)
  888. {
  889. joRtn = JObject.Parse(decData);
  890. joRtn.Add("success", "True");
  891. }
  892. else
  893. {
  894. Global.writeLog("验签失败,请核查!");
  895. }
  896. }
  897. return joRtn;
  898. }
  899. finally
  900. {
  901. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  902. }
  903. }
  904. catch (Exception ex)
  905. {
  906. if (joRtn["infcode"] == null)
  907. { joRtn.Add("infcode", -1); }
  908. if (joRtn["err_msg"] == null)
  909. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  910. outPar = JsonHelper.Compress(joRtn);
  911. return joRtn;
  912. }
  913. finally
  914. {
  915. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());
  916. this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());
  917. }
  918. }
  919. #endregion
  920. }
  921. }