InvokeHelper.cs 42 KB

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