using Newtonsoft.Json.Linq; using PTMedicalInsurance.Common.WinAPI; using PTMedicalInsurance.Helper; using PTMedicalInsurance.Variables; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace PTMedicalInsurance.Common { /// /// 银海安全控件 /// class YinHaiSafeCtrl { string progID = "YinHai.CHS.InterfaceSCS"; System.Type comType; object comInstance; /// /// 调用银海安全控件 /// /// /// public int Prepare(string funNo,out string sSafeControlsRtnValue) { string pErrMsg = string.Empty; JObject joInput = JsonHelper.setYinHaiSafe(funNo); sSafeControlsRtnValue = string.Empty; Init(); Call(funNo,joInput.ToString(), out sSafeControlsRtnValue); JObject joRtn = new JObject(); joRtn = JObject.Parse(sSafeControlsRtnValue); int errorCode = int.Parse(joRtn["code"].ToString()); string errorMessage = joRtn["message"].ToString(); if (errorCode != 1) { sSafeControlsRtnValue = errorMessage; return -1; } else { JObject jodata = JObject.FromObject(joRtn["data"]); Global.writeLog("参保地:"+ Global.pat.insuplc_admdvs); Global.writeLog("读卡返回:" + jodata.ToString()); // 如果存在则不使用读卡返回的信息 if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs)) { Global.pat.insuplc_admdvs = jodata["insuplc_admdvs"].ToString(); } Global.pat.mdtrtcertType = jodata["mdtrt_cert_type"].ToString(); Global.pat.mdtrtcertNO = jodata["mdtrt_cert_no"].ToString(); Global.pat.psn_type = jodata["psn_cert_type"].ToString(); Global.pat.certNO = jodata["certno"].ToString(); Global.pat.name = jodata["psn_name"].ToString(); Global.pat.card.SN = jodata["card_sn"].ToString(); Global.pat.card.Cardtoken = jodata["card_token"].ToString(); sSafeControlsRtnValue = jodata.ToString(); } return 0; } /// /// 调用银海安全控件 /// /// /// public int Prepare(string readCardType,string funNo, out string sSafeControlsRtnValue) { int key = 0x67; //电子凭证 if (readCardType == "01") { key = 0x50; } //身份证 if (readCardType == "02") { key = 0x53; } //社保卡 if (readCardType == "03") { key = (int)'R'; } // key = 0x46; //人脸识别 string pErrMsg = string.Empty; JObject joInput = JsonHelper.setYinHaiSafe(funNo); sSafeControlsRtnValue = string.Empty; //FindAndCloseComWindow("身份识别 - 正式版V2.310.23",key); FindAndCloseComWindow("身份识别 - 正式版V", key); Init(); Call(funNo, joInput.ToString(), out sSafeControlsRtnValue); //string sTemp = ""; //try //{ // Task.Run(() => // { // Call(funNo, joInput.ToString(), out sTemp); // Global.writeLog($"Task completed successfully.{sTemp}"); // }); //} //catch (Exception ex) //{ // Global.writeLog($"An error occurred: {ex.Message}"); //} //Global.writeLog("??" + sTemp); //sSafeControlsRtnValue = sTemp; //Thread.Sleep(1000); //AsyncMethod(); JObject joRtn = new JObject(); joRtn = JObject.Parse(sSafeControlsRtnValue); int errorCode = int.Parse(joRtn["code"].ToString()); string errorMessage = joRtn["message"].ToString(); if (errorCode != 1) { sSafeControlsRtnValue = errorMessage; return -1; } else { JObject jodata = JObject.FromObject(joRtn["data"]); //Global.writeLog("参保地:" + Global.pat.insuplc_admdvs); //Global.writeLog("读卡返回:" + jodata.ToString()); // 如果存在则不使用读卡返回的信息 if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs)) { Global.pat.insuplc_admdvs = jodata["insuplc_admdvs"].ToString(); } Global.pat.mdtrtcertType = jodata["mdtrt_cert_type"].ToString(); Global.pat.mdtrtcertNO = jodata["mdtrt_cert_no"].ToString(); Global.pat.psn_type = jodata["psn_cert_type"].ToString(); Global.pat.certNO = jodata["certno"].ToString(); Global.pat.name = jodata["psn_name"].ToString(); Global.pat.card.SN = jodata["card_sn"].ToString(); Global.pat.card.Cardtoken = jodata["card_token"].ToString(); sSafeControlsRtnValue = jodata.ToString(); } return 0; } static async void AsyncMethod(int key) { var result = await FindAndCloseComWindow("身份识别 - 正式版V",key); //var result = await FindAndCloseComWindow("身份识别 - 正式版V2.310.23", key); } static async Task FindAndCloseComWindow(string title,int key) { Task.Run(() => { bool b = false; while (!b) { b = FindAndCloseComWindowA(title,key); } }); return 0; } //static bool FindAndCloseComWindowA(string title,int key) //{ // //WndHelper.wndInfo[] windows = WndHelper.GetAllDesktopWindows(); // //for (int i = 0; i < windows.Length; i++) // //{ // // IntPtr paraentPtr = WndHelper.GetParent(windows[i].hWnd); // // int threadId = WndHelper.GetWindowThreadProcessId(windows[i].hWnd, out int processId); // // string msg = $"序号:{i},hwnd:{windows[i].hWnd},threadID:{threadId},processId:{processId},父窗口句柄:{paraentPtr},szWindowName:{windows[i].szWindowName}"; // // Global.writeLog(msg); // //} // //string title = "身份识别 - 正式版V2.310.23"; // IntPtr myPtr = WndHelper.FindWindow(null, title); // if (WndHelper.IsWindowVisible(myPtr)) // { // int len = WndHelper.GetWindowTextLength(myPtr); // StringBuilder sb = new StringBuilder(); // WndHelper.GetWindowText(myPtr, sb, len + 1); // //Global.writeLog($@"找到""{title}"":{myPtr}"); // WndHelper.SendMsg(WndHelper.msgType.keybd_event, myPtr, key); //不能触发,但能触发F // return true; // } // else // { // //Global.writeLog($@"未找到""{title}"""); // return false; // } //} // 模糊查找窗口并发送按键 public static bool FindAndCloseComWindowA(string pattern, int key) { IntPtr targetHwnd = IntPtr.Zero; WndHelper.EnumWindows((hWnd, lParam) => { if (WndHelper.IsWindowVisible(hWnd)) { int length = WndHelper.GetWindowTextLength(hWnd); if (length == 0) return true; StringBuilder sb = new StringBuilder(length + 1); WndHelper.GetWindowText(hWnd, sb, sb.Capacity); string title = sb.ToString(); // 使用 Contains 匹配 if (!string.IsNullOrEmpty(title) && title.Contains(pattern)) { targetHwnd = hWnd; return false; // 停止遍历 } // 或者使用正则表达式匹配(更灵活) // if (Regex.IsMatch(title, pattern)) // { // targetHwnd = hWnd; // return false; // } } return true; // 继续遍历 }, IntPtr.Zero.ToInt32()); if (targetHwnd != IntPtr.Zero) { WndHelper.SendMsg(WndHelper.msgType.keybd_event, targetHwnd, key); //不能触发,但能触发F return true; } return false; } /// /// 调用银海控件进行打印 /// /// /// public void YinHaiPrint(string input, out string output) { string errMsg = string.Empty; Init(); Print(input, out output); } private int Init() { comType = System.Type.GetTypeFromProgID(progID); // 创建Com的实例 if (comType != null) { Global.writeLog("开始COM组件Init"); //创建实例 comInstance = Activator.CreateInstance(comType); if (comInstance != null) { Global.writeLog("Init实例创建成功"); } //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = 0; ParamArray[1] = ""; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][0] = true; ParamMods[0][1] = true; // 设置第二个参数为返回参数,调用含有ParameterModifier数组的重载函数 comType.InvokeMember("yh_CHS_init", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, comInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); string Msg = "加载成功:" + ParamArray[1].ToString(); Global.writeLog(Msg + "___" + ParamArray[0].ToString()); return (int)ParamArray[0]; } else { string Msg = "YinHaiComType加载失败!"; Global.writeLog(Msg); return 1; } } private int Call(string infno, string inputData, out string outputData) { try { if (comType != null) { //创建实例,不能再次创建,否则会提示没有初始化 if (comInstance != null) { Global.writeLog("实例创建成功,准备调用Call服务"); } else { outputData = "实例不存在!"; Global.writeLog("实例不存在"); return -1; } //设置需要设置的参数值 object[] ParamArray = new object[3]; ParamArray[0] = infno; ParamArray[1] = inputData; ParamArray[2] = ""; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数 ParamMods[0][2] = true; comType.InvokeMember("yh_CHS_call", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, comInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); outputData = ParamArray[2].ToString(); Global.writeLog(infno, inputData,"Com输出:" + outputData); return 0; } else { outputData = "COM加载失败!"; Global.writeLog("COM加载失败!"); } } catch (Exception ex) { outputData = ex.Message; Global.writeLog("COM加载失败!" + outputData); } return -1; } /// /// 打印结算清单 /// /// /// private void Print(string input, out string output) { if (comType != null) { //创建实例,不能再次创建,否则会提示没有初始化 if (comInstance != null) { Global.writeLog("实例创建成功,准备调用Call服务"); } else { output = "实例不存在!"; Global.writeLog("实例不存在"); return; } //设置需要设置的参数值 object[] ParamArray = new object[2]; ParamArray[0] = input; ParamArray[1] = ""; ParameterModifier[] ParamMods = new ParameterModifier[1]; ParamMods[0] = new ParameterModifier(2); // 初始化为接口参数的个数 ParamMods[0][1] = true; comType.InvokeMember("yh_CHS_print", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, comInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); output = ParamArray[1].ToString(); } else { output = "COM加载失败!"; Global.writeLog("COM加载失败!"); } } public void Destroy(out string output) { // 创建Com的实例 if (comType != null) { //创建实例 comInstance = Activator.CreateInstance(comType); if (comInstance != null) { Global.writeLog("实例创建成功,准备调用Call服务"); } else { output = "实例不存在!"; Global.writeLog("实例不存在"); return; } //设置需要设置的参数值 object[] ParamArray = new object[0]; ParameterModifier[] ParamMods = new ParameterModifier[0]; ParamMods[0] = new ParameterModifier(0); // 初始化为接口参数的个数 comType.InvokeMember("yh_CHS_destroy", // 接口函数名 BindingFlags.Default | BindingFlags.InvokeMethod, null, comInstance, // 调用的COM组件 ParamArray, // 参数数组 ParamMods, // 指定返回参数的ParameterModifier数组 null, null); output = "destroy成功!"; } else { output = "COM加载失败!"; Global.writeLog("COM加载失败!"); } } } }