InvokeHelper.cs 47 KB

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