123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- 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
- {
- /// <summary>
- /// 银海安全控件
- /// </summary>
- class YinHaiSafeCtrl
- {
- string progID = "YinHai.CHS.InterfaceSCS";
- System.Type comType;
- object comInstance;
- /// <summary>
- /// 调用银海安全控件
- /// </summary>
- /// <param name="funNo"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 调用银海安全控件
- /// </summary>
- /// <param name="funNo"></param>
- /// <returns></returns>
- 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<int> 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;
- }
- /// <summary>
- /// 调用银海控件进行打印
- /// </summary>
- /// <param name="input"></param>
- /// <param name="output"></param>
- 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;
- }
-
- /// <summary>
- /// 打印结算清单
- /// </summary>
- /// <param name="input"></param>
- /// <param name="output"></param>
- 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加载失败!");
- }
- }
- }
- }
|