InvokeHelper.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. 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.inf.centerURL = "http://10.67.240.74:8086/1.0.0/hsa-fsi-" + funNO;
  439. Global.curEvt.URL = Global.inf.centerURL + funNO;
  440. joRtn = invokeCenterService(data);
  441. outPar = JsonHelper.Compress(joRtn);
  442. return joRtn;
  443. }
  444. catch (Exception ex)
  445. {
  446. if (joRtn["infcode"] == null)
  447. { joRtn.Add("infcode", -1); }
  448. if (joRtn["err_msg"] == null)
  449. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  450. outPar = JsonHelper.Compress(joRtn);
  451. return joRtn;
  452. }
  453. finally
  454. {
  455. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  456. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  457. }
  458. }
  459. /// <summary>
  460. /// 设置医保动态库目录
  461. /// </summary>
  462. /// <param name="sPath"></param>
  463. /// <param name="pErrMsg"></param>
  464. /// <returns></returns>
  465. private void invokeSetDirByOCX(ref string pErrMsg)
  466. {
  467. try
  468. {
  469. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  470. //InterfaceBase_Yn.SetDir(IntPath);
  471. }
  472. catch (Exception ex)
  473. {
  474. pErrMsg = "invokeInitByDLL.SetDir 异常:" + ex.Message;
  475. MessageBox.Show(pErrMsg);
  476. }
  477. finally
  478. {
  479. //Global.writeLog("invokeInitByDLL.SetDir设置医保动态库目录(" + IntPath + ")", "", pErrMsg);
  480. }
  481. }
  482. /// <summary>
  483. /// 医保目录txt文件下载
  484. /// </summary>
  485. /// <param name="data"></param>
  486. /// <returns></returns>
  487. public JObject DownloadCenterFile(string data)
  488. {
  489. string error = string.Empty; int errorCode = 0;
  490. string sRtn = "";
  491. try
  492. {
  493. JObject jsonInParam = JObject.Parse(data);
  494. // 去除外wrapper层便于通用
  495. Utils.removeWrapper(jsonInParam);
  496. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  497. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  498. //如果不存在目录,则创建目录
  499. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  500. {
  501. //创建文件夹
  502. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  503. }
  504. if (File.Exists(filePath))
  505. {
  506. File.Delete(filePath);
  507. }
  508. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  509. //创建一个HTTP请求
  510. Global.curEvt.URL = Global.inf.downURL;
  511. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  512. //Post请求方式
  513. request.Method = "POST";
  514. //string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  515. //string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  516. //string InsuHosID = Global.inf.hospitalNO; //医疗机构ID
  517. //string CreditID = Global.inf.CreditID; //服务商统一社会信用代码
  518. //string BusinessID = Global.inf.BusinessID; //服务商ID
  519. //内容类型
  520. request.ContentType = "application/json";
  521. //昆明增加头部信息
  522. //string sTemp = timestamp + BusinessID + nonce;
  523. //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)
  524. //string signature = Encrypt.SHA256EncryptStr(sTemp);
  525. //request.Headers.Add("hsf_signature", signature);
  526. //request.Headers.Add("hsf_timestamp", timestamp);
  527. //request.Headers.Add("hsf_nonce", nonce);
  528. //request.Headers.Add("fixmedins_code", InsuHosID);
  529. //request.Headers.Add("infosyscode", CreditID);
  530. //设置参数,并进行URL编码
  531. string paraUrlCoded = JsonHelper.toJsonString(jsonInParam);
  532. byte[] payload;
  533. //将Json字符串转化为字节
  534. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  535. //设置请求的ContentLength
  536. request.ContentLength = payload.Length;
  537. Stream writer;
  538. try
  539. {
  540. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  541. }
  542. catch (Exception)
  543. {
  544. writer = null;
  545. errorCode = -100;
  546. error = "连接服务器失败!";
  547. }
  548. //将请求参数写入流
  549. writer.Write(payload, 0, payload.Length);
  550. writer.Close();//关闭请求流
  551. // String strValue = "";//strValue为http响应所返回的字符流
  552. //发送请求并获取相应回应数据
  553. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  554. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  555. Stream responseStream = response.GetResponseStream();
  556. //创建本地文件写入流
  557. byte[] bArr = new byte[1024];
  558. int iTotalSize = 0;
  559. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  560. while (size > 0)
  561. {
  562. iTotalSize += size;
  563. fs.Write(bArr, 0, size);
  564. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  565. }
  566. fs.Close();
  567. responseStream.Close();
  568. dynamic joReturn = new JObject();
  569. joReturn.errorCode = errorCode;
  570. joReturn.errorMessage = error;
  571. joReturn.filePath = filePath;
  572. sRtn = joReturn.ToString();
  573. return joReturn;
  574. }
  575. catch (Exception ex)
  576. {
  577. errorCode = -100;
  578. error = ex.Message;
  579. dynamic joReturn = new JObject();
  580. joReturn.errorCode = errorCode;
  581. joReturn.errorMessage = error;
  582. sRtn = joReturn.ToString();
  583. return joReturn;
  584. }
  585. finally
  586. {
  587. Global.writeLog("DownloadCenterFile" +"(" + Global.curEvt.URL + ")", data, sRtn);
  588. }
  589. }
  590. /// <summary>
  591. /// 调用医保动态库初始化 CM 20220925
  592. /// </summary>
  593. /// <param name="fixmedins_code"></param>
  594. /// <param name="infosyscode"></param>
  595. /// <param name="infosyssign"></param>
  596. /// <param name="url"></param>
  597. /// <param name="pErrMsg"></param>
  598. /// <returns>0或小于0</returns>
  599. private int invokeInitByOCX(string fixmedins_code, string infosyscode, string infosyssign, string url, ref string pErrMsg)
  600. {
  601. //pErrMsg = "";
  602. JObject joRtn = new JObject();
  603. try
  604. {
  605. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  606. //string pRtn = InterfaceBase_Yn.Init(fixmedins_code, infosyscode, infosyssign, url);
  607. //joRtn = JObject.Parse(pRtn);
  608. if (joRtn["infcode"].ToString() != "0")
  609. {
  610. pErrMsg = joRtn["err_msg"].ToString();
  611. return -1;
  612. }
  613. else
  614. {
  615. return 0;
  616. }
  617. }
  618. catch (Exception ex)
  619. {
  620. pErrMsg = "invokeInitByDLL.Init 异常:" + ex.Message;
  621. return -1;
  622. }
  623. finally
  624. {
  625. Global.writeLog("invokeInitByDLL.Init医保动态库初始化(" + url + ")", "", pErrMsg);
  626. }
  627. }
  628. /// <summary>
  629. /// 调用医保动态库通用业务函数 CM 20220925
  630. /// </summary>
  631. /// <param name="fixmedins_code"></param>
  632. /// <param name="infosyscode"></param>
  633. /// <param name="infosyssign"></param>
  634. /// <param name="inputData"></param>
  635. /// <param name="outputData"></param>
  636. /// <param name="pErrMsg"></param>
  637. /// <returns>0或小于0</returns>
  638. private int invokeBusiessByOCX(string fixmedins_code, string infosyscode, string infosyssign, string inputData, ref string outputData, ref string pErrMsg)
  639. {
  640. pErrMsg = "";
  641. outputData = "";
  642. JObject joRtn = new JObject();
  643. try
  644. {
  645. try
  646. {
  647. //chsinterfaceyn.chsdllClass InterfaceBase_Yn1 = new chsinterfaceyn.chsdllClass();
  648. //InterfaceBase_Yn1.SetDir(IntPath);
  649. }
  650. catch (Exception ex)
  651. {
  652. pErrMsg = "invokeInitByDLL.SetDir 异常:" + ex.Message;
  653. MessageBox.Show(pErrMsg);
  654. }
  655. finally
  656. {
  657. //Global.writeLog("invokeInitByDLL.SetDir设置医保动态库目录(" + IntPath + ")", "", pErrMsg);
  658. }
  659. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  660. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  661. //调用业务函数
  662. //string pRtn =InterfaceBase_Yn.BusinessHandleW(fixmedins_code, infosyscode, infosyssign, inputData);
  663. //string pRtn = InterfaceBase_Yn.UploadFile(fixmedins_code, infosyscode, infosyssign, Global.inf.fileName,inputData);
  664. //joRtn = JObject.Parse(pRtn);
  665. if (joRtn["infcode"].ToString() != "0")
  666. {
  667. pErrMsg = joRtn["err_msg"].ToString();
  668. //outputData = pRtn;
  669. return -1;
  670. }
  671. else
  672. {
  673. //outputData = pRtn;
  674. return 0;
  675. }
  676. }
  677. catch (Exception ex)
  678. {
  679. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  680. return -1;
  681. }
  682. finally
  683. {
  684. Global.writeLog("invokeInitByDLL.BusinessHandle医保动态库通用业务函数", inputData, outputData);
  685. }
  686. }
  687. /// <summary>
  688. /// 医保动态库下载目录文件
  689. /// </summary>
  690. /// <param name="data"></param>
  691. /// <returns></returns>
  692. public JObject DownloadCenterFileByDll(string data)
  693. {
  694. string error = string.Empty; int errorCode = 0;
  695. string sRtn = "";
  696. try
  697. {
  698. JObject jsonInParam = JObject.Parse(data);
  699. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  700. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  701. //如果不存在目录,则创建目录
  702. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  703. {
  704. //创建文件夹
  705. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  706. }
  707. if (File.Exists(filePath))
  708. {
  709. File.Delete(filePath);
  710. }
  711. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  712. //创建一个HTTP请求
  713. Global.curEvt.URL = Global.inf.centerURL;
  714. int iRes = invokeDownloadFileByDLL(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, data.ToString(),ref sRtn, ref error);
  715. if (iRes == 0)
  716. {
  717. dynamic joReturn = new JObject();
  718. joReturn.errorCode = errorCode;
  719. joReturn.errorMessage = error;
  720. joReturn.filePath = filePath;
  721. sRtn = joReturn.ToString();
  722. return joReturn;
  723. }
  724. else
  725. {
  726. errorCode = -100;
  727. dynamic joReturn = new JObject();
  728. joReturn.errorCode = errorCode;
  729. joReturn.errorMessage = error;
  730. sRtn = joReturn.ToString();
  731. return joReturn;
  732. }
  733. }
  734. catch (Exception ex)
  735. {
  736. errorCode = -100;
  737. error = ex.Message;
  738. dynamic joReturn = new JObject();
  739. joReturn.errorCode = errorCode;
  740. joReturn.errorMessage = error;
  741. sRtn = joReturn.ToString();
  742. return joReturn;
  743. }
  744. finally
  745. {
  746. Global.writeLog("DownloadCenterFile" + "(" + Global.curEvt.URL + ")", data, sRtn);
  747. }
  748. }
  749. /// <summary>
  750. /// 保存中心交易日志到数据库
  751. /// </summary>
  752. /// <param name="inParam"></param>
  753. /// <param name="outParam"></param>
  754. /// <param name="inParamPlain"></param>
  755. /// <param name="outParamPlain"></param>
  756. private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
  757. {
  758. dynamic joIris = new JObject();
  759. string sRtn = "";
  760. try
  761. {
  762. //解析postContent,插入医保交易日志表
  763. JObject joInParam = new JObject(JObject.Parse(inParam));
  764. //解包
  765. JObject joIn = Utils.removeWrapper(joInParam);
  766. JObject joOut = new JObject(JObject.Parse(outParam));
  767. JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
  768. JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
  769. JArray jaParams = new JArray();
  770. JObject joParam = new JObject();
  771. joParam.Add("inParam", JObject.FromObject(joIn));
  772. joParam.Add("outParam", JObject.FromObject(joOut));
  773. joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
  774. joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
  775. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  776. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  777. joParam.Add("updateUserID", Global.user.ID);
  778. joParam.Add("psn_no", Global.pat.psn_no);
  779. jaParams.Add(joParam);
  780. joIris.code = "09010021";
  781. joIris.Add("params", jaParams);
  782. //InvokeHelper invoker = new InvokeHelper();
  783. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  784. }
  785. catch (Exception ex)
  786. {
  787. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  788. Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
  789. }
  790. }
  791. /**********************************************************调用DLL方式**************************************************************/
  792. public int InvokeInitByDLL(ref string pErrMsg)
  793. {
  794. //string pErrMsg = "";
  795. int pRtn =-1;
  796. JObject joRtn = new JObject();
  797. try
  798. {
  799. StringBuilder outSb = new StringBuilder(40960);
  800. pRtn = Init(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, Global.inf.centerURL, outSb);
  801. if (pRtn != 0)
  802. {
  803. pErrMsg = outSb.ToString();
  804. return -1;
  805. }
  806. else
  807. {
  808. return 0;
  809. }
  810. }
  811. catch (Exception ex)
  812. {
  813. pErrMsg = "invokeInitByDLL.Init 异常:" + ex.Message;
  814. return -1;
  815. }
  816. finally
  817. {
  818. Global.writeLog("InvokeInitByDLL(" + Global.inf.centerURL + ")", Global.inf.CreditID +":" + Global.inf.BusinessID,pRtn.ToString() + pErrMsg);
  819. }
  820. }
  821. private int invokeBusiessByDLL(string inputData, ref string outputData, ref string pErrMsg)
  822. {
  823. pErrMsg = "";
  824. outputData = "";
  825. JObject joRtn = new JObject();
  826. try
  827. {
  828. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  829. StringBuilder errmsgSb = new StringBuilder(4096);
  830. StringBuilder outSb = new StringBuilder(40960);
  831. //调用业务函数
  832. int pRtn = BusinessHandle(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, inputData, outSb, errmsgSb);
  833. if (pRtn != 0)
  834. {
  835. outputData = outSb.ToString();
  836. pErrMsg = errmsgSb.ToString();
  837. return -1;
  838. }
  839. else
  840. {
  841. outputData = outSb.ToString();
  842. return 0;
  843. }
  844. }
  845. catch (Exception ex)
  846. {
  847. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  848. return -1;
  849. }
  850. finally
  851. {
  852. Global.writeLog("CreditID11", Global.inf.CreditID, Global.inf.BusinessID);
  853. Global.writeLog("invokeInitByDLL.BusinessHandle医保动态库通用业务函数", inputData, outputData);
  854. }
  855. }
  856. private int invokeBusiessWByDLL(string inputData, ref string outputData, ref string pErrMsg)
  857. {
  858. pErrMsg = "";
  859. outputData = "";
  860. JObject joRtn = new JObject();
  861. try
  862. {
  863. //inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  864. StringBuilder errmsgSb = new StringBuilder(4096);
  865. StringBuilder outSb = new StringBuilder(40960);
  866. StringBuilder sbHospitalNO = new StringBuilder(Global.inf.hospitalNO);
  867. StringBuilder sbCreditID = new StringBuilder(Global.inf.CreditID);
  868. StringBuilder sbBusinessID = new StringBuilder(Global.inf.BusinessID);
  869. StringBuilder sbInput = new StringBuilder(inputData);
  870. //调用业务函数
  871. int pRtn = BusinessHandleW(sbHospitalNO, sbCreditID, sbBusinessID, sbInput, outSb, errmsgSb);
  872. if (pRtn != 0)
  873. {
  874. outputData = outSb.ToString();
  875. pErrMsg = errmsgSb.ToString();
  876. return -1;
  877. }
  878. else
  879. {
  880. outputData = outSb.ToString();
  881. return 0;
  882. }
  883. }
  884. catch (Exception ex)
  885. {
  886. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  887. return -1;
  888. }
  889. finally
  890. {
  891. Global.writeLog("CreditID12", Global.inf.CreditID, Global.inf.BusinessID);
  892. Global.writeLog("invokeInitByDLL.BusinessHandleW医保动态库通用业务函数", inputData, outputData);
  893. }
  894. }
  895. private int invokeUploadFileByDLL(string inputData, ref string outputData, ref string pErrMsg)
  896. {
  897. pErrMsg = "";
  898. outputData = "";
  899. JObject joRtn = new JObject();
  900. try
  901. {
  902. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  903. StringBuilder errmsgSb = new StringBuilder(4096);
  904. StringBuilder outSb = new StringBuilder(40960);
  905. //调用业务函数
  906. int pRtn = UploadFile(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, Global.inf.fileName,inputData, outSb, errmsgSb);
  907. if (pRtn != 0)
  908. {
  909. outputData = outSb.ToString();
  910. pErrMsg = errmsgSb.ToString();
  911. return -1;
  912. }
  913. else
  914. {
  915. outputData = outSb.ToString();
  916. return 0;
  917. }
  918. }
  919. catch (Exception ex)
  920. {
  921. pErrMsg = "invokeUploadFileByDLL.UploadFile 异常:" + ex.Message;
  922. return -1;
  923. }
  924. finally
  925. {
  926. Global.writeLog("invokeUploadFileByDLL.UploadFile医保动态库上传业务函数", inputData, outputData);
  927. }
  928. }
  929. }
  930. }