InvokeHelper.cs 42 KB

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