using Newtonsoft.Json.Linq; using PTMedicalInsurance.CardReaders; using PTMedicalInsurance.Common; using PTMedicalInsurance.Entity; using PTMedicalInsurance.Forms; using PTMedicalInsurance.Helper; using PTMedicalInsurance.Variables; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace PTMedicalInsurance.Business { class ReadCardProcess : AbstractProcess { private static readonly Semaphore semaphore = new Semaphore(1, 1); public override CallResult Process(JObject input) { JObject joCardInfo = new JObject(); ChooseCard cc = new ChooseCard(); JObject joRtn = new JObject(); JObject joInput = new JObject(); string msg; try { DialogResult dialog = tools.ShowAppDiaglog(cc); if (dialog == DialogResult.OK) { Global.businessType = ""; Global.pat.certType = "01"; //电子凭证/人脸识别 if ((cc.cardType == "01") || (cc.cardType == "05")) { if (cc.cardType == "01") { Global.pat.mdtrtcertType = "01"; Global.businessType = cc.businessType; CardReader reader = CardReader.Instance; if (reader.ECQuery(out msg) != 0) { outParam = "读电子凭证失败!" + msg; return Error(outParam); }; JObject joTmp = JObject.Parse(msg); if (joTmp["code"].ToString() != "0") { outParam = "读电子凭证失败!" + joTmp["message"].ToString(); return Error(outParam); } Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joTmp, "data.ecToken"); Global.pat.ecToken = Global.pat.mdtrtcertNO; Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joTmp, "data.insuOrg"); Global.pat.certType = JsonHelper.getDestValue(joTmp, "data.idType"); Global.pat.certNO = JsonHelper.getDestValue(joTmp, "data.idNo"); Global.pat.name = JsonHelper.getDestValue(joTmp, "data.userName"); trade1101(out outParam); return Parse(outParam); } else { Global.pat.mdtrtcertType = "01"; Global.pat.certType = "01"; Random rd = new Random(); int iNum = rd.Next(); string sLSH = DateTime.Now.ToString("YYYYMMDDHHNNSS"); // + Global.inf.hospitalNO + iNum.ToString().Substring(0, 5); joCardInfo.Add("operatorId", Global.user.ID); joCardInfo.Add("operatorName", Global.user.name); joCardInfo.Add("officeId", "A10"); joCardInfo.Add("officeName", "眼科"); joCardInfo.Add("orgId", Global.inf.hospitalNO); joCardInfo.Add("businessType", cc.businessType); //1.调用人脸识别认证 cn.nhsa.ec.auth joCardInfo.Add("outBizNo", iNum); joCardInfo.Add("extData", ""); JObject JoInputAuth = new JObject(); JoInputAuth.Add("orgId", Global.inf.hospitalNO); JoInputAuth.Add("transType", "cn.nhsa.ec.auth"); JoInputAuth.Add("data", joCardInfo); JoInputAuth.Add("extData", ""); CardReader reader = CardReader.Instance; //调用刷脸认证接口cn.nhsa.ec.auth返回授权信息authNo reader.ReadECToken(Global.inf.ecURL, JoInputAuth.ToString(), out msg); if (JsonHelper.getDestValue(JObject.Parse(msg), "code") != "0") { outParam = "调用刷脸认证接口cn.nhsa.ec.auth失败:" + msg; return Error(outParam); } else { //成功返回授权信息 string auth = JsonHelper.getDestValue(JObject.Parse(msg), "data.authNo"); JObject JoAuthCheckInput = new JObject(); JoAuthCheckInput.Add("orgId", Global.inf.hospitalNO); JoAuthCheckInput.Add("outBizNo", iNum.ToString().Substring(0, 9)); JoAuthCheckInput.Add("authNo", auth); JoAuthCheckInput.Add("businessType", cc.businessType); JoAuthCheckInput.Add("operatorId", Global.user.ID); JoAuthCheckInput.Add("operatorName", Global.user.name); JoAuthCheckInput.Add("officeId", Global.user.officeID); JoAuthCheckInput.Add("officeName", Global.user.officeName); JoAuthCheckInput.Add("extData", ""); JObject JoAuthChck = new JObject(); JoAuthChck.Add("orgId", Global.inf.hospitalNO); JoAuthChck.Add("transType", "cn.nhsa.auth.check"); JoAuthChck.Add("data", JoAuthCheckInput); JoAuthChck.Add("extra", ""); //2.调用刷脸认证接口cn.nhsa.ec.auth返回授权信息authNo reader.ReadECToken(Global.inf.ecURL, JoAuthChck.ToString(), out msg); if (JsonHelper.getDestValue(JObject.Parse(msg), "code") != "0") { outParam = "调用刷脸授权获取医保身份接口cn.nhsa.auth.check失败:" + msg; return Error(outParam); } else { Global.pat.ecToken = JsonHelper.getDestValue(JObject.Parse(msg), "data.ecToken"); Global.pat.name = JsonHelper.getDestValue(JObject.Parse(msg), "data.userName"); Global.pat.IDType = JsonHelper.getDestValue(JObject.Parse(msg), "data.idType"); Global.pat.IDNO = JsonHelper.getDestValue(JObject.Parse(msg), "data.idNo"); Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(msg), "data.insuOrg"); Global.pat.mdtrtcertNO = Global.pat.ecToken; Global.pat.certType = Global.pat.IDType; Global.pat.certNO = Global.pat.IDNO; } } trade1101(out outParam); return Parse(outParam); } } else //身份证 if (cc.cardType == "02") { Global.pat.mdtrtcertType = "02"; int rtn = -1; //手工输入身份证 if (cc.tbID.Text != "") { Global.pat.certType = "01"; Global.pat.mdtrtcertNO= Global.pat.certNO = cc.tbID.Text; Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; rtn = 0; } else { CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver(); if (reader.ReadSFZ(out msg) != 0) { outParam = "读身份证失败!" + msg; return Error(outParam); }; Global.pat.certType = "01"; Global.pat.mdtrtcertNO = tools.getDestPosStrBySpliterCardInfo(msg, 1, "^"); Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; rtn = 0; } if (rtn == 0) { trade1101(out outParam); } return Parse(outParam); } else //社保卡 if (cc.cardType == "03") { #region 调用读卡接口信息 Global.pat.mdtrtcertType = "03"; int rtn = 0; string basInfo = "", regInfo = "", sOutPar = ""; //重复初始化导致闪退,换一种写法 //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode); Global.writeLog_debug($"调用单例模式类,程序应用域:{AppDomain.CurrentDomain.FriendlyName}--{AppDomain.CurrentDomain.Id}"); AppDomain currentDomain = AppDomain.CurrentDomain; CardReader reader = CardReader.Instance; reader.Initialize(Global.inf.cardURL, Global.inf.areaCode); string mutexName = "Global\\MyApp_InitializedMutex"; // 确保名称唯一 bool initialized; //if (Mutex.OpenExisting(mutexName) != null) //{ // Global.writeLog_debug($"Mutex:检测到已初始化"); //} //else //{ // Mutex mutex = new Mutex(true, mutexName, out initialized); // Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!"); // if (reader.Init() != 0) // { // outParam = "读社保卡,初始化失败!"; // return Error("读社保卡"); // }; //} // 创建新的互斥体 bool createdNew; //using (Mutex mutex = new Mutex(true, mutexName, out createdNew)) Mutex mutex = new Mutex(true, mutexName, out createdNew); { if (createdNew) { Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!"); try { // 执行初始化逻辑 if (reader.Init() != 0) { outParam = "读社保卡,初始化失败!"; return Error("读社保卡"); } Global.writeLog_debug("Mutex 初始化完成!"); } catch (Exception ex) { Global.writeLog_debug($"初始化过程中发生异常: {ex.Message}"); outParam = "初始化过程中发生错误!"; return Error("初始化失败"); } } else { Global.writeLog_debug("Mutex 检测到已初始化!"); } // 确保释放互斥体 mutex.ReleaseMutex(); } //二代社保卡可用 if (reader.ReadCardBas(out basInfo, out regInfo) != 0) { outParam = "读社保卡,获取卡信息失败!" + basInfo; return Error("读社保卡"); } else { //【社保卡读卡成功返回】 Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地 Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码 Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号 Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码 Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名 Global.pat.certType = cc.psnCertType; Global.pat.mdtrtcertType = "03"; Global.pat.mdtrtcertNO = Global.pat.certNO; if (cc.bYDSBK) { Global.pat.mdtrtcertNO = Global.pat.card.NO; } if (Global.pat.insuplc_admdvs .Substring(0,2)!= "44") { Global.pat.mdtrtcertNO = Global.pat.card.NO; } //清除原数据 trade1101(out outParam); } //// 明泰读卡器 //CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver(); //if (reader.ReadCardUnclassifiedBas(3,out basInfo) != 0) //{ // outParam = "读社保卡,获取卡信息失败!" + basInfo; // return Error("读社保卡"); //} //else //{ // //【社保卡读卡成功返回】 // Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地 // Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码 // Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号 // Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码 // Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名 // Global.pat.certType = cc.psnCertType; // Global.pat.mdtrtcertType = "03"; // Global.pat.mdtrtcertNO = Global.pat.certNO; // if (cc.bYDSBK) // { // Global.pat.mdtrtcertNO = Global.pat.card.NO; // } // if (Global.pat.mdtrtcertNO =="") // { // Global.pat.mdtrtcertNO = Global.pat.card.NO; // } // //清除原数据 // trade1101(out outParam); //} return Parse(outParam); #endregion } else //非中国大陆公民身份证人员 if (cc.cardType == "04") { Global.pat.certType = cc.psnCertType; Global.pat.mdtrtcertNO = Global.pat.certNO = cc.tbID.Text; Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; //非大陆公民读卡参保地传440300 Global.pat.mdtrtcertType = "99"; trade1101(out outParam); return Parse(outParam); } } else { //outParam = JsonHelper.setExceptionJson().ToString(); return Exception(-1, "读卡", "取消读卡"); } return Success(); } catch (Exception ex) { return Error("异常:" + ex.Message); } } /// /// 通过证件号获取基本信息(无卡) /// /// /// /// public int trade1101(out string outParam) { outParam = ""; string errorMsg = ""; JObject joInput = new JObject(); JObject joData = new JObject(); joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType); joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO); //Utils.ConvertMdtrtcertNo() joData.Add("card_sn", Global.pat.card.SN); joData.Add("begntime", Utils.GetShortDateTimeNow()); joData.Add("psn_cert_type", Global.pat.certType); joData.Add("certno", Global.pat.certNO); //证件号码 joData.Add("psn_name", Global.pat.name); joInput.Add("data", joData); InvokeHelper invoker = new InvokeHelper(); JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput); if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0) { outParam = "获取病人信息失败:" + errorMsg; return -1; } else { //joRtn["output"]["baseinfo"]["bizCode"] = Global.pat.card.BusinessCode; outParam = joRtn.ToString(); parsePatient(joRtn); return 0; } } /// /// 读卡并获取基本信息 /// /// /// public int trade1161(out string outParam) { outParam = ""; string errorMsg = ""; JObject joInput = new JObject(); joInput.Add("begntime", Utils.GetShortDateTimeNow()); JObject joRtn = new JObject(); joRtn = invoker.invokeCenterService(TradeEnum.ReadCardInfo, joInput); if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0) { outParam = "读卡失败:" + errorMsg; return -1; } else { parsePatient(joRtn); outParam = joRtn.ToString(); } return 0; } /// /// 解析人员基本信息 /// /// public void parsePatient(JObject joRtn) { // 基线版 PersonCardInfo info = JsonHelper.getOutput(joRtn); if (info.cardInfo != null) { Global.pat.card.NO = info.cardInfo.cardno; Global.pat.card.SN = info.cardInfo.card_sn; Global.pat.card.Cardtoken = info.cardInfo.ecToken; Global.pat.ecToken = info.cardInfo.ecToken; } //Global.writeLog("病人信息:"+joRtn.ToString()); if (info.insuInfo != null && info.insuInfo.Length > 0) { //参保地 Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs; Global.pat.insuplc_name = info.insuInfo[0].insuplc_name; Global.pat.medType = info.insuInfo[0].med_type ?? "C"; } //证件号 Global.pat.certNO = info.baseInfo?.certno; // 人员证件类型 Global.pat.certType = info.baseInfo?.psn_cert_type; // 就诊类型 if (string.IsNullOrEmpty(Global.pat.mdtrtcertType)) { Global.pat.mdtrtcertType = "03"; } // 就诊凭证号 if (!string.IsNullOrEmpty(Global.pat.card.NO)) { Global.pat.mdtrtcertNO = Global.pat.card.NO; } // 没有卡号用证件号 if (string.IsNullOrEmpty(Global.pat.mdtrtcertNO)) { Global.pat.mdtrtcertNO = Global.pat.certNO; } Global.pat.payOrdId = info.platformOrderNo; } } }