using Newtonsoft.Json.Linq; using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace ThCardReader { class NationalEcBusiness { private static SiBusinessAddress address = null; [DllImport("user32.dll")] private extern static void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); [DllImport("user32.dll")] private extern static int SetCursorPos(int x, int y); #if !DISABLE_NATIONEC [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private extern static IntPtr NationEcTrans(string url, string pindata, ref byte outinfo); #endif [Flags] enum MouseEventFlag : uint { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, XDown = 0x0080, XUp = 0x0100, Wheel = 0x0800, VirtualDesk = 0x4000, Absolute = 0x8000 } public static void SimulateClick() { int count = 0; Process p = Process.GetCurrentProcess(); IntPtr beginHandle = p.MainWindowHandle; while (count < 5 && p.MainWindowHandle == beginHandle) { count += 1; Thread.Sleep(200); } SetCursorPos(Screen.PrimaryScreen.WorkingArea.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2); mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero); } public static JObject ReadQrCode(string biztype) { #if DISABLE_NATIONEC JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "NationECCode.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "NationECCode.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (null == address) { address = MainForm.address; } Thread thread = new Thread(SimulateClick); thread.Start(); string pindata = "{\"data\":{\"businessType\":\"" + biztype + "\",\"deviceType\":\"\",\"orgId\":\"" + address.organizationId + "\"},\"orgId\":\"" + address.organizationId + "\",\"transType\":\"ec.query\"}"; byte[] outinfo = new byte[4096]; IntPtr retPtr = NationEcTrans(address.readQrEcTokenUrl, pindata, ref outinfo[0]); // IntPtr retPtr = NationEcTrans("https://test_dvs.hun.hsip.gov.cn/localcfc/api/hsecfc/localQrCodeQuery", pindata, ref outinfo[0]); var businessResultCode = Marshal.PtrToStringAnsi(retPtr).Trim().Replace("\u0000", ""); string outstr = Encoding.Default.GetString(outinfo).Trim().Replace("\u0000", ""); ReadCardResult tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(outstr); JObject result = new JObject(); if (tmp.code == 0) { result.Add("code", 200); result.Add("message", "读取医保电子凭证成功。"); result.Add("type", "qrcode"); result.Add("data", outstr); result.Add("sign", null); } else { result.Add("code", 1001); result.Add("message", tmp.message); } return result; #endif } public static JObject ReadIdCard(string biztype) { #if DISABLE_NATIONEC JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "NationECCode.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "NationECCode.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (null == address) { address = MainForm.address; } string pindata = "{\"data\":{\"businessType\":\"" + biztype + "\",\"deviceType\":\"\",\"orgId\":\"" + address.organizationId + "\"},\"orgId\":\"" + address.organizationId + "\",\"transType\":\"cn.nhsa.cert.get\"}"; byte[] outinfo = new byte[4096]; IntPtr retPtr = NationEcTrans(address.readQrEcTokenUrl, pindata, ref outinfo[0]); string businessResultCode = Marshal.PtrToStringAnsi(retPtr).Trim().Replace("\u0000", ""); string outstr = Encoding.Default.GetString(outinfo).Trim().Replace("\u0000", ""); ReadCardResult tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(outstr); JObject result = new JObject(); if (tmp.code == 0) { result.Add("code", 200); result.Add("message", "终端读取身份证成功。"); result.Add("type", "face"); result.Add("data", outstr); result.Add("sign", null); } else { result.Add("code", 1001); result.Add("message", tmp.message); } return result; #endif } public static JObject ReadQrCode2(string biztype) { #if DISABLE_NATIONEC JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "NationECCode.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "NationECCode.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (null == address) { address = MainForm.address; } string pindata = "{\"data\":{\"businessType\":\"" + biztype + "\",\"deviceType\":\"\",\"orgId\":\"" + address.organizationId + "\"},\"orgId\":\"" + address.organizationId + "\",\"transType\":\"cn.nhsa.qrcode.get\"}"; byte[] outinfo = new byte[4096]; IntPtr retPtr = NationEcTrans(address.readQrEcTokenUrl, pindata, ref outinfo[0]); string businessResultCode = Marshal.PtrToStringAnsi(retPtr).Trim().Replace("\u0000", ""); string outstr = Encoding.Default.GetString(outinfo).Trim().Replace("\u0000", ""); ReadCardResult tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(outstr); JObject result = new JObject(); if (tmp.code == 0) { result.Add("code", 200); result.Add("message", "终端读取电子凭证成功。"); result.Add("type", "face"); result.Add("data", outstr); result.Add("sign", null); } else { result.Add("code", 1001); result.Add("message", tmp.message); } return result; #endif } public static JObject ReadFace(string biztype) { #if DISABLE_NATIONEC JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "NationECCode.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard"); result.Add("device", "NationECCode.dll(已屏蔽)"); result.Add("suggestion", "请使用华视读卡器测试功能"); return result; #else if (null == address) { address = MainForm.address; } string pindata = "{\"data\":{\"businessType\":\"" + biztype + "\",\"deviceType\":\"\",\"orgId\":\"" + address.organizationId + "\"},\"orgId\":\"" + address.organizationId + "\",\"transType\":\"cn.nhsa.ec.auth\"}"; byte[] outinfo = new byte[4096]; IntPtr retPtr = NationEcTrans(address.readQrEcTokenUrl, pindata, ref outinfo[0]); string businessResultCode = Marshal.PtrToStringAnsi(retPtr).Trim().Replace("\u0000", ""); string outstr = Encoding.Default.GetString(outinfo).Trim().Replace("\u0000", ""); ReadCardResult tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(outstr); JObject result = new JObject(); if (tmp.code == 0) { FaceAuthData authData = Newtonsoft.Json.JsonConvert.DeserializeObject(tmp.data.ToString()); return getUserInfoByAuthNo(biztype, authData.authNo); } else { result.Add("code", 1001); result.Add("message", tmp.message); return result; } #endif } private static JObject getUserInfoByAuthNo(string biztype, string authNo) { #if DISABLE_NATIONEC JObject result = new JObject(); result.Add("code", 1001); result.Add("message", "NationECCode.dll已屏蔽"); return result; #else string pindata = "{\"data\":{\"businessType\":\"" + biztype + "\",\"authNo\":\"" + authNo + "\",\"deviceType\":\"\",\"orgId\":\"" + address.organizationId + "\"},\"orgId\":\"" + address.organizationId + "\",\"transType\":\"cn.nhsa.auth.check\"}"; byte[] outinfo = new byte[4096]; IntPtr retPtr = NationEcTrans(address.readQrEcTokenUrl, pindata, ref outinfo[0]); string businessResultCode = Marshal.PtrToStringAnsi(retPtr).Trim().Replace("\u0000", ""); string outstr = Encoding.Default.GetString(outinfo).Trim().Replace("\u0000", ""); ReadCardResult tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(outstr); JObject result = new JObject(); if (tmp.code == 0) { result.Add("code", 200); result.Add("message", "刷脸获取医保身份成功。"); result.Add("type", "face"); result.Add("data", outstr); result.Add("sign", null); } else { result.Add("code", 1001); result.Add("message", tmp.message); } return result; #endif } } }