InvokeHelper.cs 34 KB

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