InvokeHelper.cs 41 KB

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