YinHaiSafeCtrl.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. FindAndCloseComWindow("身份识别 - 正式版V", key);
  94. Init();
  95. Call(funNo, joInput.ToString(), out sSafeControlsRtnValue);
  96. //string sTemp = "";
  97. //try
  98. //{
  99. // Task.Run(() =>
  100. // {
  101. // Call(funNo, joInput.ToString(), out sTemp);
  102. // Global.writeLog($"Task completed successfully.{sTemp}");
  103. // });
  104. //}
  105. //catch (Exception ex)
  106. //{
  107. // Global.writeLog($"An error occurred: {ex.Message}");
  108. //}
  109. //Global.writeLog("??" + sTemp);
  110. //sSafeControlsRtnValue = sTemp;
  111. //Thread.Sleep(1000);
  112. //AsyncMethod();
  113. JObject joRtn = new JObject();
  114. joRtn = JObject.Parse(sSafeControlsRtnValue);
  115. int errorCode = int.Parse(joRtn["code"].ToString());
  116. string errorMessage = joRtn["message"].ToString();
  117. if (errorCode != 1)
  118. {
  119. sSafeControlsRtnValue = errorMessage;
  120. return -1;
  121. }
  122. else
  123. {
  124. JObject jodata = JObject.FromObject(joRtn["data"]);
  125. //Global.writeLog("参保地:" + Global.pat.insuplc_admdvs);
  126. //Global.writeLog("读卡返回:" + jodata.ToString());
  127. // 如果存在则不使用读卡返回的信息
  128. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  129. {
  130. Global.pat.insuplc_admdvs = jodata["insuplc_admdvs"].ToString();
  131. }
  132. Global.pat.mdtrtcertType = jodata["mdtrt_cert_type"].ToString();
  133. Global.pat.mdtrtcertNO = jodata["mdtrt_cert_no"].ToString();
  134. Global.pat.psn_type = jodata["psn_cert_type"].ToString();
  135. Global.pat.certNO = jodata["certno"].ToString();
  136. Global.pat.name = jodata["psn_name"].ToString();
  137. Global.pat.card.SN = jodata["card_sn"].ToString();
  138. Global.pat.card.Cardtoken = jodata["card_token"].ToString();
  139. sSafeControlsRtnValue = jodata.ToString();
  140. }
  141. return 0;
  142. }
  143. static async void AsyncMethod(int key)
  144. {
  145. var result = await FindAndCloseComWindow("身份识别 - 正式版V",key);
  146. //var result = await FindAndCloseComWindow("身份识别 - 正式版V2.310.23", key);
  147. }
  148. static async Task<int> FindAndCloseComWindow(string title,int key)
  149. {
  150. Task.Run(() =>
  151. {
  152. bool b = false;
  153. while (!b)
  154. {
  155. b = FindAndCloseComWindowA(title,key);
  156. }
  157. });
  158. return 0;
  159. }
  160. //static bool FindAndCloseComWindowA(string title,int key)
  161. //{
  162. // //WndHelper.wndInfo[] windows = WndHelper.GetAllDesktopWindows();
  163. // //for (int i = 0; i < windows.Length; i++)
  164. // //{
  165. // // IntPtr paraentPtr = WndHelper.GetParent(windows[i].hWnd);
  166. // // int threadId = WndHelper.GetWindowThreadProcessId(windows[i].hWnd, out int processId);
  167. // // string msg = $"序号:{i},hwnd:{windows[i].hWnd},threadID:{threadId},processId:{processId},父窗口句柄:{paraentPtr},szWindowName:{windows[i].szWindowName}";
  168. // // Global.writeLog(msg);
  169. // //}
  170. // //string title = "身份识别 - 正式版V2.310.23";
  171. // IntPtr myPtr = WndHelper.FindWindow(null, title);
  172. // if (WndHelper.IsWindowVisible(myPtr))
  173. // {
  174. // int len = WndHelper.GetWindowTextLength(myPtr);
  175. // StringBuilder sb = new StringBuilder();
  176. // WndHelper.GetWindowText(myPtr, sb, len + 1);
  177. // //Global.writeLog($@"找到""{title}"":{myPtr}");
  178. // WndHelper.SendMsg(WndHelper.msgType.keybd_event, myPtr, key); //不能触发,但能触发F
  179. // return true;
  180. // }
  181. // else
  182. // {
  183. // //Global.writeLog($@"未找到""{title}""");
  184. // return false;
  185. // }
  186. //}
  187. // 模糊查找窗口并发送按键
  188. public static bool FindAndCloseComWindowA(string pattern, int key)
  189. {
  190. IntPtr targetHwnd = IntPtr.Zero;
  191. WndHelper.EnumWindows((hWnd, lParam) =>
  192. {
  193. if (WndHelper.IsWindowVisible(hWnd))
  194. {
  195. int length = WndHelper.GetWindowTextLength(hWnd);
  196. if (length == 0) return true;
  197. StringBuilder sb = new StringBuilder(length + 1);
  198. WndHelper.GetWindowText(hWnd, sb, sb.Capacity);
  199. string title = sb.ToString();
  200. // 使用 Contains 匹配
  201. if (!string.IsNullOrEmpty(title) && title.Contains(pattern))
  202. {
  203. targetHwnd = hWnd;
  204. return false; // 停止遍历
  205. }
  206. // 或者使用正则表达式匹配(更灵活)
  207. // if (Regex.IsMatch(title, pattern))
  208. // {
  209. // targetHwnd = hWnd;
  210. // return false;
  211. // }
  212. }
  213. return true; // 继续遍历
  214. }, IntPtr.Zero.ToInt32());
  215. if (targetHwnd != IntPtr.Zero)
  216. {
  217. WndHelper.SendMsg(WndHelper.msgType.keybd_event, targetHwnd, key); //不能触发,但能触发F
  218. return true;
  219. }
  220. return false;
  221. }
  222. /// <summary>
  223. /// 调用银海控件进行打印
  224. /// </summary>
  225. /// <param name="input"></param>
  226. /// <param name="output"></param>
  227. public void YinHaiPrint(string input, out string output)
  228. {
  229. string errMsg = string.Empty;
  230. Init();
  231. Print(input, out output);
  232. }
  233. private int Init()
  234. {
  235. comType = System.Type.GetTypeFromProgID(progID);
  236. // 创建Com的实例
  237. if (comType != null)
  238. {
  239. Global.writeLog("开始COM组件Init");
  240. //创建实例
  241. comInstance = Activator.CreateInstance(comType);
  242. if (comInstance != null)
  243. {
  244. Global.writeLog("Init实例创建成功");
  245. }
  246. //设置需要设置的参数值
  247. object[] ParamArray = new object[2];
  248. ParamArray[0] = 0;
  249. ParamArray[1] = "";
  250. ParameterModifier[] ParamMods = new ParameterModifier[1];
  251. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  252. ParamMods[0][0] = true;
  253. ParamMods[0][1] = true; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数
  254. comType.InvokeMember("yh_CHS_init", // 接口函数名
  255. BindingFlags.Default | BindingFlags.InvokeMethod,
  256. null,
  257. comInstance, // 调用的COM组件
  258. ParamArray, // 参数数组
  259. ParamMods, // 指定返回参数的ParameterModifier数组
  260. null,
  261. null);
  262. string Msg = "加载成功:" + ParamArray[1].ToString();
  263. Global.writeLog(Msg + "___" + ParamArray[0].ToString());
  264. return (int)ParamArray[0];
  265. }
  266. else
  267. {
  268. string Msg = "YinHaiComType加载失败!";
  269. Global.writeLog(Msg);
  270. return 1;
  271. }
  272. }
  273. private int Call(string infno, string inputData, out string outputData)
  274. {
  275. try
  276. {
  277. if (comType != null)
  278. {
  279. //创建实例,不能再次创建,否则会提示没有初始化
  280. if (comInstance != null)
  281. {
  282. Global.writeLog("实例创建成功,准备调用Call服务");
  283. }
  284. else
  285. {
  286. outputData = "实例不存在!";
  287. Global.writeLog("实例不存在");
  288. return -1;
  289. }
  290. //设置需要设置的参数值
  291. object[] ParamArray = new object[3];
  292. ParamArray[0] = infno;
  293. ParamArray[1] = inputData;
  294. ParamArray[2] = "";
  295. ParameterModifier[] ParamMods = new ParameterModifier[1];
  296. ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
  297. ParamMods[0][2] = true;
  298. comType.InvokeMember("yh_CHS_call", // 接口函数名
  299. BindingFlags.Default | BindingFlags.InvokeMethod,
  300. null,
  301. comInstance, // 调用的COM组件
  302. ParamArray, // 参数数组
  303. ParamMods, // 指定返回参数的ParameterModifier数组
  304. null,
  305. null);
  306. outputData = ParamArray[2].ToString();
  307. Global.writeLog(infno, inputData,"Com输出:" + outputData);
  308. return 0;
  309. }
  310. else
  311. {
  312. outputData = "COM加载失败!";
  313. Global.writeLog("COM加载失败!");
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. outputData = ex.Message;
  319. Global.writeLog("COM加载失败!" + outputData);
  320. }
  321. return -1;
  322. }
  323. /// <summary>
  324. /// 打印结算清单
  325. /// </summary>
  326. /// <param name="input"></param>
  327. /// <param name="output"></param>
  328. private void Print(string input, out string output)
  329. {
  330. if (comType != null)
  331. {
  332. //创建实例,不能再次创建,否则会提示没有初始化
  333. if (comInstance != null)
  334. {
  335. Global.writeLog("实例创建成功,准备调用Call服务");
  336. }
  337. else
  338. {
  339. output = "实例不存在!";
  340. Global.writeLog("实例不存在");
  341. return;
  342. }
  343. //设置需要设置的参数值
  344. object[] ParamArray = new object[2];
  345. ParamArray[0] = input;
  346. ParamArray[1] = "";
  347. ParameterModifier[] ParamMods = new ParameterModifier[1];
  348. ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数
  349. ParamMods[0][1] = true;
  350. comType.InvokeMember("yh_CHS_print", // 接口函数名
  351. BindingFlags.Default | BindingFlags.InvokeMethod,
  352. null,
  353. comInstance, // 调用的COM组件
  354. ParamArray, // 参数数组
  355. ParamMods, // 指定返回参数的ParameterModifier数组
  356. null,
  357. null);
  358. output = ParamArray[1].ToString();
  359. }
  360. else
  361. {
  362. output = "COM加载失败!";
  363. Global.writeLog("COM加载失败!");
  364. }
  365. }
  366. public void Destroy(out string output)
  367. {
  368. // 创建Com的实例
  369. if (comType != null)
  370. {
  371. //创建实例
  372. comInstance = Activator.CreateInstance(comType);
  373. if (comInstance != null)
  374. {
  375. Global.writeLog("实例创建成功,准备调用Call服务");
  376. }
  377. else
  378. {
  379. output = "实例不存在!";
  380. Global.writeLog("实例不存在");
  381. return;
  382. }
  383. //设置需要设置的参数值
  384. object[] ParamArray = new object[0];
  385. ParameterModifier[] ParamMods = new ParameterModifier[0];
  386. ParamMods[0] = new ParameterModifier(0); // 初始化为接口参数的个数
  387. comType.InvokeMember("yh_CHS_destroy", // 接口函数名
  388. BindingFlags.Default | BindingFlags.InvokeMethod,
  389. null,
  390. comInstance, // 调用的COM组件
  391. ParamArray, // 参数数组
  392. ParamMods, // 指定返回参数的ParameterModifier数组
  393. null,
  394. null);
  395. output = "destroy成功!";
  396. }
  397. else
  398. {
  399. output = "COM加载失败!";
  400. Global.writeLog("COM加载失败!");
  401. }
  402. }
  403. }
  404. }