InvokeHelper.cs 47 KB

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