InvokeHelper.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. using System.Runtime.InteropServices;
  26. using PTMedicalInsurance.Forms;
  27. using GMCrypto.Lib;
  28. namespace PTMedicalInsurance.Helper
  29. {
  30. class InvokeHelper
  31. {
  32. private string serviceURL;
  33. private string authorization;
  34. public InvokeHelper()
  35. {
  36. IniFile ini = new IniFile(Global.curEvt.path + @"\CenterServiceURL.ini");
  37. Global.inf.centerURL = ini.ReadValue("CENTER", "url");
  38. Global.inf.uploadURL = ini.ReadValue("CENTER", "upload");
  39. Global.inf.downURL = ini.ReadValue("CENTER", "download");
  40. Global.inf.ecURL = ini.ReadValue("CENTER", "ecToken");
  41. Global.inf.mobilePayURL = ini.ReadValue("CENTER", "mobilePay");
  42. Global.inf.ecPrescURL = ini.ReadValue("CENTER", "prescription");
  43. if (string.IsNullOrEmpty(Global.inf.mobilePayURL))
  44. {
  45. Global.inf.mobilePayURL = "http://10.123.185.12:8080";
  46. }
  47. if (string.IsNullOrEmpty(Global.inf.ecPrescURL))
  48. {
  49. Global.inf.ecPrescURL = "http://10.123.185.12:8080/epc/api";
  50. }
  51. }
  52. #region 内部服务调用
  53. /// <summary>
  54. /// iris服务调用的封装
  55. /// </summary>
  56. /// <param name="data"></param>
  57. /// <returns></returns>
  58. public JObject invokeIrisService(string data, string serviceDesc)
  59. {
  60. string rtn = "", url = "";
  61. JObject joRtn = new JObject();
  62. try
  63. {
  64. //先根据用户请求的uri构造请求地址
  65. url = serviceURL;
  66. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  67. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  68. //创建Web访问对象
  69. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  70. //把用户传过来的数据转成“UTF-8”的字节流
  71. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  72. //添加头部信息
  73. myRequest.Method = "POST";
  74. myRequest.ContentLength = buf.Length;
  75. myRequest.ContentType = "application/json";
  76. myRequest.Headers.Add("Authorization", authorization);
  77. myRequest.MaximumAutomaticRedirections = 1;
  78. myRequest.AllowAutoRedirect = true;
  79. //发送请求
  80. Stream stream = myRequest.GetRequestStream();
  81. stream.Write(buf, 0, buf.Length);
  82. stream.Close();
  83. //获取接口返回值
  84. //通过Web访问对象获取响应内容
  85. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  86. rtn = getResponseData(myResponse);
  87. joRtn = JObject.Parse(rtn);
  88. return joRtn;
  89. }
  90. catch (Exception ex)
  91. {
  92. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  93. rtn = JsonConvert.SerializeObject(joRtn);
  94. return joRtn;
  95. }
  96. }
  97. /// <summary>
  98. /// HIS服务调用的封装
  99. /// </summary>
  100. /// <param name="data"></param>
  101. /// <returns></returns>
  102. ///
  103. public JObject invokeHISService(string data, string serviceDesc)
  104. {
  105. JObject joRtn = new JObject();
  106. try
  107. {
  108. //先根据用户请求的uri构造请求地址
  109. serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
  110. authorization = Global.hisConfig.authorization;
  111. joRtn = invokeIrisService(data, serviceDesc);
  112. return joRtn;
  113. }
  114. catch (Exception ex)
  115. {
  116. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  117. return joRtn;
  118. }
  119. finally
  120. {
  121. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
  122. }
  123. }
  124. /// <summary>
  125. /// 医保平台服务调用的封装
  126. /// </summary>
  127. /// <param name="data"></param>
  128. /// <returns></returns>
  129. public JObject invokeInsuService(string data, string serviceDesc)
  130. {
  131. string rtn = "";
  132. JObject joRtn = new JObject();
  133. try
  134. {
  135. //先根据用户请求的uri构造请求地址
  136. serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
  137. authorization = Global.insuConfig.authorization;
  138. joRtn = invokeIrisService(data, serviceDesc);
  139. rtn = JsonConvert.SerializeObject(joRtn);
  140. //if (serviceDesc == "插入签到信息")
  141. //{
  142. // MessageBox.Show("插入签到信息入参:" + data +"|返回值:"+ rtn.ToString()+"|"+ Global.insuConfig.url);
  143. //}
  144. return joRtn;
  145. }
  146. catch (Exception ex)
  147. {
  148. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  149. rtn = JsonConvert.SerializeObject(joRtn);
  150. return joRtn;
  151. }
  152. finally
  153. {
  154. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
  155. }
  156. }
  157. private string getResponseData(HttpWebResponse response)
  158. {
  159. string data = "";
  160. if (response != null)
  161. {
  162. Stream s = response.GetResponseStream();
  163. StreamReader sRead = new StreamReader(s);
  164. data = sRead.ReadToEnd();
  165. sRead.Close();
  166. response.Close();
  167. }
  168. return data;
  169. }
  170. #endregion
  171. #region 医保中心调用
  172. private JObject invokeCenterService(string data)
  173. {
  174. JObject joRtn = new JObject();
  175. try
  176. {
  177. IInvokeCenter center = InvokeCenterFactory.create();
  178. string outputData = "";
  179. string errMsg = "";
  180. int iInt = center.Init(ref errMsg);
  181. if (iInt == 0)
  182. {
  183. iInt = center.Business(data, ref outputData, ref errMsg);
  184. if (iInt == 0 && !string.IsNullOrEmpty(outputData))
  185. {
  186. try
  187. {
  188. joRtn = JObject.Parse(outputData);
  189. }
  190. catch (Exception ex)
  191. {
  192. joRtn.Add("infcode", iInt);
  193. joRtn.Add("err_msg", "返回参数异常:" + outputData);
  194. }
  195. }
  196. else
  197. {
  198. joRtn.Add("infcode", iInt);
  199. joRtn.Add("err_msg", outputData);
  200. return joRtn;
  201. }
  202. return joRtn;
  203. }
  204. else
  205. {
  206. joRtn.Add("infcode", -1);
  207. joRtn.Add("err_msg", "医保动态库初始化失败invokeInitByDLL:" + errMsg);
  208. return joRtn;
  209. }
  210. }
  211. finally
  212. {
  213. Global.writeLog($"调用中心({Global.curEvt.URL}):", JsonHelper.Compress(data), joRtn.ToString());
  214. // 保存到服务器
  215. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  216. }
  217. }
  218. private void prepareCallCenter(string funNo)
  219. {
  220. string prefix = Global.inf.centerURL;
  221. switch (funNo)
  222. {
  223. case "9101":
  224. prefix = Global.inf.uploadURL;
  225. break;
  226. case "9102":
  227. prefix = Global.inf.downURL;
  228. break;
  229. default:
  230. prefix = Global.inf.centerURL;
  231. break;
  232. }
  233. Global.curEvt.URL = prefix + funNo;
  234. }
  235. /// <summary>
  236. /// 这个是调用业务服务的invokeCenterService
  237. /// </summary>
  238. /// <param name="funNO"></param>
  239. /// <param name="data"></param>
  240. /// <returns></returns>
  241. public JObject invokeCenterService(string funNO, JObject data)
  242. {
  243. // 动态调试模式
  244. if (Global.curEvt.enabledDebug)
  245. {
  246. CenterResult center = new CenterResult();
  247. center.setTradeNo(funNO);
  248. if (center.ShowDialog() == DialogResult.OK)
  249. {
  250. string outPar = center.returnData;
  251. return JObject.Parse(outPar);
  252. }
  253. }
  254. prepareCallCenter(funNO);
  255. return invokeCenterService(JsonHelper.toJsonString(data));
  256. }
  257. /// <summary>
  258. /// 这个是下载目录用的invokeCenterService
  259. /// </summary>
  260. /// <param name="funNO"></param>
  261. /// <param name="data"></param>
  262. /// <returns></returns>
  263. public JObject invokeCenterService(string funNO, string data)
  264. {
  265. // 动态调试模式
  266. if (Global.curEvt.enabledDebug)
  267. {
  268. CenterResult center = new CenterResult();
  269. center.setTradeNo(funNO);
  270. if (center.ShowDialog() == DialogResult.OK)
  271. {
  272. string outPar = center.returnData;
  273. return JObject.Parse(outPar);
  274. }
  275. }
  276. prepareCallCenter(funNO);
  277. return invokeCenterService(data);
  278. }
  279. public JObject invokeCenterService(TradeEnum trade, JObject joInput)
  280. {
  281. string funNo = trade.GetCode();
  282. // 入参统一转换
  283. JObject request = Utils.ConvertRequest<JObject>(trade, joInput);
  284. string data = JsonHelper.toJsonString(request);
  285. // 统一封装请求头
  286. if (trade.GetMode() == ModeEnum.REST)
  287. {
  288. data = JsonHelper.setCenterInpar(funNo, request);
  289. }
  290. JObject joRtn = new JObject();
  291. // 调试模式
  292. if (Global.curEvt.enabledDebug)
  293. {
  294. CenterResult center = new CenterResult();
  295. center.setTradeNo(trade.GetCode(), data);
  296. if (center.ShowDialog() == DialogResult.OK)
  297. {
  298. // 接口实际返回数据
  299. string outPar = center.returnData;
  300. joRtn = JObject.Parse(outPar);
  301. }
  302. }
  303. else
  304. {
  305. prepareCallURI(trade);
  306. joRtn = invokeCenterService(data);
  307. }
  308. // 返回结果统一转换
  309. joRtn = Utils.ConvertResponse<JObject>(trade, joRtn);
  310. return joRtn;
  311. }
  312. /// <summary>
  313. /// 医保目录txt文件下载
  314. /// </summary>
  315. /// <param name="data"></param>
  316. /// <returns></returns>
  317. public JObject DownloadCenterFile(string data)
  318. {
  319. //download file
  320. IInvokeCenter center = InvokeCenterFactory.create();
  321. string outputMsg = "";
  322. JObject joRtn = new JObject();
  323. int rtnCode = center.DownloadFile(data, ref outputMsg);
  324. if(rtnCode==0)
  325. {
  326. joRtn = JObject.Parse(outputMsg);
  327. }
  328. else
  329. {
  330. joRtn.Add("infcode", -1);
  331. joRtn.Add("err_msg", "下载文件失败DownloadFile:" + outputMsg);
  332. }
  333. return joRtn;
  334. }
  335. private void prepareCallURI(TradeEnum trade)
  336. {
  337. string funNo = trade.GetCode();
  338. //Global.curEvt.funNo = funNo;
  339. if (Global.curEvt.testMode)
  340. {
  341. // 医保测试环境
  342. //LoadCenterURL(true);
  343. }
  344. string prefix = Global.inf.centerURL;
  345. switch (trade)
  346. {
  347. case TradeEnum.FileUpload:
  348. prefix = Global.inf.uploadURL ?? Global.inf.centerURL;
  349. break;
  350. case TradeEnum.FileDownload:
  351. prefix = Global.inf.centerURL;
  352. break;
  353. default:
  354. prefix = Global.inf.centerURL;
  355. break;
  356. }
  357. // 根据情况确实是否需要加funNo
  358. Global.curEvt.URL = prefix + funNo;
  359. }
  360. #endregion
  361. #region 移动
  362. /// <summary>
  363. /// 移动
  364. /// </summary>
  365. /// <param name="funNO"></param>
  366. /// <param name="data"></param>
  367. /// <returns></returns>
  368. public JObject invokeMPService(string funNO, string data)
  369. {
  370. return invokeMPService(funNO, JObject.Parse(data));
  371. }
  372. public JObject invokeMPService(string funNO, JObject joInput)
  373. {
  374. JObject joRtn = new JObject();
  375. String outPar = "";
  376. try
  377. {
  378. string url = "";
  379. switch (funNO)
  380. {
  381. case "6201":
  382. url = "/org/local/api/hos/uldFeeInfo";
  383. break;
  384. case "6202":
  385. url = "/org/local/api/hos/pay_order";
  386. break;
  387. case "6203":
  388. url = "/org/local/api/hos/refund_Order";
  389. break;
  390. case "6301":
  391. url = "/org/local/api/hos/query_order_info";
  392. break;
  393. case "6401":
  394. url = "/org/local/api/hos/revoke_order";
  395. break;
  396. default:
  397. break;
  398. }
  399. EncryptHelper encrypt = new EncryptHelper();
  400. string data = JsonHelper.setMPCenterInpar(funNO, joInput);
  401. // 移动支付地址
  402. Global.curEvt.URL = Global.inf.mobilePayURL + url;
  403. string outputData = "", errMsg = "";
  404. // 动态调试模式
  405. if (Global.curEvt.enabledDebug)
  406. {
  407. CenterResult center = new CenterResult();
  408. center.setTradeNo(funNO);
  409. if (center.ShowDialog() == DialogResult.OK)
  410. {
  411. outPar = center.returnData;
  412. return JObject.Parse(outPar);
  413. }
  414. }
  415. try
  416. {
  417. InvokeRestCenter mobileCenter = new InvokeRestCenter();
  418. int iInt = mobileCenter.Business(data, ref outputData, ref errMsg);
  419. joRtn = JObject.Parse(outputData);
  420. string encData = JsonHelper.getDestValue(joRtn, "encData");
  421. string signData = JsonHelper.getDestValue(joRtn, "signData");
  422. if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
  423. {
  424. joRtn.Remove("encData");
  425. joRtn.Remove("signData");
  426. joRtn.Remove("data");
  427. //解密
  428. string decData = encrypt.decrypt(encData);
  429. // 验签
  430. JsonConvert.DefaultSettings = () => new JsonSerializerSettings
  431. {
  432. FloatParseHandling = FloatParseHandling.Decimal
  433. };
  434. joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
  435. bool rtn = encrypt.verify(joRtn, signData);
  436. if (rtn)
  437. {
  438. joRtn = JObject.Parse(decData);
  439. joRtn.Add("success", "True");
  440. }
  441. else
  442. {
  443. Global.writeLog("验签失败,请核查!");
  444. }
  445. }
  446. return joRtn;
  447. }
  448. finally
  449. {
  450. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  451. }
  452. }
  453. catch (Exception ex)
  454. {
  455. if (joRtn["infcode"] == null)
  456. { joRtn.Add("infcode", -1); }
  457. if (joRtn["err_msg"] == null)
  458. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  459. outPar = JsonHelper.Compress(joRtn);
  460. return joRtn;
  461. }
  462. finally
  463. {
  464. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());
  465. this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());
  466. }
  467. }
  468. /// <summary>
  469. /// 保存中心交易日志到数据库
  470. /// </summary>
  471. /// <param name="inParam"></param>
  472. /// <param name="outParam"></param>
  473. /// <param name="inParamPlain"></param>
  474. /// <param name="outParamPlain"></param>
  475. private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
  476. {
  477. dynamic joIris = new JObject();
  478. string sRtn = "";
  479. try
  480. {
  481. //解析postContent,插入医保交易日志表
  482. JObject joInParam = new JObject(JObject.Parse(inParam));
  483. //解包
  484. JObject joIn = Utils.removeWrapper(joInParam);
  485. JObject joOut = new JObject(JObject.Parse(outParam));
  486. JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
  487. JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
  488. JArray jaParams = new JArray();
  489. JObject joParam = new JObject();
  490. joParam.Add("inParam", JObject.FromObject(joIn));
  491. joParam.Add("outParam", JObject.FromObject(joOut));
  492. joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
  493. joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
  494. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  495. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  496. joParam.Add("updateUserID", Global.user.ID);
  497. joParam.Add("psn_no", Global.pat.psn_no);
  498. jaParams.Add(joParam);
  499. joIris.code = "09010021";
  500. joIris.Add("params", jaParams);
  501. //InvokeHelper invoker = new InvokeHelper();
  502. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  503. }
  504. catch (Exception ex)
  505. {
  506. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  507. Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
  508. }
  509. }
  510. #endregion
  511. #region 电子处方
  512. /// </summary>
  513. /// <param name="funNO"></param>
  514. /// <param name="data"></param>
  515. /// <returns></returns>
  516. public JObject invokeEPCenterService(string funNO, string data)
  517. {
  518. JObject joRtn = new JObject();
  519. string outPar = "";
  520. try
  521. {
  522. Global.curEvt.URL = Global.inf.ecPrescURL;
  523. switch (funNO)
  524. {
  525. case "7101":
  526. {
  527. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/uploadChk";
  528. break;
  529. }
  530. case "7102":
  531. {
  532. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxFixmedinsSign";
  533. break;
  534. }
  535. case "7103":
  536. {
  537. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxFileUpld";
  538. break;
  539. }
  540. case "7104":
  541. {
  542. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxUndo";
  543. break;
  544. }
  545. case "7105":
  546. {
  547. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/hospRxDetlQuery";
  548. break;
  549. }
  550. case "7106":
  551. {
  552. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxChkInfoQuery";
  553. break;
  554. }
  555. case "7107":
  556. {
  557. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxSetlInfoQuery";
  558. break;
  559. }
  560. case "7108":
  561. {
  562. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxChkInfoCallback";
  563. break;
  564. }
  565. case "7109":
  566. {
  567. Global.curEvt.URL = Global.curEvt.URL + "/fixmedins/rxSetlInfoCallback";
  568. break;
  569. }
  570. }
  571. //Global.curEvt.URL = Global.inf.centerURL;
  572. joRtn = invokeEPCenterService(data);
  573. outPar = JsonHelper.Compress(joRtn);
  574. return joRtn;
  575. }
  576. catch (Exception ex)
  577. {
  578. if (joRtn["infcode"] == null)
  579. { joRtn.Add("infcode", -1); }
  580. if (joRtn["err_msg"] == null)
  581. { joRtn.Add("err_msg", "invokeCenterServicePresCir(3):" + ex.Message); }
  582. outPar = JsonHelper.Compress(joRtn);
  583. return joRtn;
  584. }
  585. finally
  586. {
  587. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  588. //this.saveCenterLog(JsonHelper.Compress(data), outPar, JsonHelper.Compress(data), outPar);
  589. }
  590. }
  591. /// <summary>
  592. /// 医保电子处方流转调用服务
  593. /// </summary>
  594. /// <param name="data"></param>
  595. /// <returns></returns>
  596. private JObject invokeEPCenterService(string data)
  597. {
  598. string postContent = "";
  599. JObject joRtn = new JObject();
  600. try
  601. {
  602. string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  603. string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  604. //内容类型
  605. //Signer signer = new Signer();
  606. //signer.Key = Global.inf.privateKey; //应用编码
  607. //signer.Secret = Global.inf.secretKey; //secretKey 私钥
  608. //HttpRequest Resquest = new HttpRequest("POST", new Uri(Global.curEvt.URL));
  609. //Resquest.headers.Add("charset", "UTF-8");
  610. //Resquest.headers.Add("x-hw-id", signer.Key);
  611. //Resquest.headers.Add("x-tif-timestamp", timestamp);
  612. //Resquest.headers.Add("x-tif-passid", signer.Key);
  613. //Resquest.headers.Add("x-tif-nonce", nonce);
  614. //Resquest.body = signData;
  615. //HttpWebRequest req = signer.Sign(Resquest);
  616. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  617. req.Method = "POST";
  618. req.ContentType = "application/json;charset=utf8";
  619. req.Timeout = 5 * 10000;
  620. try
  621. {
  622. var writer = new StreamWriter(req.GetRequestStream());
  623. writer.Write(data);
  624. writer.Flush();
  625. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  626. StreamReader reader = new StreamReader(resp.GetResponseStream());
  627. string RtnStr = reader.ReadToEnd();
  628. joRtn = JObject.Parse(RtnStr);
  629. // 解密返回值
  630. EncryptHelper encrypt = new EncryptHelper(Global.inf.appId, Global.inf.secretKey, Global.inf.publicKey, Global.inf.privateKey);
  631. string encData = JsonHelper.getDestValue(joRtn, "encData");
  632. string signData = JsonHelper.getDestValue(joRtn, "signData");
  633. Global.writeLog("【密文出参】:\r\n" + RtnStr);
  634. if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
  635. {
  636. joRtn.Remove("encData");
  637. joRtn.Remove("signData");
  638. joRtn.Remove("data");
  639. //解密
  640. string decData = encrypt.decrypt(encData);
  641. // 验签
  642. JsonConvert.DefaultSettings = () => new JsonSerializerSettings
  643. {
  644. FloatParseHandling = FloatParseHandling.Decimal
  645. };
  646. joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
  647. bool rtn = encrypt.verify(joRtn, signData);
  648. if (rtn)
  649. {
  650. Global.writeLog("【明文出参】:\r\n" + decData);
  651. joRtn = JObject.Parse(decData);
  652. joRtn.Add("code", 0);
  653. joRtn.Add("message", "成功");
  654. }
  655. else
  656. {
  657. Global.writeLog("验签失败,请核查!");
  658. }
  659. }
  660. return joRtn;
  661. }
  662. catch (WebException e)
  663. {
  664. HttpWebResponse resp = (HttpWebResponse)e.Response;
  665. if (resp != null)
  666. {
  667. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(a)", new StreamReader(resp.GetResponseStream()).ReadToEnd() + "异常内容:" + e.Message);
  668. }
  669. else
  670. {
  671. return JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常(b)", e.Message);
  672. }
  673. }
  674. }
  675. catch (Exception ex)
  676. {
  677. postContent = "调用中心服务异常" + ex.Message;
  678. joRtn.Add("infcode", -1);
  679. joRtn.Add("err_msg", "invokeCenterService(Exception_Last):" + ex.Message);
  680. return joRtn;
  681. }
  682. }
  683. #endregion
  684. }
  685. }