| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844 |
-
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using PTMedicalInsurance.Helper;
- using PTMedicalInsurance.Variables;
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace PTMedicalInsurance.CardReaders
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class CardReader:MarshalByRefObject
- {
- const string DllPath = @"SSCard.dll";
- private bool initilaized = false;
- [DllImport("SSCard.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int Init(IntPtr pURL, IntPtr pUser);
- [DllImport("SSCard.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- private static extern int ReadCardBas(IntPtr pOutBuff, int nOutBuffLen, IntPtr pSignBuff, int nSignBuffLen);
- [DllImport("SSCard.dll", EntryPoint = "VerifyPIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int VerifyPIN(StringBuilder pOutBuff, int nOutBuffLen);
- [DllImport("SSCard.dll", EntryPoint = "ChangePIN", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ChangePIN(StringBuilder pOutBuff, int nOutBuffLen);
- [DllImport("SSCard.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ReadSFZ(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);
- [DllImport("SSCard.dll", EntryPoint = "GetQRBase", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int GetQRBase(int nTimeout, StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);
-
- [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);
- private static readonly Lazy<CardReader> _lazyInstance = new Lazy<CardReader>(() => new CardReader());
- private string URL;
- private string User;
- private static CardReader _instance;
- private static readonly object _lock = new object();
- public static int count =0;
- public static int countA = 0;
- public static int countinit = 0;
- private CardReader() { }
- public static CardReader Instance => _lazyInstance.Value;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Initialize(string url, string user)
- {
- lock (_lock)
- {
- if (string.IsNullOrEmpty(URL) && string.IsNullOrEmpty(User))
- {
- URL = url;
- User = user;
- }
- else
- {
-
- }
- }
- }
- private CardReader(string URL, string User)
- {
- this.URL = URL;
- this.User = User;
- }
- public int Init()
- {
- if (initilaized)
- {
- Global.writeLog_debug("检测到已经初始化,本次不再初始化!");
- return 0;
- }
- countinit++;
- Global.writeLog_debug($"开始初始化:{countinit}");
-
- string errMsg = "";
- int result = -1;
-
-
- byte[] urlBytes = Encoding.ASCII.GetBytes(URL + '\0');
- byte[] userBytes = Encoding.ASCII.GetBytes(User + '\0');
-
-
-
-
- IntPtr pUrl = Marshal.AllocHGlobal(urlBytes.Length);
- IntPtr pUser = Marshal.AllocHGlobal(userBytes.Length);
- try
- {
-
- Marshal.Copy(urlBytes, 0, pUrl, urlBytes.Length);
- Marshal.Copy(userBytes, 0, pUser, userBytes.Length);
- result = Init(pUrl, pUser);
- initilaized = true;
- return result;
- }
- catch (Exception ex)
- {
- errMsg = ex.Message;
- return result;
- }
- finally
- {
- Global.writeLog_debug("初始化", $"countinit:{countinit}", result.ToString() + ":" + errMsg);
-
-
-
-
- if (pUrl != IntPtr.Zero) Marshal.FreeHGlobal(pUrl);
- if (pUser != IntPtr.Zero) Marshal.FreeHGlobal(pUser);
- }
- }
-
-
-
-
-
- public int ReadCardBas(out string basInfo)
- {
- basInfo = "";
- int rtn = -1;
-
-
-
-
- IntPtr pOutBuff = Marshal.AllocHGlobal(1024);
- IntPtr pSignBuff = Marshal.AllocHGlobal(8192);
- try
- {
- rtn = ReadCardBas(pOutBuff, 1024, pSignBuff, 8192);
- if (rtn == 0)
- {
-
- string outString = Marshal.PtrToStringAnsi(pOutBuff);
- string signString = Marshal.PtrToStringAnsi(pSignBuff);
- basInfo = outString;
- Global.pat.card.BusinessCode = signString;
- }
- else
- {
- string outString = Marshal.PtrToStringAnsi(pOutBuff);
- basInfo = outString;
- }
- return rtn;
- }
- catch (Exception ex)
- {
- basInfo = "ReadCardBas异常:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("读社保卡", "", basInfo);
-
-
-
-
- Marshal.FreeHGlobal(pOutBuff);
- Marshal.FreeHGlobal(pSignBuff);
- }
- }
- public int ReadCardBas(out string basInfo, out string regInfo)
- {
- basInfo = "";regInfo = "";
- int rtn = -1;
-
-
-
-
- IntPtr pOutBuff = Marshal.AllocHGlobal(1024);
- IntPtr pSignBuff = Marshal.AllocHGlobal(8192);
- try
- {
- rtn = ReadCardBas(pOutBuff, 1024, pSignBuff, 8192);
- if (rtn == 0)
- {
-
- string outString = Marshal.PtrToStringAnsi(pOutBuff);
- string signString = Marshal.PtrToStringAnsi(pSignBuff);
- basInfo = outString;
- regInfo = signString;
- Global.pat.card.BusinessCode = signString;
- }
- else
- {
- string outString = Marshal.PtrToStringAnsi(pOutBuff);
- string signString = Marshal.PtrToStringAnsi(pSignBuff);
- basInfo = outString;
- regInfo = signString;
- }
- return rtn;
- }
- catch (Exception ex)
- {
- basInfo = "ReadCardBas异常:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("读社保卡", "", basInfo);
-
-
-
-
- Marshal.FreeHGlobal(pOutBuff);
- Marshal.FreeHGlobal(pSignBuff);
- }
- }
-
-
-
-
-
-
- public int ReadSFZ(out string OutBuff)
- {
- OutBuff = "";
- int rtn = -1;
- try
- {
- StringBuilder sbOutBuff = new StringBuilder(1024);
- StringBuilder sbBusiCardBuff = new StringBuilder(1024);
- rtn = ReadSFZ(sbOutBuff, 1024, sbBusiCardBuff, 1024);
-
- OutBuff = sbOutBuff.ToString();
- Global.pat.card.BusinessCode = sbBusiCardBuff.ToString();
- return rtn;
- }
- catch (Exception ex)
- {
- OutBuff = "ReadSFZ 异常:" + ex.Message;
- return rtn;
- }
- }
- public int ReadECToken(string URL, string inputParam, out string sOutPar)
- {
- sOutPar = "";
- StringBuilder sbURL = new StringBuilder(URL);
- StringBuilder sbInput = new StringBuilder(inputParam);
- StringBuilder sbOut = new StringBuilder(40960);
- IntPtr pInt = CardReader.NationEcTrans(sbURL, sbInput, sbOut);
- string outputData = sbOut.ToString();
-
- sOutPar = outputData;
- if (JsonHelper.getDestValue(JObject.Parse(outputData), "code") != "0")
- {
- return -1;
- }
- return 0;
- }
-
-
-
-
-
- public int ECQuery(out string OutBuff)
- {
- OutBuff = "";
- try
- {
- EcQueryData ecqueryData = new EcQueryData();
- ecqueryData.orgId = Global.inf.hospitalNO;
- ecqueryData.operatorId = Global.user.ID;
- ecqueryData.operatorName = Global.user.name;
- ecqueryData.officeId = Global.user.officeID;
- ecqueryData.officeName = Global.user.officeName;
- ecqueryData.businessType = Global.businessType;
-
-
- NationECInput nationECInput = new NationECInput();
- nationECInput.data = ecqueryData;
- nationECInput.orgId = Global.inf.hospitalNO;
- nationECInput.transType = "ec.query";
- nationECInput.extra = "";
-
-
- NationECCodeAPI nationECCodeAPI = new NationECCodeAPI(Global.inf.ecURL);
- return nationECCodeAPI.NationEcTrans(nationECInput, out OutBuff);
- }
- catch (Exception ex)
- {
- OutBuff = "ECQuery 异常:" + ex.Message;
- return -1;
- }
- }
- public static string parseECTokenOutput(string data)
- {
- JObject joRtn = JObject.Parse(data);
- Global.pat.ecToken = JsonHelper.getDestValue(joRtn, "data.ecToken");
- Global.pat.certNO = JsonHelper.getDestValue(joRtn, "data.idNo");
- Global.pat.name = JsonHelper.getDestValue(joRtn, "data.userName");
- Global.pat.certType = JsonHelper.getDestValue(joRtn, "data.idType");
- Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joRtn, "data.insuOrg");
-
- Global.pat.mdtrtcertNO = Global.pat.ecToken;
- return data;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- class CardReaderBySSCardDriver
- {
- [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardUnclassifiedBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ReadCardUnclassifiedBas(int iType, IntPtr pOutInfo);
- [DllImport("SSCardDriver.dll", EntryPoint = "iReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ReadSFZ(StringBuilder pOutInfo);
- [DllImport("SSCardDriver.dll", EntryPoint = "iGetQRBase", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int GetQRBase(int TimeOut, IntPtr pOutInfo);
- public int ReadCardUnclassifiedBas(int iType, out string basInfo)
- {
- basInfo = "";
- int rtn = -1;
-
-
-
-
- IntPtr pOutBuff = Marshal.AllocHGlobal(1024);
- try
- {
- rtn = ReadCardUnclassifiedBas(iType, pOutBuff);
- if (rtn == 0)
- {
-
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- else
- {
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- return rtn;
- }
- catch (Exception ex)
- {
- basInfo = "ReadCardUnclassifiedBas异常:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("读社保卡", "", basInfo);
-
-
-
-
- Marshal.FreeHGlobal(pOutBuff);
- }
- }
-
-
-
-
-
- public int ReadSFZ(out string OutBuff)
- {
- OutBuff = "";
- int rtn = -1;
- try
- {
- StringBuilder sbOutBuff = new StringBuilder(1024);
- rtn = ReadSFZ(sbOutBuff);
-
- OutBuff = sbOutBuff.ToString();
- return rtn;
- }
- catch (Exception ex)
- {
- OutBuff = "ReadSFZ 异常:" + ex.Message;
- return rtn;
- }
- }
- public int GetQRBase(int TimeOut, out string basInfo)
- {
- basInfo = "";
- int rtn = -1;
-
- IntPtr pOutBuff = Marshal.AllocHGlobal(1024);
- try
- {
- rtn = GetQRBase(TimeOut, pOutBuff);
- if (rtn == 0)
- {
-
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- else
- {
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- return rtn;
- }
- catch (Exception ex)
- {
- basInfo = "GetQRBase:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("读社保卡", "", basInfo);
-
-
-
-
- Marshal.FreeHGlobal(pOutBuff);
- }
- }
- }
-
-
-
- class CardReaderByHdEmb
- {
- [DllImport("SSSE32.dll", EntryPoint = "ICC_Reader_Open", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ICC_Reader_Open(IntPtr pDevName);
- [DllImport("SSSE32.dll", EntryPoint = "iReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
- static extern int ReadCardBas(int iType, IntPtr pOutInfo);
- public int ICC_Reader_Open(string devName, out string error)
- {
- error = "";
- int rtn = -1;
-
- IntPtr pOutBuff = Marshal.StringToHGlobalAnsi(devName);
- try
- {
- rtn = ICC_Reader_Open(pOutBuff);
- if (rtn == 0)
- {
- error = "端口打开成功";
- }
- else
- {
- error = Marshal.PtrToStringAnsi(pOutBuff) + "端口打开失败";
- }
- return rtn;
- }
- catch (Exception ex)
- {
- error = "打开端口异常:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("打开端口", devName, error);
-
- Marshal.FreeHGlobal(pOutBuff);
- }
- }
- public int ReadCardBas(int iType, out string basInfo)
- {
- basInfo = "";
- int rtn = -1;
-
-
-
-
- IntPtr pOutBuff = Marshal.AllocHGlobal(1024);
- try
- {
- rtn = ReadCardBas(iType, pOutBuff);
- if (rtn == 0)
- {
-
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- else
- {
- basInfo = Marshal.PtrToStringAnsi(pOutBuff);
- }
- return rtn;
- }
- catch (Exception ex)
- {
- basInfo = "ReadCardBas:" + ex.Message;
- return rtn;
- }
- finally
- {
- Global.writeLog("读社保卡", "", basInfo);
-
- Marshal.FreeHGlobal(pOutBuff);
- }
- }
- }
- }
|