InvokeHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 PTMedicalInsurance.Forms;
  27. namespace PTMedicalInsurance.Helper
  28. {
  29. public class InvokeHelper
  30. {
  31. private string serviceURL;
  32. private string authorization;
  33. public InvokeHelper()
  34. {
  35. if (string.IsNullOrEmpty(Global.inf.centerURL))
  36. {
  37. IniFile ini = new IniFile(Global.curEvt.path + @"\CenterServiceURL.ini");
  38. Global.inf.centerURL = ini.ReadValue("CENTER", "url");
  39. Global.inf.uploadURL = ini.ReadValue("CENTER", "upload");
  40. Global.inf.downURL = ini.ReadValue("CENTER", "download");
  41. Global.inf.ecURL = ini.ReadValue("CENTER", "ecToken");
  42. }
  43. }
  44. #region 内部服务调用
  45. /// <summary>
  46. /// iris服务调用的封装
  47. /// </summary>
  48. /// <param name="data"></param>
  49. /// <returns></returns>
  50. public JObject invokeIrisService(string data, string serviceDesc)
  51. {
  52. string rtn = "", url = "";
  53. JObject joRtn = new JObject();
  54. try
  55. {
  56. //先根据用户请求的uri构造请求地址
  57. url = serviceURL;
  58. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  59. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  60. //创建Web访问对象
  61. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  62. //把用户传过来的数据转成“UTF-8”的字节流
  63. byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
  64. //添加头部信息
  65. myRequest.Method = "POST";
  66. myRequest.ContentLength = buf.Length;
  67. myRequest.ContentType = "application/json";
  68. myRequest.Headers.Add("Authorization", authorization);
  69. myRequest.MaximumAutomaticRedirections = 1;
  70. myRequest.AllowAutoRedirect = true;
  71. //发送请求
  72. Stream stream = myRequest.GetRequestStream();
  73. stream.Write(buf, 0, buf.Length);
  74. stream.Close();
  75. //获取接口返回值
  76. //通过Web访问对象获取响应内容
  77. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  78. rtn = getResponseData(myResponse);
  79. joRtn = JObject.Parse(rtn);
  80. return joRtn;
  81. }
  82. catch (Exception ex)
  83. {
  84. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  85. rtn = JsonConvert.SerializeObject(joRtn);
  86. return joRtn;
  87. }
  88. }
  89. /// <summary>
  90. /// HIS服务调用的封装
  91. /// </summary>
  92. /// <param name="data"></param>
  93. /// <returns></returns>
  94. ///
  95. public JObject invokeHISService(string data, string serviceDesc)
  96. {
  97. JObject joRtn = new JObject();
  98. try
  99. {
  100. //先根据用户请求的uri构造请求地址
  101. serviceURL = string.Format("{0}/{1}", Global.hisConfig.ip, Global.hisConfig.url);
  102. authorization = Global.hisConfig.authorization;
  103. joRtn = invokeIrisService(data, serviceDesc);
  104. return joRtn;
  105. }
  106. catch (Exception ex)
  107. {
  108. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  109. return joRtn;
  110. }
  111. finally
  112. {
  113. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), JsonHelper.Compress(joRtn));
  114. }
  115. }
  116. /// <summary>
  117. /// 医保平台服务调用的封装
  118. /// </summary>
  119. /// <param name="data"></param>
  120. /// <returns></returns>
  121. public JObject invokeInsuService(string data, string serviceDesc)
  122. {
  123. string rtn = "";
  124. JObject joRtn = new JObject();
  125. try
  126. {
  127. //先根据用户请求的uri构造请求地址
  128. serviceURL = string.Format("{0}/{1}", Global.insuConfig.ip, Global.insuConfig.url);
  129. authorization = Global.insuConfig.authorization;
  130. joRtn = invokeIrisService(data, serviceDesc);
  131. rtn = JsonConvert.SerializeObject(joRtn);
  132. //if (serviceDesc == "插入签到信息")
  133. //{
  134. // MessageBox.Show("插入签到信息入参:" + data +"|返回值:"+ rtn.ToString()+"|"+ Global.insuConfig.url);
  135. //}
  136. return joRtn;
  137. }
  138. catch (Exception ex)
  139. {
  140. joRtn = JsonHelper.setExceptionJson(-1, serviceDesc, ex.Message);
  141. rtn = JsonConvert.SerializeObject(joRtn);
  142. return joRtn;
  143. }
  144. finally
  145. {
  146. Global.writeLog_Iris(serviceDesc + "(" + serviceURL + ")" + "Authorization:" + (authorization), JsonHelper.Compress(data), rtn);
  147. }
  148. }
  149. private string getResponseData(HttpWebResponse response)
  150. {
  151. string data = "";
  152. if (response != null)
  153. {
  154. Stream s = response.GetResponseStream();
  155. StreamReader sRead = new StreamReader(s);
  156. data = sRead.ReadToEnd();
  157. sRead.Close();
  158. response.Close();
  159. }
  160. return data;
  161. }
  162. #endregion
  163. #region 医保中心调用
  164. /// <summary>
  165. /// 仅辅助交易、自定义交易使用
  166. /// </summary>
  167. /// <param name="funNo"></param>
  168. /// <param name="joInput"></param>
  169. /// <returns></returns>
  170. public JObject invokeCenterAgentService(string funNo, JObject joInput)
  171. {
  172. Global.curEvt.URL = Global.inf.centerURL + Global.curEvt.funNo;
  173. string data = JsonHelper.setCenterInpar(funNo, joInput);
  174. //if (funNo.StartsWith("35"))
  175. //{
  176. // // 进销存测试
  177. // Global.curEvt.URL = "http://10.42.149.235:9091/hss/api/hiinterface";
  178. //}
  179. return invokeCenterService(data);
  180. }
  181. private JObject invokeCenterService(string data, ModeEnum mode = ModeEnum.REST)
  182. {
  183. JObject joRtn = new JObject();
  184. try
  185. {
  186. IInvokeCenter center = new InvokeCenterFactory().create(mode);
  187. string outputData = "";
  188. string errMsg = "";
  189. int iInt = center.Init(ref errMsg);
  190. if (iInt == 0)
  191. {
  192. iInt = center.Business(data, ref outputData, ref errMsg);
  193. if (iInt == 0 && !string.IsNullOrEmpty(outputData))
  194. {
  195. try
  196. {
  197. joRtn = JObject.Parse(outputData);
  198. }
  199. catch (Exception ex)
  200. {
  201. joRtn.Add("infcode", iInt);
  202. joRtn.Add("err_msg", "返回参数异常:" + outputData);
  203. }
  204. }
  205. else
  206. {
  207. joRtn.Add("infcode", iInt);
  208. joRtn.Add("err_msg", outputData);
  209. return joRtn;
  210. }
  211. return joRtn;
  212. }
  213. else
  214. {
  215. joRtn.Add("infcode", -1);
  216. joRtn.Add("err_msg", "医保动态库初始化失败invokeInitByDLL:" + errMsg);
  217. return joRtn;
  218. }
  219. }
  220. finally
  221. {
  222. Global.writeLog("调用中心:", JsonHelper.Compress(data), joRtn.ToString());
  223. // 保存到服务器
  224. this.saveCenterLog(JsonHelper.Compress(data),joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  225. }
  226. }
  227. private void prepareCallURI(TradeEnum trade)
  228. {
  229. Global.curEvt.funNo = trade.GetCode() ;
  230. string prefix = Global.inf.centerURL;
  231. switch (trade)
  232. {
  233. case TradeEnum.FileUpload:
  234. prefix = Global.inf.uploadURL??Global.inf.centerURL;
  235. break;
  236. case TradeEnum.FileDownload:
  237. prefix = Global.inf.downURL;
  238. break;
  239. default:
  240. prefix = Global.inf.centerURL;
  241. break;
  242. }
  243. // 根据情况确实是否需要加funNo
  244. Global.curEvt.URL = prefix + Global.curEvt.funNo;
  245. }
  246. /// <summary>
  247. /// 通过枚举转换要调用的接口
  248. /// </summary>
  249. /// <param name="trade"></param>
  250. /// <param name="data"></param>
  251. /// <returns></returns>
  252. public JObject invokeCenterService(TradeEnum trade, JObject joInput)
  253. {
  254. string funNo = trade.GetCode();
  255. // 入参统一转换
  256. JObject request = Utils.ConvertRequest<JObject>(trade,joInput);
  257. string data = JsonHelper.toJsonString(request);
  258. // 统一封装请求头
  259. if (trade.GetMode() == ModeEnum.REST)
  260. {
  261. data = JsonHelper.setCenterInpar(funNo, request);
  262. }
  263. JObject joRtn = new JObject();
  264. // 调试模式
  265. if (Global.curEvt.enabledDebug)
  266. {
  267. CenterResult center = new CenterResult();
  268. center.setTradeNo(trade.GetCode(), data);
  269. if (center.ShowDialog() == DialogResult.OK)
  270. {
  271. // 接口实际返回数据
  272. string outPar = center.returnData;
  273. joRtn = JObject.Parse(outPar);
  274. }
  275. }
  276. else
  277. {
  278. prepareCallURI(trade);
  279. joRtn = invokeCenterService(data, trade.GetMode());
  280. }
  281. // 返回结果统一转换
  282. joRtn = Utils.ConvertResponse<JObject>(trade,joRtn);
  283. return joRtn;
  284. }
  285. /// <summary>
  286. /// 医保目录txt文件下载
  287. /// </summary>
  288. /// <param name="data"></param>
  289. /// <returns></returns>
  290. public JObject DownloadCenterFile(string data, ModeEnum mode = ModeEnum.REST)
  291. {
  292. //download file
  293. IInvokeCenter center = new InvokeCenterFactory().create(mode);
  294. string outputMsg = "";
  295. JObject joRtn = new JObject();
  296. int rtnCode = center.DownloadFile(data, ref outputMsg);
  297. if(rtnCode==0)
  298. {
  299. joRtn = JObject.Parse(outputMsg);
  300. }
  301. else
  302. {
  303. joRtn.Add("infcode", -1);
  304. joRtn.Add("err_msg", "下载文件失败DownloadFile:" + outputMsg);
  305. }
  306. return joRtn;
  307. }
  308. #endregion
  309. #region 移动
  310. /// <summary>
  311. /// 移动
  312. /// </summary>
  313. /// <param name="funNO"></param>
  314. /// <param name="data"></param>
  315. /// <returns></returns>
  316. public JObject invokeMPService(string funNO, string data)
  317. {
  318. JObject joRtn = new JObject();
  319. String outPar = "";
  320. try
  321. {
  322. Global.curEvt.URL = Global.inf.centerURL + funNO;
  323. joRtn = invokeCenterService(data);
  324. outPar = JsonHelper.Compress(joRtn);
  325. return joRtn;
  326. }
  327. catch (Exception ex)
  328. {
  329. if (joRtn["infcode"] == null)
  330. { joRtn.Add("infcode", -1); }
  331. if (joRtn["err_msg"] == null)
  332. { joRtn.Add("err_msg", "invokeCenterService(3):" + ex.Message); }
  333. outPar = JsonHelper.Compress(joRtn);
  334. return joRtn;
  335. }
  336. finally
  337. {
  338. Global.writeLog(funNO + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(data), joRtn.ToString());
  339. this.saveCenterLog(JsonHelper.Compress(data), joRtn.ToString(), JsonHelper.Compress(data), joRtn.ToString());
  340. }
  341. }
  342. /// <summary>
  343. /// 保存中心交易日志到数据库
  344. /// </summary>
  345. /// <param name="inParam"></param>
  346. /// <param name="outParam"></param>
  347. /// <param name="inParamPlain"></param>
  348. /// <param name="outParamPlain"></param>
  349. private void saveCenterLog(string inParam, string outParam, string inParamPlain, string outParamPlain)
  350. {
  351. dynamic joIris = new JObject();
  352. string sRtn = "";
  353. try
  354. {
  355. //解析postContent,插入医保交易日志表
  356. JObject joInParam = new JObject(JObject.Parse(inParam));
  357. //解包
  358. JObject joIn = Utils.removeWrapper(joInParam);
  359. JObject joOut = new JObject(JObject.Parse(outParam));
  360. JObject joInPlain = new JObject(JObject.Parse(inParamPlain));
  361. JObject joOutPlain = new JObject(JObject.Parse(outParamPlain));
  362. JArray jaParams = new JArray();
  363. JObject joParam = new JObject();
  364. joParam.Add("inParam", JObject.FromObject(joIn));
  365. joParam.Add("outParam", JObject.FromObject(joOut));
  366. joParam.Add("inParamPlain", JObject.FromObject(joInPlain));
  367. joParam.Add("outParamPlain", JObject.FromObject(joOutPlain));
  368. joParam.Add("HospitalDr", Global.inf.hospitalDr);
  369. joParam.Add("InterfaceDr", Global.inf.interfaceDr);
  370. joParam.Add("updateUserID", Global.user.ID);
  371. joParam.Add("psn_no", Global.pat.psn_no);
  372. jaParams.Add(joParam);
  373. joIris.code = "09010021";
  374. joIris.Add("params", jaParams);
  375. //InvokeHelper invoker = new InvokeHelper();
  376. sRtn = invokeInsuService(joIris.ToString(), "保存日志到数据库").ToString();
  377. }
  378. catch (Exception ex)
  379. {
  380. sRtn = JsonHelper.setExceptionJson(-100, "保存日志异常", ex.Message).ToString();
  381. Global.writeLog_Iris("保存日志异常:" + sRtn.ToString());
  382. }
  383. }
  384. #endregion
  385. }
  386. }