InvokeRestCenter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Common;
  3. using PTMedicalInsurance.Variables;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Runtime.InteropServices;
  12. using System.Reflection;
  13. using System.Windows.Forms;
  14. namespace PTMedicalInsurance.Helper
  15. {
  16. class InvokeRestCenter : IInvokeCenter
  17. {
  18. string progID = "yinhai.yh_hb_sctr";
  19. System.Type YinHaiComType;
  20. object YinHaiComInstance;
  21. public int yh_hb_call(string infno, string input, out string output)
  22. {
  23. try
  24. {
  25. Type YinHaiComType = System.Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
  26. if (YinHaiComType != null)
  27. {
  28. //创建实例,不能再次创建,否则会提示没有初始化
  29. object YinHaiComInstance = System.Activator.CreateInstance(YinHaiComType);
  30. if (YinHaiComInstance != null)
  31. {
  32. Global.writeLog("实例创建成功,准备调用yh_hb_call服务");
  33. }
  34. else
  35. {
  36. output = "实例不存在!";
  37. Global.writeLog("实例不存在");
  38. return -1;
  39. }
  40. //GlobalVariables.writeLog("入参infno:" + infno);
  41. //GlobalVariables.writeLog("入参input:" + input);
  42. //设置需要设置的参数值
  43. object[] ParamArray = new object[3];
  44. ParamArray[0] = infno;
  45. ParamArray[1] = input;
  46. ParamArray[2] = "";
  47. ParameterModifier[] ParamMods = new ParameterModifier[1];
  48. ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  49. //ParamMods[0][0] = false;
  50. //ParamMods[0][1] = false;
  51. ParamMods[0][2] = true;
  52. YinHaiComType.InvokeMember("yh_hb_call", // 接口函数名
  53. BindingFlags.Default | BindingFlags.InvokeMethod,
  54. null,
  55. YinHaiComInstance, // 调用的COM组件
  56. ParamArray, // 参数数组
  57. ParamMods, // 指定返回参数的ParameterModifier数组
  58. null,
  59. null);
  60. output = ParamArray[2].ToString();
  61. Global.writeLog("Com输出:" + output);
  62. return 0;
  63. }
  64. else
  65. {
  66. output = "COM加载失败!";
  67. Global.writeLog("COM加载失败!");
  68. return -1;
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. output = ex.Message;
  74. Global.writeLog("COM加载失败!" + output);
  75. return -1;
  76. }
  77. }
  78. public int BusinessCom(string funNo, string inputData, ref string outputData, ref string pErrMsg)
  79. {
  80. outputData = "";
  81. pErrMsg = "";
  82. JObject joRtn = new JObject();
  83. try
  84. {
  85. Global.writeLog(funNo + "(" + Global.curEvt.URL + ")", "哈哈", "呵呵哈哈哈呵呵");
  86. int iRes = yh_hb_call(funNo, inputData, out outputData);
  87. return iRes;
  88. }
  89. catch (Exception ex)
  90. {
  91. joRtn.Add("infcode", -1);
  92. joRtn.Add("err_msg", "调用中心服务异常BusinessCom(1):" + ex.Message);
  93. outputData = JsonHelper.toJsonString(joRtn);
  94. return -1;
  95. }
  96. finally
  97. {
  98. Global.writeLog(funNo + "(" + Global.curEvt.URL + ")", JsonHelper.Compress(inputData), JsonHelper.Compress(outputData));
  99. }
  100. }
  101. public int Business(string inputData, ref string outputData, ref string pErrMsg)
  102. {
  103. outputData = "";
  104. pErrMsg = "";
  105. JObject joRtn = new JObject();
  106. try
  107. {
  108. //创建一个HTTP请求
  109. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.inf.centerURL);
  110. //Post请求方式
  111. request.Method = "POST";
  112. //string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  113. //string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  114. //string BusinessID = Global.inf.BusinessID; //服务商ID
  115. //string InsuHosID = Global.inf.hospitalNO; //医疗机构ID
  116. //string CreditID = Global.inf.CreditID; //服务商统一社会信用代码
  117. //内容类型
  118. request.ContentType = "application/json";
  119. //昆明增加头部信息
  120. //string sTemp = timestamp + BusinessID + nonce;
  121. //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)
  122. //string signature = Encrypt.SHA256EncryptStr(sTemp);
  123. //request.Headers.Add("hsf_signature", signature);
  124. //request.Headers.Add("hsf_timestamp", timestamp);
  125. //request.Headers.Add("hsf_nonce", nonce);
  126. //request.Headers.Add("fixmedins_code", InsuHosID);
  127. //request.Headers.Add("infosyscode", CreditID);
  128. //设置参数,并进行URL编码
  129. string paraUrlCoded = inputData;//System.Web.HttpUtility.UrlEncode(jsonParas);
  130. byte[] payload;
  131. //将Json字符串转化为字节
  132. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  133. //设置请求的ContentLength
  134. request.ContentLength = payload.Length;
  135. //发送请求,获得请求流
  136. Stream writer;
  137. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  138. //将请求参数写入流
  139. writer.Write(payload, 0, payload.Length);
  140. writer.Close();//关闭请求流
  141. // String strValue = "";//strValue为http响应所返回的字符流
  142. HttpWebResponse response = null;
  143. try
  144. {
  145. //获得响应流
  146. response = (HttpWebResponse)request.GetResponse();
  147. }
  148. catch (Exception ex)
  149. {
  150. JsonHelper.setExceptionJson(-99, "centerServeiceInvok中获得响应流异常", ex.Message);
  151. return -1;
  152. }
  153. outputData = getResponseData(response);
  154. joRtn = JObject.Parse(outputData);//返回Json数据
  155. if (joRtn.ContainsKey("body"))
  156. {
  157. joRtn = (JObject)joRtn.GetValue("body");
  158. outputData = JsonHelper.toJsonString(joRtn);
  159. }
  160. return 0;
  161. }
  162. catch (Exception ex)
  163. {
  164. joRtn.Add("infcode", -1);
  165. joRtn.Add("err_msg", "调用中心服务异常invokeCenterService(1):" + ex.Message);
  166. outputData = JsonHelper.toJsonString(joRtn);
  167. return -1;
  168. }
  169. finally
  170. {
  171. Global.writeLog(Global.inf.centerURL, inputData, outputData);
  172. }
  173. }
  174. public int BusinessExt(string inputData, ref string outputData, ref string pErrMsg)
  175. {
  176. return this.Business(inputData, ref outputData, ref pErrMsg);
  177. }
  178. public int DownloadFile(string inputData, ref string outputData)
  179. {
  180. outputData = "";
  181. string error = string.Empty; int errorCode = 0;
  182. try
  183. {
  184. JObject jsonInParam = JObject.Parse(inputData);
  185. // 去除外wrapper层便于通用
  186. Utils.removeWrapper(jsonInParam);
  187. string fileName = (string)jsonInParam["input"]["fsDownloadIn"]["filename"];
  188. string filePath = Global.curEvt.path + "\\Download\\" + fileName;
  189. //如果不存在目录,则创建目录
  190. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  191. {
  192. //创建文件夹
  193. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  194. }
  195. if (File.Exists(filePath))
  196. {
  197. File.Delete(filePath);
  198. }
  199. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  200. //创建一个HTTP请求
  201. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.curEvt.URL);
  202. //Post请求方式
  203. request.Method = "POST";
  204. //string timestamp = TimeStamp.get13().ToString(); //当前时间戳(秒)
  205. //string nonce = Guid.NewGuid().ToString(); //非重复的随机字符串(十分钟内不能重复)
  206. //string InsuHosID = Global.inf.hospitalNO; //医疗机构ID
  207. //string CreditID = Global.inf.CreditID; //服务商统一社会信用代码
  208. //string BusinessID = Global.inf.BusinessID; //服务商ID
  209. //内容类型
  210. request.ContentType = "application/json";
  211. //昆明增加头部信息
  212. //string sTemp = timestamp + BusinessID + nonce;
  213. //Sha256 加密生成的签名 signature = sha256(hsf_timestamp + infosyssign + hsf_nonce)
  214. //string signature = Encrypt.SHA256EncryptStr(sTemp);
  215. //request.Headers.Add("hsf_signature", signature);
  216. //request.Headers.Add("hsf_timestamp", timestamp);
  217. //request.Headers.Add("hsf_nonce", nonce);
  218. //request.Headers.Add("fixmedins_code", InsuHosID);
  219. //request.Headers.Add("infosyscode", CreditID);
  220. //设置参数,并进行URL编码
  221. string paraUrlCoded = JsonHelper.toJsonString(jsonInParam);
  222. byte[] payload;
  223. //将Json字符串转化为字节
  224. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  225. //设置请求的ContentLength
  226. request.ContentLength = payload.Length;
  227. Stream writer;
  228. try
  229. {
  230. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  231. }
  232. catch (Exception)
  233. {
  234. writer = null;
  235. errorCode = -100;
  236. error = "连接服务器失败!";
  237. }
  238. //将请求参数写入流
  239. writer.Write(payload, 0, payload.Length);
  240. writer.Close();//关闭请求流
  241. // String strValue = "";//strValue为http响应所返回的字符流
  242. //发送请求并获取相应回应数据
  243. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  244. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  245. Stream responseStream = response.GetResponseStream();
  246. //创建本地文件写入流
  247. byte[] bArr = new byte[1024];
  248. int iTotalSize = 0;
  249. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  250. while (size > 0)
  251. {
  252. iTotalSize += size;
  253. fs.Write(bArr, 0, size);
  254. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  255. }
  256. fs.Close();
  257. responseStream.Close();
  258. dynamic joReturn = new JObject();
  259. joReturn.errorCode = errorCode;
  260. joReturn.errorMessage = error;
  261. joReturn.filePath = filePath;
  262. outputData = joReturn.ToString();
  263. }
  264. catch (Exception ex)
  265. {
  266. errorCode = -100;
  267. error = ex.Message;
  268. dynamic joReturn = new JObject();
  269. joReturn.errorCode = errorCode;
  270. joReturn.errorMessage = error;
  271. outputData = joReturn.ToString();
  272. return -1;
  273. }
  274. finally
  275. {
  276. Global.writeLog("DownloadCenterFile" + "(" + Global.curEvt.URL + ")", inputData, outputData);
  277. }
  278. return 0;
  279. }
  280. public int DownloadPDF(string inputData, string filePath, out string errMsg)
  281. {
  282. errMsg = "";
  283. try
  284. {
  285. JObject jsonInParam = JObject.Parse(inputData);
  286. // 去除外wrapper层便于通用
  287. Utils.removeWrapper(jsonInParam);
  288. //如果不存在目录,则创建目录
  289. if (!Directory.Exists(Global.curEvt.path + "\\Download"))
  290. {
  291. //创建文件夹
  292. DirectoryInfo dirInfo = Directory.CreateDirectory(Global.curEvt.path + "\\Download");
  293. }
  294. if (File.Exists(filePath))
  295. {
  296. File.Delete(filePath);
  297. }
  298. FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  299. //创建一个HTTP请求
  300. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Global.inf.centerURL);
  301. //Post请求方式
  302. request.Method = "POST";
  303. //内容类型
  304. request.ContentType = "application/json";
  305. //设置参数,并进行URL编码
  306. string paraUrlCoded = JsonHelper.toJsonString(jsonInParam);
  307. byte[] payload;
  308. //将Json字符串转化为字节
  309. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  310. //设置请求的ContentLength
  311. request.ContentLength = payload.Length;
  312. Stream writer;
  313. try
  314. {
  315. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  316. }
  317. catch (Exception)
  318. {
  319. writer = null;
  320. errMsg = "连接服务器失败!";
  321. return -1;
  322. }
  323. //将请求参数写入流
  324. writer.Write(payload, 0, payload.Length);
  325. writer.Close();//关闭请求流
  326. // String strValue = "";//strValue为http响应所返回的字符流
  327. //发送请求并获取相应回应数据
  328. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  329. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  330. Stream responseStream = response.GetResponseStream();
  331. //创建本地文件写入流
  332. byte[] bArr = new byte[1024];
  333. int iTotalSize = 0;
  334. int size = responseStream.Read(bArr, 0, (int)bArr.Length);
  335. while (size > 0)
  336. {
  337. iTotalSize += size;
  338. fs.Write(bArr, 0, size);
  339. size = responseStream.Read(bArr, 0, (int)bArr.Length);
  340. }
  341. fs.Close();
  342. responseStream.Close();
  343. errMsg = filePath;
  344. return 0;
  345. }
  346. catch (Exception ex)
  347. {
  348. errMsg = ex.Message;
  349. return -1;
  350. }
  351. finally
  352. {
  353. Global.writeLog("DownloadPDF" + "(" + Global.inf.centerURL + ")", inputData, errMsg);
  354. }
  355. }
  356. public int Init(ref string pErrMsg)
  357. {
  358. return 0;
  359. }
  360. public int UploadFile(string inputData, ref string outputData, ref string pErrMsg)
  361. {
  362. throw new NotImplementedException();
  363. }
  364. private string getResponseData(HttpWebResponse response)
  365. {
  366. string data = "";
  367. if (response != null)
  368. {
  369. Stream s = response.GetResponseStream();
  370. StreamReader sRead = new StreamReader(s);
  371. data = sRead.ReadToEnd();
  372. sRead.Close();
  373. response.Close();
  374. }
  375. return data;
  376. }
  377. }
  378. }