YinHaiSafeCtrl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Common.WinAPI;
  3. using PTMedicalInsurance.Helper;
  4. using PTMedicalInsurance.Variables;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace PTMedicalInsurance.Common
  14. {
  15. /// <summary>
  16. /// 银海安全控件
  17. /// </summary>
  18. class YinHaiSafeCtrl
  19. {
  20. string progID = "YinHai.CHS.InterfaceSCS";
  21. System.Type comType;
  22. object comInstance;
  23. /// <summary>
  24. /// 调用银海安全控件
  25. /// </summary>
  26. /// <param name="funNo"></param>
  27. /// <returns></returns>
  28. public int Prepare(string funNo,out string sSafeControlsRtnValue)
  29. {
  30. string pErrMsg = string.Empty;
  31. JObject joInput = JsonHelper.setYinHaiSafe(funNo);
  32. sSafeControlsRtnValue = string.Empty;
  33. Init();
  34. Call(funNo,joInput.ToString(), out sSafeControlsRtnValue);
  35. JObject joRtn = new JObject();
  36. joRtn = JObject.Parse(sSafeControlsRtnValue);
  37. int errorCode = int.Parse(joRtn["code"].ToString());
  38. string errorMessage = joRtn["message"].ToString();
  39. if (errorCode != 1)
  40. {
  41. sSafeControlsRtnValue = errorMessage;
  42. return -1;
  43. }
  44. else
  45. {
  46. JObject jodata = JObject.FromObject(joRtn["data"]);
  47. Global.writeLog("参保地:"+ Global.pat.insuplc_admdvs);
  48. Global.writeLog("读卡返回:" + jodata.ToString());
  49. // 如果存在则不使用读卡返回的信息
  50. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  51. {
  52. Global.pat.insuplc_admdvs = jodata["insuplc_admdvs"].ToString();
  53. }
  54. Global.pat.mdtrtcertType = jodata["mdtrt_cert_type"].ToString();
  55. Global.pat.mdtrtcertNO = jodata["mdtrt_cert_no"].ToString();
  56. Global.pat.psn_type = jodata["psn_cert_type"].ToString();
  57. Global.pat.certNO = jodata["certno"].ToString();
  58. Global.pat.name = jodata["psn_name"].ToString();
  59. Global.pat.card.SN = jodata["card_sn"].ToString();
  60. Global.pat.card.Cardtoken = jodata["card_token"].ToString();
  61. sSafeControlsRtnValue = jodata.ToString();
  62. }
  63. return 0;
  64. }
  65. /// <summary>
  66. /// 调用银海安全控件
  67. /// </summary>
  68. /// <param name="funNo"></param>
  69. /// <returns></returns>
  70. public int Prepare(string readCardType,string funNo, out string sSafeControlsRtnValue)
  71. {
  72. int key = 0x67;
  73. //电子凭证
  74. if (readCardType == "01")
  75. {
  76. key = 0x50;
  77. }
  78. //身份证
  79. if (readCardType == "02")
  80. {
  81. key = 0x53;
  82. }
  83. //社保卡
  84. if (readCardType == "03")
  85. {
  86. key = (int)'R';
  87. }
  88. // key = 0x46; //人脸识别
  89. string pErrMsg = string.Empty;
  90. JObject joInput = JsonHelper.setYinHaiSafe(funNo);
  91. sSafeControlsRtnValue = string.Empty;
  92. FindAndCloseComWindow("身份识别 - 正式版V2.310.23",key);
  93. Init();
  94. Call(funNo, joInput.ToString(), out sSafeControlsRtnValue);
  95. //string sTemp = "";
  96. //try
  97. //{
  98. // Task.Run(() =>
  99. // {
  100. // Call(funNo, joInput.ToString(), out sTemp);
  101. // Global.writeLog($"Task completed successfully.{sTemp}");
  102. // });
  103. //}
  104. //catch (Exception ex)
  105. //{
  106. // Global.writeLog($"An error occurred: {ex.Message}");
  107. //}
  108. //Global.writeLog("??" + sTemp);
  109. //sSafeControlsRtnValue = sTemp;
  110. //Thread.Sleep(1000);
  111. //AsyncMethod();
  112. JObject joRtn = new JObject();
  113. joRtn = JObject.Parse(sSafeControlsRtnValue);
  114. int errorCode = int.Parse(joRtn["code"].ToString());
  115. string errorMessage = joRtn["message"].ToString();
  116. if (errorCode != 1)
  117. {
  118. sSafeControlsRtnValue = errorMessage;
  119. return -1;
  120. }
  121. else
  122. {
  123. JObject jodata = JObject.FromObject(joRtn["data"]);
  124. //Global.writeLog("参保地:" + Global.pat.insuplc_admdvs);
  125. //Global.writeLog("读卡返回:" + jodata.ToString());
  126. // 如果存在则不使用读卡返回的信息
  127. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  128. {
  129. Global.pat.insuplc_admdvs = jodata["insuplc_admdvs"].ToString();
  130. }
  131. Global.pat.mdtrtcertType = jodata["mdtrt_cert_type"].ToString();
  132. Global.pat.mdtrtcertNO = jodata["mdtrt_cert_no"].ToString();
  133. Global.pat.psn_type = jodata["psn_cert_type"].ToString();
  134. Global.pat.certNO = jodata["certno"].ToString();
  135. Global.pat.name = jodata["psn_name"].ToString();
  136. Global.pat.card.SN = jodata["card_sn"].ToString();
  137. Global.pat.card.Cardtoken = jodata["card_token"].ToString();
  138. sSafeControlsRtnValue = jodata.ToString();
  139. }
  140. return 0;
  141. }
  142. static async void AsyncMethod(int key)
  143. {
  144. var result = await FindAndCloseComWindow("身份识别 - 正式版V2.310.23",key);
  145. }
  146. static async Task<int> FindAndCloseComWindow(string title,int key)
  147. {
  148. Task.Run(() =>
  149. {
  150. bool b = false;
  151. while (!b)
  152. {
  153. b = FindAndCloseComWindowA(title,key);
  154. }
  155. });
  156. return 0;
  157. }
  158. static bool FindAndCloseComWindowA(string title,int key)
  159. {
  160. //WndHelper.wndInfo[] windows = WndHelper.GetAllDesktopWindows();
  161. //for (int i = 0; i < windows.Length; i++)
  162. //{
  163. // IntPtr paraentPtr = WndHelper.GetParent(windows[i].hWnd);
  164. // int threadId = WndHelper.GetWindowThreadProcessId(windows[i].hWnd, out int processId);
  165. // string msg = $"序号:{i},hwnd:{windows[i].hWnd},threadID:{threadId},processId:{processId},父窗口句柄:{paraentPtr},szWindowName:{windows[i].szWindowName}";
  166. // Global.writeLog(msg);
  167. //}
  168. //string title = "身份识别 - 正式版V2.310.23";
  169. IntPtr myPtr = WndHelper.FindWindow(null, title);
  170. if (WndHelper.IsWindowVisible(myPtr))
  171. {
  172. int len = WndHelper.GetWindowTextLength(myPtr);
  173. StringBuilder sb = new StringBuilder();
  174. WndHelper.GetWindowText(myPtr, sb, len + 1);
  175. //Global.writeLog($@"找到""{title}"":{myPtr}");
  176. WndHelper.SendMsg(WndHelper.msgType.keybd_event, myPtr, key); //不能触发,但能触发F
  177. return true;
  178. }
  179. else
  180. {
  181. //Global.writeLog($@"未找到""{title}""");
  182. return false;
  183. }
  184. }
  185. /// <summary>
  186. /// 调用银海控件进行打印
  187. /// </summary>
  188. /// <param name="input"></param>
  189. /// <param name="output"></param>
  190. public void YinHaiPrint(string input, out string output)
  191. {
  192. string errMsg = string.Empty;
  193. Init();
  194. Print(input, out output);
  195. }
  196. private int Init()
  197. {
  198. comType = System.Type.GetTypeFromProgID(progID);
  199. // 创建Com的实例
  200. if (comType != null)
  201. {
  202. Global.writeLog("开始COM组件Init");
  203. //创建实例
  204. comInstance = Activator.CreateInstance(comType);
  205. if (comInstance != null)
  206. {
  207. Global.writeLog("Init实例创建成功");
  208. }
  209. //设置需要设置的参数值
  210. object[] ParamArray = new object[2];
  211. ParamArray[0] = 0;
  212. ParamArray[1] = "";
  213. ParameterModifier[] ParamMods = new ParameterModifier[1];
  214. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  215. ParamMods[0][0] = true;
  216. ParamMods[0][1] = true; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数
  217. comType.InvokeMember("yh_CHS_init", // 接口函数名
  218. BindingFlags.Default | BindingFlags.InvokeMethod,
  219. null,
  220. comInstance, // 调用的COM组件
  221. ParamArray, // 参数数组
  222. ParamMods, // 指定返回参数的ParameterModifier数组
  223. null,
  224. null);
  225. string Msg = "加载成功:" + ParamArray[1].ToString();
  226. Global.writeLog(Msg + "___" + ParamArray[0].ToString());
  227. return (int)ParamArray[0];
  228. }
  229. else
  230. {
  231. string Msg = "YinHaiComType加载失败!";
  232. Global.writeLog(Msg);
  233. return 1;
  234. }
  235. }
  236. private int Call(string infno, string inputData, out string outputData)
  237. {
  238. try
  239. {
  240. if (comType != null)
  241. {
  242. //创建实例,不能再次创建,否则会提示没有初始化
  243. if (comInstance != null)
  244. {
  245. Global.writeLog("实例创建成功,准备调用Call服务");
  246. }
  247. else
  248. {
  249. outputData = "实例不存在!";
  250. Global.writeLog("实例不存在");
  251. return -1;
  252. }
  253. //设置需要设置的参数值
  254. object[] ParamArray = new object[3];
  255. ParamArray[0] = infno;
  256. ParamArray[1] = inputData;
  257. ParamArray[2] = "";
  258. ParameterModifier[] ParamMods = new ParameterModifier[1];
  259. ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  260. ParamMods[0][2] = true;
  261. comType.InvokeMember("yh_CHS_call", // 接口函数名
  262. BindingFlags.Default | BindingFlags.InvokeMethod,
  263. null,
  264. comInstance, // 调用的COM组件
  265. ParamArray, // 参数数组
  266. ParamMods, // 指定返回参数的ParameterModifier数组
  267. null,
  268. null);
  269. outputData = ParamArray[2].ToString();
  270. Global.writeLog(infno, inputData,"Com输出:" + outputData);
  271. return 0;
  272. }
  273. else
  274. {
  275. outputData = "COM加载失败!";
  276. Global.writeLog("COM加载失败!");
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. outputData = ex.Message;
  282. Global.writeLog("COM加载失败!" + outputData);
  283. }
  284. return -1;
  285. }
  286. /// <summary>
  287. /// 打印结算清单
  288. /// </summary>
  289. /// <param name="input"></param>
  290. /// <param name="output"></param>
  291. private void Print(string input, out string output)
  292. {
  293. if (comType != null)
  294. {
  295. //创建实例,不能再次创建,否则会提示没有初始化
  296. if (comInstance != null)
  297. {
  298. Global.writeLog("实例创建成功,准备调用Call服务");
  299. }
  300. else
  301. {
  302. output = "实例不存在!";
  303. Global.writeLog("实例不存在");
  304. return;
  305. }
  306. //设置需要设置的参数值
  307. object[] ParamArray = new object[2];
  308. ParamArray[0] = input;
  309. ParamArray[1] = "";
  310. ParameterModifier[] ParamMods = new ParameterModifier[1];
  311. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  312. ParamMods[0][1] = true;
  313. comType.InvokeMember("yh_CHS_print", // 接口函数名
  314. BindingFlags.Default | BindingFlags.InvokeMethod,
  315. null,
  316. comInstance, // 调用的COM组件
  317. ParamArray, // 参数数组
  318. ParamMods, // 指定返回参数的ParameterModifier数组
  319. null,
  320. null);
  321. output = ParamArray[1].ToString();
  322. }
  323. else
  324. {
  325. output = "COM加载失败!";
  326. Global.writeLog("COM加载失败!");
  327. }
  328. }
  329. public void Destroy(out string output)
  330. {
  331. // 创建Com的实例
  332. if (comType != null)
  333. {
  334. //创建实例
  335. comInstance = Activator.CreateInstance(comType);
  336. if (comInstance != null)
  337. {
  338. Global.writeLog("实例创建成功,准备调用Call服务");
  339. }
  340. else
  341. {
  342. output = "实例不存在!";
  343. Global.writeLog("实例不存在");
  344. return;
  345. }
  346. //设置需要设置的参数值
  347. object[] ParamArray = new object[0];
  348. ParameterModifier[] ParamMods = new ParameterModifier[0];
  349. ParamMods[0] = new ParameterModifier(0); // 初始化为接口参数的个数
  350. comType.InvokeMember("yh_CHS_destroy", // 接口函数名
  351. BindingFlags.Default | BindingFlags.InvokeMethod,
  352. null,
  353. comInstance, // 调用的COM组件
  354. ParamArray, // 参数数组
  355. ParamMods, // 指定返回参数的ParameterModifier数组
  356. null,
  357. null);
  358. output = "destroy成功!";
  359. }
  360. else
  361. {
  362. output = "COM加载失败!";
  363. Global.writeLog("COM加载失败!");
  364. }
  365. }
  366. }
  367. }