InvokeHelper.cs 42 KB

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