InvokeHelper.cs 46 KB

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