InvokeHelper.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. if (Global.inf.interfaceDr == 12)//省(瑶海)
  100. {
  101. Global.inf.uploadURL = "http://10.88.29.31:80/fsi/api/fileupload/upload";
  102. Global.inf.downURL = "http://10.88.29.31:80/fsi/api/fileupload/download";
  103. Global.inf.ecURL = "http://10.66.159.55:8080/localcfc/api/hsecfc/localQrCodeQuery";
  104. }
  105. if (Global.inf.interfaceDr == 15)//省
  106. {
  107. Global.inf.uploadURL = "http://10.66.155.176:80/fsi/api/fileupload/upload";
  108. Global.inf.downURL = "http://10.66.155.176:80/fsi/api/fileupload/download";
  109. Global.inf.ecURL = "http://10.66.159.55:8080/localcfc/api/hsecfc/localQrCodeQuery";
  110. }
  111. if (Global.inf.interfaceDr == 16)//市(瑶海)
  112. {
  113. Global.inf.uploadURL = "http://10.67.240.56:80/fsi/api/fileupload/upload";
  114. Global.inf.downURL = "http://10.67.240.56:80/fsi/api/fileupload/download";
  115. Global.inf.ecURL = "http://10.66.159.55:8080/localcfc/api/hsecfc/localQrCodeQuery";
  116. }
  117. if (Global.inf.interfaceDr == 17)//市
  118. {
  119. Global.inf.uploadURL = "http://10.67.240.56:80/fsi/api/fileupload/upload";
  120. Global.inf.downURL = "http://10.67.240.56:80/fsi/api/fileupload/download";
  121. Global.inf.ecURL = "http://10.66.159.55:8080/localcfc/api/hsecfc/localQrCodeQuery";
  122. }
  123. // Global.inf.uploadURL = ini.ReadValue("CENTER", "upload");
  124. //Global.inf.downURL = ini.ReadValue("CENTER", "download");
  125. // Global.inf.ecURL = ini.ReadValue("CENTER", "ecToken");
  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. switch (funNo)
  418. {
  419. case "4101A":
  420. case "3101":
  421. case "3102":
  422. prefix = "http://10.66.155.173:8086/1.0.0/hsa-fsi-";
  423. break;
  424. default:
  425. prefix = Global.inf.centerURL;
  426. break;
  427. }
  428. //移动支付使用医保测试环境【测试】,正式使用时须去掉
  429. if (funNo.StartsWith("62") || funNo.StartsWith("63") || funNo.StartsWith("64"))
  430. {
  431. prefix = "http://10.67.248.214:8086/1.0.0/hsa-fsi-"; //测试
  432. }
  433. Global.curEvt.URL = prefix + funNo;
  434. }
  435. /// <summary>
  436. /// 这个是下载目录用的invokeCenterService
  437. /// </summary>
  438. /// <param name="funNO"></param>
  439. /// <param name="data"></param>
  440. /// <returns></returns>
  441. public JObject invokeCenterService(string funNO, string data)
  442. {
  443. JObject joRtn = new JObject();
  444. try
  445. {
  446. // 动态调试模式
  447. if (Global.curEvt.enabledDebug)
  448. {
  449. CenterResult center = new CenterResult();
  450. center.setTradeNo(funNO);
  451. if (center.ShowDialog() == DialogResult.OK)
  452. {
  453. string outPar = center.returnData;
  454. return JObject.Parse(outPar);
  455. }
  456. }
  457. setCenterURL(funNO);
  458. joRtn = invokeCenterService(data);
  459. return joRtn;
  460. }
  461. catch (Exception ex)
  462. {
  463. if (joRtn["infcode"] == null)
  464. { joRtn.Add("infcode", -1); }
  465. if (joRtn["err_msg"] == null)
  466. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  467. return joRtn;
  468. }
  469. finally
  470. {
  471. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  472. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  473. }
  474. }
  475. /// <summary>
  476. /// 移动
  477. /// </summary>
  478. /// <param name="funNO"></param>
  479. /// <param name="data"></param>
  480. /// <returns></returns>
  481. ///
  482. public JObject invokeMPService(string funNO, string data)
  483. {
  484. return invokeMPService(funNO,JObject.Parse(data));
  485. }
  486. public JObject invokeMPService(string funNO, JObject joInput)
  487. {
  488. JObject joRtn = new JObject();
  489. String outPar = "";
  490. try
  491. {
  492. string url = "";
  493. switch (funNO)
  494. {
  495. case "6201":
  496. url = "/org/local/api/hos/uldFeeInfo";
  497. break;
  498. case "6202":
  499. url = "/org/local/api/hos/pay_order";
  500. break;
  501. case "6203":
  502. url = "/org/local/api/hos/refund_Order";
  503. break;
  504. case "6301":
  505. url = "/org/local/api/hos/query_order_info";
  506. break;
  507. case "6401":
  508. url = "/org/local/api/hos/revoke_order";
  509. break;
  510. default:
  511. break;
  512. }
  513. EncryptHelper encrypt = new EncryptHelper();
  514. string data = JsonHelper.setMPCenterInpar(funNO, joInput);
  515. // 移动支付地址
  516. Global.curEvt.URL = Global.inf.mobilePayURL + url;
  517. // 动态调试模式
  518. if (Global.curEvt.enabledDebug)
  519. {
  520. CenterResult center = new CenterResult();
  521. center.setTradeNo(funNO);
  522. if (center.ShowDialog() == DialogResult.OK)
  523. {
  524. outPar = center.returnData;
  525. return JObject.Parse(outPar);
  526. }
  527. }
  528. try
  529. {
  530. joRtn = invokeCenterService(data);
  531. Global.writeLog(funNO + "【密文出参】:\r\n" + joRtn.ToString());
  532. string encData = JsonHelper.getDestValue(joRtn, "encData");
  533. string signData = JsonHelper.getDestValue(joRtn, "signData");
  534. if (!string.IsNullOrEmpty(encData) && !string.IsNullOrEmpty(signData))
  535. {
  536. joRtn.Remove("encData");
  537. joRtn.Remove("signData");
  538. joRtn.Remove("data");
  539. //解密
  540. string decData = encrypt.decrypt(encData);
  541. // 验签
  542. JsonConvert.DefaultSettings = () => new JsonSerializerSettings
  543. {
  544. FloatParseHandling = FloatParseHandling.Decimal
  545. };
  546. joRtn.Add("data", JToken.FromObject(JsonConvert.DeserializeObject(decData)));
  547. bool rtn = encrypt.verify(joRtn, signData);
  548. if (rtn)
  549. {
  550. Global.writeLog("验签通过!");
  551. }
  552. else
  553. {
  554. Global.writeLog("验签失败,请核查!");
  555. }
  556. if (!string.IsNullOrEmpty(decData))
  557. {
  558. Global.writeLog(funNO + "【明文出参】:\r\n" + decData);
  559. joRtn = JObject.Parse(decData);
  560. joRtn.Add("success", "True");
  561. }
  562. }
  563. return joRtn;
  564. }
  565. finally
  566. {
  567. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  568. }
  569. }
  570. catch (Exception ex)
  571. {
  572. if (joRtn["infcode"] == null)
  573. { joRtn.Add("infcode", -1); }
  574. if (joRtn["err_msg"] == null)
  575. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  576. outPar = JsonHelper.Compress(joRtn);
  577. return joRtn;
  578. }
  579. finally
  580. {
  581. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", joInput.ToString(), joRtn.ToString());
  582. this.saveCenterLog(joInput.ToString(), joRtn.ToString(), joInput.ToString(), joRtn.ToString());
  583. }
  584. }
  585. /// <summary>
  586. /// 设置医保动态库目录
  587. /// </summary>
  588. /// <param name="sPath"></param>
  589. /// <param name="pErrMsg"></param>
  590. /// <returns></returns>
  591. private void invokeSetDirByOCX(ref string pErrMsg)
  592. {
  593. try
  594. {
  595. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  596. //InterfaceBase_Yn.SetDir(IntPath);
  597. }
  598. catch (Exception ex)
  599. {
  600. pErrMsg = "invokeInitByDLL.SetDir 异常:" + ex.Message;
  601. MessageBox.Show(pErrMsg);
  602. }
  603. finally
  604. {
  605. //Global.writeLog("invokeInitByDLL.SetDir设置医保动态库目录(" + IntPath + ")", "", pErrMsg);
  606. }
  607. }
  608. /// <summary>
  609. /// 医保目录txt文件下载
  610. /// </summary>
  611. /// <param name="data"></param>
  612. /// <returns></returns>
  613. public JObject DownloadCenterFile(string data)
  614. {
  615. string error = string.Empty; int errorCode = 0;
  616. string sRtn = "";
  617. try
  618. {
  619. JObject jsonInParam = JObject.Parse(data);
  620. // 去除外wrapper层便于通用
  621. Utils.removeWrapper(jsonInParam);
  622. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  623. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  624. //如果不存在目录,则创建目录
  625. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  626. {
  627. //创建文件夹
  628. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  629. }
  630. if (File.Exists(filePath))
  631. {
  632. File.Delete(filePath);
  633. }
  634. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  635. //创建一个HTTP请求
  636. Global.curEvt.URL = Global.inf.downURL;
  637. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  638. //Post请求方式
  639. request.Method = "POST";
  640. //string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  641. //string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  642. //string InsuHosID = Global.inf.hospitalNO; //医疗机构ID
  643. //string CreditID = Global.inf.CreditID; //服务商统一社会信用代码
  644. //string BusinessID = Global.inf.BusinessID; //服务商ID
  645. //内容类型
  646. request.ContentType = "application/json";
  647. //昆明增加头部信息
  648. //string sTemp = timestamp + BusinessID + nonce;
  649. //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)
  650. //string signature = Encrypt.SHA256EncryptStr(sTemp);
  651. //request.Headers.Add("hsf_signature", signature);
  652. //request.Headers.Add("hsf_timestamp", timestamp);
  653. //request.Headers.Add("hsf_nonce", nonce);
  654. //request.Headers.Add("fixmedins_code", InsuHosID);
  655. //request.Headers.Add("infosyscode", CreditID);
  656. //设置参数,并进行URL编码
  657. string paraUrlCoded = JsonHelper.toJsonString(jsonInParam);
  658. byte[] payload;
  659. //将Json字符串转化为字节
  660. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  661. //设置请求的ContentLength
  662. request.ContentLength = payload.Length;
  663. Stream writer;
  664. try
  665. {
  666. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  667. }
  668. catch (Exception)
  669. {
  670. writer = null;
  671. errorCode = -100;
  672. error = "连接服务器失败!";
  673. }
  674. //将请求参数写入流
  675. writer.Write(payload, 0, payload.Length);
  676. writer.Close();//关闭请求流
  677. // String strValue = "";//strValue为http响应所返回的字符流
  678. //发送请求并获取相应回应数据
  679. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  680. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  681. Stream responseStream = response.GetResponseStream();
  682. //创建本地文件写入流
  683. byte[] bArr = new byte[1024];
  684. int iTotalSize = 0;
  685. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  686. while (size > 0)
  687. {
  688. iTotalSize += size;
  689. fs.Write(bArr, 0, size);
  690. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  691. }
  692. fs.Close();
  693. responseStream.Close();
  694. dynamic joReturn = new JObject();
  695. joReturn.errorCode = errorCode;
  696. joReturn.errorMessage = error;
  697. joReturn.filePath = filePath;
  698. sRtn = joReturn.ToString();
  699. return joReturn;
  700. }
  701. catch (Exception ex)
  702. {
  703. errorCode = -100;
  704. error = ex.Message;
  705. dynamic joReturn = new JObject();
  706. joReturn.errorCode = errorCode;
  707. joReturn.errorMessage = error;
  708. sRtn = joReturn.ToString();
  709. return joReturn;
  710. }
  711. finally
  712. {
  713. Global.writeLog("DownloadCenterFile" +"(" + Global.inf.downURL + ")", data, sRtn);
  714. }
  715. }
  716. /// <summary>
  717. /// 调用医保动态库初始化 CM 20220925
  718. /// </summary>
  719. /// <param name="fixmedins_code"></param>
  720. /// <param name="infosyscode"></param>
  721. /// <param name="infosyssign"></param>
  722. /// <param name="url"></param>
  723. /// <param name="pErrMsg"></param>
  724. /// <returns>0或小于0</returns>
  725. private int invokeInitByOCX(string fixmedins_code, string infosyscode, string infosyssign, string url, ref string pErrMsg)
  726. {
  727. //pErrMsg = "";
  728. JObject joRtn = new JObject();
  729. try
  730. {
  731. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  732. //string pRtn = InterfaceBase_Yn.Init(fixmedins_code, infosyscode, infosyssign, url);
  733. //joRtn = JObject.Parse(pRtn);
  734. if (joRtn["infcode"].ToString() != "0")
  735. {
  736. pErrMsg = joRtn["err_msg"].ToString();
  737. return -1;
  738. }
  739. else
  740. {
  741. return 0;
  742. }
  743. }
  744. catch (Exception ex)
  745. {
  746. pErrMsg = "invokeInitByDLL.Init 异常:" + ex.Message;
  747. return -1;
  748. }
  749. finally
  750. {
  751. Global.writeLog("invokeInitByDLL.Init医保动态库初始化(" + url + ")", "", pErrMsg);
  752. }
  753. }
  754. /// <summary>
  755. /// 调用医保动态库通用业务函数 CM 20220925
  756. /// </summary>
  757. /// <param name="fixmedins_code"></param>
  758. /// <param name="infosyscode"></param>
  759. /// <param name="infosyssign"></param>
  760. /// <param name="inputData"></param>
  761. /// <param name="outputData"></param>
  762. /// <param name="pErrMsg"></param>
  763. /// <returns>0或小于0</returns>
  764. private int invokeBusiessByOCX(string fixmedins_code, string infosyscode, string infosyssign, string inputData, ref string outputData, ref string pErrMsg)
  765. {
  766. pErrMsg = "";
  767. outputData = "";
  768. JObject joRtn = new JObject();
  769. try
  770. {
  771. try
  772. {
  773. //chsinterfaceyn.chsdllClass InterfaceBase_Yn1 = new chsinterfaceyn.chsdllClass();
  774. //InterfaceBase_Yn1.SetDir(IntPath);
  775. }
  776. catch (Exception ex)
  777. {
  778. pErrMsg = "invokeInitByDLL.SetDir 异常:" + ex.Message;
  779. MessageBox.Show(pErrMsg);
  780. }
  781. finally
  782. {
  783. //Global.writeLog("invokeInitByDLL.SetDir设置医保动态库目录(" + IntPath + ")", "", pErrMsg);
  784. }
  785. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  786. //chsinterfaceyn.chsdllClass InterfaceBase_Yn = new chsinterfaceyn.chsdllClass();
  787. //调用业务函数
  788. //string pRtn =InterfaceBase_Yn.BusinessHandleW(fixmedins_code, infosyscode, infosyssign, inputData);
  789. //string pRtn = InterfaceBase_Yn.UploadFile(fixmedins_code, infosyscode, infosyssign, Global.inf.fileName,inputData);
  790. //joRtn = JObject.Parse(pRtn);
  791. if (joRtn["infcode"].ToString() != "0")
  792. {
  793. pErrMsg = joRtn["err_msg"].ToString();
  794. //outputData = pRtn;
  795. return -1;
  796. }
  797. else
  798. {
  799. //outputData = pRtn;
  800. return 0;
  801. }
  802. }
  803. catch (Exception ex)
  804. {
  805. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  806. return -1;
  807. }
  808. finally
  809. {
  810. Global.writeLog("invokeInitByDLL.BusinessHandle医保动态库通用业务函数", inputData, outputData);
  811. }
  812. }
  813. /// <summary>
  814. /// 医保动态库下载目录文件
  815. /// </summary>
  816. /// <param name="data"></param>
  817. /// <returns></returns>
  818. public JObject DownloadCenterFileByDll(string data)
  819. {
  820. string error = string.Empty; int errorCode = 0;
  821. string sRtn = "";
  822. try
  823. {
  824. JObject jsonInParam = JObject.Parse(data);
  825. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  826. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  827. //如果不存在目录,则创建目录
  828. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  829. {
  830. //创建文件夹
  831. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  832. }
  833. if (File.Exists(filePath))
  834. {
  835. File.Delete(filePath);
  836. }
  837. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  838. //创建一个HTTP请求
  839. Global.curEvt.URL = Global.inf.centerURL;
  840. int iRes = invokeDownloadFileByDLL(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, data.ToString(),ref sRtn, ref error);
  841. if (iRes == 0)
  842. {
  843. dynamic joReturn = new JObject();
  844. joReturn.errorCode = errorCode;
  845. joReturn.errorMessage = error;
  846. joReturn.filePath = filePath;
  847. sRtn = joReturn.ToString();
  848. return joReturn;
  849. }
  850. else
  851. {
  852. errorCode = -100;
  853. dynamic joReturn = new JObject();
  854. joReturn.errorCode = errorCode;
  855. joReturn.errorMessage = error;
  856. sRtn = joReturn.ToString();
  857. return joReturn;
  858. }
  859. }
  860. catch (Exception ex)
  861. {
  862. errorCode = -100;
  863. error = ex.Message;
  864. dynamic joReturn = new JObject();
  865. joReturn.errorCode = errorCode;
  866. joReturn.errorMessage = error;
  867. sRtn = joReturn.ToString();
  868. return joReturn;
  869. }
  870. finally
  871. {
  872. Global.writeLog("DownloadCenterFile" + "(" + Global.curEvt.URL + ")", data, sRtn);
  873. }
  874. }
  875. /// <summary>
  876. /// 保存中心交易日志到数据库
  877. /// </summary>
  878. /// <param name="inParam"></param>
  879. /// <param name="outParam"></param>
  880. /// <param name="inParamPlain"></param>
  881. /// <param name="outParamPlain"></param>
  882. private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
  883. {
  884. dynamic joIris = new JObject();
  885. string sRtn = "";
  886. try
  887. {
  888. //解析postContent,插入医保交易日志表
  889. JObject joInParam = new JObject(JObject.Parse(inParam));
  890. //解包
  891. JObject joIn = Utils.removeWrapper(joInParam);
  892. JObject joOut = new JObject(JObject.Parse(outParam));
  893. JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
  894. JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
  895. JArray jaParams = new JArray();
  896. JObject joParam = new JObject();
  897. joParam.Add("inParam", JObject.FromObject(joIn));
  898. joParam.Add("outParam", JObject.FromObject(joOut));
  899. joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
  900. joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
  901. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  902. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  903. joParam.Add("updateUserID", Global.user.ID);
  904. joParam.Add("psn_no", Global.pat.psn_no);
  905. jaParams.Add(joParam);
  906. joIris.code = "09010021";
  907. joIris.Add("params", jaParams);
  908. //InvokeHelper invoker = new InvokeHelper();
  909. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  910. }
  911. catch (Exception ex)
  912. {
  913. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  914. Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
  915. }
  916. }
  917. /**********************************************************调用DLL方式**************************************************************/
  918. public int InvokeInitByDLL(ref string pErrMsg)
  919. {
  920. //string pErrMsg = "";
  921. int pRtn =-1;
  922. JObject joRtn = new JObject();
  923. try
  924. {
  925. StringBuilder outSb = new StringBuilder(40960);
  926. pRtn = Init(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, Global.inf.centerURL, outSb);
  927. if (pRtn != 0)
  928. {
  929. pErrMsg = outSb.ToString();
  930. return -1;
  931. }
  932. else
  933. {
  934. return 0;
  935. }
  936. }
  937. catch (Exception ex)
  938. {
  939. pErrMsg = "invokeInitByDLL.Init 异常:" + ex.Message;
  940. return -1;
  941. }
  942. finally
  943. {
  944. Global.writeLog("InvokeInitByDLL(" + Global.inf.centerURL + ")", Global.inf.CreditID +":" + Global.inf.BusinessID,pRtn.ToString() + pErrMsg);
  945. }
  946. }
  947. private int invokeBusiessByDLL(string inputData, ref string outputData, ref string pErrMsg)
  948. {
  949. pErrMsg = "";
  950. outputData = "";
  951. JObject joRtn = new JObject();
  952. try
  953. {
  954. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  955. StringBuilder errmsgSb = new StringBuilder(4096);
  956. StringBuilder outSb = new StringBuilder(40960);
  957. //调用业务函数
  958. int pRtn = BusinessHandle(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, inputData, outSb, errmsgSb);
  959. if (pRtn != 0)
  960. {
  961. outputData = outSb.ToString();
  962. pErrMsg = errmsgSb.ToString();
  963. return -1;
  964. }
  965. else
  966. {
  967. outputData = outSb.ToString();
  968. return 0;
  969. }
  970. }
  971. catch (Exception ex)
  972. {
  973. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  974. return -1;
  975. }
  976. finally
  977. {
  978. Global.writeLog("CreditID11", Global.inf.CreditID, Global.inf.BusinessID);
  979. Global.writeLog("invokeInitByDLL.BusinessHandle医保动态库通用业务函数", inputData, outputData);
  980. }
  981. }
  982. private int invokeBusiessWByDLL(string inputData, ref string outputData, ref string pErrMsg)
  983. {
  984. pErrMsg = "";
  985. outputData = "";
  986. JObject joRtn = new JObject();
  987. try
  988. {
  989. //inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  990. StringBuilder errmsgSb = new StringBuilder(4096);
  991. StringBuilder outSb = new StringBuilder(40960);
  992. StringBuilder sbHospitalNO = new StringBuilder(Global.inf.hospitalNO);
  993. StringBuilder sbCreditID = new StringBuilder(Global.inf.CreditID);
  994. StringBuilder sbBusinessID = new StringBuilder(Global.inf.BusinessID);
  995. StringBuilder sbInput = new StringBuilder(inputData);
  996. //调用业务函数
  997. int pRtn = BusinessHandleW(sbHospitalNO, sbCreditID, sbBusinessID, sbInput, outSb, errmsgSb);
  998. if (pRtn != 0)
  999. {
  1000. outputData = outSb.ToString();
  1001. pErrMsg = errmsgSb.ToString();
  1002. return -1;
  1003. }
  1004. else
  1005. {
  1006. outputData = outSb.ToString();
  1007. return 0;
  1008. }
  1009. }
  1010. catch (Exception ex)
  1011. {
  1012. pErrMsg = "invokeInitByDLL.BusinessHandle 异常:" + ex.Message;
  1013. return -1;
  1014. }
  1015. finally
  1016. {
  1017. Global.writeLog("CreditID12", Global.inf.CreditID, Global.inf.BusinessID);
  1018. Global.writeLog("invokeInitByDLL.BusinessHandleW医保动态库通用业务函数", inputData, outputData);
  1019. }
  1020. }
  1021. private int invokeUploadFileByDLL(string inputData, ref string outputData, ref string pErrMsg)
  1022. {
  1023. pErrMsg = "";
  1024. outputData = "";
  1025. JObject joRtn = new JObject();
  1026. try
  1027. {
  1028. inputData = inputData.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  1029. StringBuilder errmsgSb = new StringBuilder(4096);
  1030. StringBuilder outSb = new StringBuilder(40960);
  1031. //调用业务函数
  1032. int pRtn = UploadFile(Global.inf.hospitalNO, Global.inf.CreditID, Global.inf.BusinessID, Global.inf.fileName,inputData, outSb, errmsgSb);
  1033. if (pRtn != 0)
  1034. {
  1035. outputData = outSb.ToString();
  1036. pErrMsg = errmsgSb.ToString();
  1037. return -1;
  1038. }
  1039. else
  1040. {
  1041. outputData = outSb.ToString();
  1042. return 0;
  1043. }
  1044. }
  1045. catch (Exception ex)
  1046. {
  1047. pErrMsg = "invokeUploadFileByDLL.UploadFile 异常:" + ex.Message;
  1048. return -1;
  1049. }
  1050. finally
  1051. {
  1052. Global.writeLog("invokeUploadFileByDLL.UploadFile医保动态库上传业务函数", inputData, outputData);
  1053. }
  1054. }
  1055. }
  1056. }