ReadCardProcess.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.CardReaders;
  3. using PTMedicalInsurance.Common;
  4. using PTMedicalInsurance.Helper;
  5. using PTMedicalInsurance.Variables;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Threading;
  12. namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.ReadCard
  13. {
  14. class ReadCardProcess:AbstractProcess
  15. {
  16. private static readonly Semaphore semaphore = new Semaphore(1, 1);
  17. public override CallResult Process(JObject input)
  18. {
  19. outParam = "";
  20. string sOutPar;
  21. JObject joRtn = new JObject();
  22. JObject joInput = new JObject();
  23. InvokeHelper invoker = new InvokeHelper();
  24. try
  25. {
  26. Global.pat.certType = "01";
  27. //电子凭证
  28. if (Global.ssmp.cardType == "02")
  29. {
  30. Global.pat.mdtrtcertType = "01";
  31. //CardReader reader = new CardReader();
  32. CardReader reader = CardReader.Instance;
  33. if (reader.ECQuery(out outParam) != 0)
  34. {
  35. outParam = "读电子凭证失败!" + outParam;
  36. return Error(outParam);
  37. };
  38. JObject joTmp = JObject.Parse(outParam);
  39. if (joTmp["code"].ToString() != "0")
  40. {
  41. outParam = "读电子凭证失败!" + joTmp["message"].ToString();
  42. return Error(outParam);
  43. }
  44. Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joTmp, "data.ecToken");
  45. Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joTmp, "data.insuOrg");
  46. Global.pat.certType = JsonHelper.getDestValue(joTmp, "data.idType");
  47. Global.pat.certNO = JsonHelper.getDestValue(joTmp, "data.idNo");
  48. Global.pat.name = JsonHelper.getDestValue(joTmp, "data.userName");
  49. }
  50. //身份证
  51. if (Global.ssmp.cardType == "04")
  52. {
  53. if (Global.pat.card.SearchAdmKey != "")
  54. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  55. Global.pat.mdtrtcertType = "02";
  56. //Global.pat.mdtrtcertNO = cc.sP_CertNO;
  57. Global.pat.certType = "01";
  58. //Global.pat.certNO = cc.sP_IDNO;
  59. //Global.pat.name = cc.PatName;
  60. Global.pat.card.SN = "";
  61. //物理读身份证
  62. //CardReader reader = new CardReader();
  63. CardReader reader = CardReader.Instance;
  64. //reader.Initialize(Global.inf.cardURL, Global.inf.areaCode);
  65. if (reader.ReadSFZ(out outParam) != 0)
  66. {
  67. outParam = "读身份证失败!" + outParam ;
  68. return Error(outParam);
  69. };
  70. Global.pat.mdtrtcertNO = JsonHelper.getDestValue(JObject.Parse(outParam), "IDCardNo");
  71. }
  72. //社保卡
  73. //深圳星耀自助机读卡器是德卡
  74. if (Global.ssmp.cardType == "03")
  75. {
  76. //深圳星耀普瑞自助机是德卡
  77. if (Global.inf.hospitalNO == "H44030305189")
  78. {
  79. CardReader reader = CardReader.Instance;
  80. Global.pat.mdtrtcertType = "03";
  81. int rtn = 0;
  82. string basInfo = "", regInfo = "";
  83. //重复初始化导致闪退,换一种写法
  84. //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode);
  85. Global.writeLog_debug($"调用单例模式类,程序应用域:{AppDomain.CurrentDomain.FriendlyName}--{AppDomain.CurrentDomain.Id}");
  86. AppDomain currentDomain = AppDomain.CurrentDomain;
  87. //CardReader reader = CardReader.Instance;
  88. reader.Initialize(Global.inf.cardURL, Global.inf.areaCode);
  89. string mutexName = "Global\\MyApp_InitializedMutex"; // 确保名称唯一
  90. // 创建新的互斥体
  91. bool createdNew;
  92. using (Mutex mutex = new Mutex(true, mutexName, out createdNew))
  93. // Mutex mutex = new Mutex(true, mutexName, out createdNew);
  94. {
  95. if (createdNew)
  96. {
  97. Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!");
  98. try
  99. {
  100. // 执行初始化逻辑
  101. if (reader.Init() != 0)
  102. {
  103. outParam = "读社保卡,初始化失败!";
  104. return Error("读社保卡");
  105. }
  106. Global.writeLog_debug("Mutex 初始化完成!");
  107. }
  108. catch (Exception ex)
  109. {
  110. Global.writeLog_debug($"初始化过程中发生异常: {ex.Message}");
  111. outParam = "初始化过程中发生错误!";
  112. return Error("初始化失败");
  113. }
  114. }
  115. else
  116. {
  117. Global.writeLog_debug("Mutex 检测到已初始化!");
  118. }
  119. // 确保释放互斥体
  120. mutex.ReleaseMutex();
  121. }
  122. //二代社保卡可用
  123. if (reader.ReadCardBas(out basInfo, out regInfo) != 0)
  124. {
  125. outParam = "读社保卡,获取卡信息失败!" + basInfo;
  126. return Error("读社保卡");
  127. }
  128. else
  129. {
  130. //【社保卡读卡成功返回】
  131. Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  132. Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  133. Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  134. Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  135. Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  136. Global.pat.certType = "01";
  137. Global.pat.mdtrtcertType = "03";
  138. Global.pat.mdtrtcertNO = Global.pat.certNO;
  139. //异地患者需要传卡号,否则超长错误
  140. if (Global.pat.insuplc_admdvs.Substring(0, 2) != "44")
  141. {
  142. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  143. }
  144. //清除原数据
  145. trade1101(out outParam);
  146. }
  147. }
  148. else //深圳普瑞自助机内置的是华大的读卡器
  149. {
  150. Global.pat.mdtrtcertType = "03";
  151. int rtn = 0;
  152. string basInfo = "", regInfo = "";
  153. CardReaderByHdEmb reader = new CardReaderByHdEmb();
  154. if (reader.ICC_Reader_Open("USB1", out errMsg) != 0)
  155. {
  156. //return Error(errMsg);
  157. }
  158. if (reader.ReadCardBas(3, out basInfo) != 0)
  159. {
  160. outParam = "读社保卡,获取卡信息失败!" + basInfo;
  161. return Error("读社保卡USB1");
  162. }
  163. else
  164. {
  165. //【社保卡读卡成功返回】
  166. Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  167. Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  168. Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  169. Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  170. Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  171. Global.pat.certType = "01";
  172. Global.pat.mdtrtcertType = "03";
  173. Global.pat.mdtrtcertNO = Global.pat.certNO;
  174. //异地患者需要传卡号,否则超长错误
  175. if (Global.pat.insuplc_admdvs.Substring(0, 2) != "44")
  176. {
  177. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  178. }
  179. }
  180. }
  181. }
  182. if (trade1101(out outParam) == 0)
  183. {
  184. return Success();
  185. }
  186. return Exception(-1, "读卡", outParam);
  187. }
  188. catch (Exception ex)
  189. {
  190. return Error("异常:" + ex.Message);
  191. }
  192. }
  193. /// <summary>
  194. /// 通过证件号获取基本信息(无卡)
  195. /// </summary>
  196. /// <param name="cardNo"></param>
  197. /// <param name="outParam"></param>
  198. /// <returns></returns>
  199. public int trade1101(out string outParam)
  200. {
  201. #region【1101获取身份信息】
  202. JObject joData = new JObject();
  203. joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
  204. joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
  205. joData.Add("card_sn", Global.pat.card.SN);
  206. //joData.Add("begntime", "");
  207. joData.Add("psn_cert_type", Global.pat.certType);
  208. joData.Add("certno", Global.pat.certNO); //证件号码
  209. joData.Add("psn_name", Global.pat.name);
  210. JObject joInput = new JObject();
  211. joInput.Add("data", joData);
  212. //InvokeHelper invoker = new InvokeHelper();
  213. JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput);
  214. if (JsonHelper.parseCenterRtnValue(joRtn, out outParam) != 0)
  215. {
  216. outParam = "读卡失败:" + outParam;
  217. return -1;
  218. }
  219. else
  220. {
  221. //joRtn.Add("cardinfo",joCardInfo);
  222. outParam = joRtn.ToString();
  223. Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(outParam), "output.insuinfo.insuplc_admdvs"); //参保地
  224. Global.pat.certNO = JsonHelper.getDestValue(JObject.Parse(outParam), "output.baseinfo.certno"); //证件号码
  225. Global.pat.psn_no = JsonHelper.getDestValue(JObject.Parse(outParam), "output.baseinfo.psn_no"); //人员编号
  226. outParam = outParam.Replace(@"\", "").Replace("\"{", "{").Replace("}\"", "}");
  227. return 0;
  228. }
  229. #endregion
  230. }
  231. }
  232. }