ReadCardProcess.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.CardReaders;
  3. using PTMedicalInsurance.Common;
  4. using PTMedicalInsurance.Entity;
  5. using PTMedicalInsurance.Forms;
  6. using PTMedicalInsurance.Helper;
  7. using PTMedicalInsurance.Variables;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace PTMedicalInsurance.Business
  16. {
  17. class ReadCardProcess : AbstractProcess
  18. {
  19. private static readonly Semaphore semaphore = new Semaphore(1, 1);
  20. public override CallResult Process(JObject input)
  21. {
  22. JObject joCardInfo = new JObject();
  23. ChooseCard cc = new ChooseCard();
  24. JObject joRtn = new JObject();
  25. JObject joInput = new JObject();
  26. string msg;
  27. try
  28. {
  29. DialogResult dialog = tools.ShowAppDiaglog(cc);
  30. if (dialog == DialogResult.OK)
  31. {
  32. Global.businessType = "";
  33. Global.pat.certType = "01";
  34. //电子凭证/人脸识别
  35. if ((cc.cardType == "01") || (cc.cardType == "05"))
  36. {
  37. if (cc.cardType == "01")
  38. {
  39. Global.pat.mdtrtcertType = "01";
  40. Global.businessType = cc.businessType;
  41. CardReader reader = CardReader.Instance;
  42. if (reader.ECQuery(out msg) != 0)
  43. {
  44. outParam = "读电子凭证失败!" + msg;
  45. return Error(outParam);
  46. };
  47. JObject joTmp = JObject.Parse(msg);
  48. if (joTmp["code"].ToString() != "0")
  49. {
  50. outParam = "读电子凭证失败!" + joTmp["message"].ToString();
  51. return Error(outParam);
  52. }
  53. Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joTmp, "data.ecToken");
  54. Global.pat.ecToken = Global.pat.mdtrtcertNO;
  55. Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joTmp, "data.insuOrg");
  56. Global.pat.certType = JsonHelper.getDestValue(joTmp, "data.idType");
  57. Global.pat.certNO = JsonHelper.getDestValue(joTmp, "data.idNo");
  58. Global.pat.name = JsonHelper.getDestValue(joTmp, "data.userName");
  59. trade1101(out outParam);
  60. return Parse(outParam);
  61. }
  62. else
  63. {
  64. Global.pat.mdtrtcertType = "01";
  65. Global.pat.certType = "01";
  66. Random rd = new Random();
  67. int iNum = rd.Next();
  68. string sLSH = DateTime.Now.ToString("YYYYMMDDHHNNSS"); // + Global.inf.hospitalNO + iNum.ToString().Substring(0, 5);
  69. joCardInfo.Add("operatorId", Global.user.ID);
  70. joCardInfo.Add("operatorName", Global.user.name);
  71. joCardInfo.Add("officeId", "A10");
  72. joCardInfo.Add("officeName", "眼科");
  73. joCardInfo.Add("orgId", Global.inf.hospitalNO);
  74. joCardInfo.Add("businessType", cc.businessType);
  75. //1.调用人脸识别认证 cn.nhsa.ec.auth
  76. joCardInfo.Add("outBizNo", iNum);
  77. joCardInfo.Add("extData", "");
  78. JObject JoInputAuth = new JObject();
  79. JoInputAuth.Add("orgId", Global.inf.hospitalNO);
  80. JoInputAuth.Add("transType", "cn.nhsa.ec.auth");
  81. JoInputAuth.Add("data", joCardInfo);
  82. JoInputAuth.Add("extData", "");
  83. CardReader reader = CardReader.Instance;
  84. //调用刷脸认证接口cn.nhsa.ec.auth返回授权信息authNo
  85. reader.ReadECToken(Global.inf.ecURL, JoInputAuth.ToString(), out msg);
  86. if (JsonHelper.getDestValue(JObject.Parse(msg), "code") != "0")
  87. {
  88. outParam = "调用刷脸认证接口cn.nhsa.ec.auth失败:" + msg;
  89. return Error(outParam);
  90. }
  91. else
  92. {
  93. //成功返回授权信息
  94. string auth = JsonHelper.getDestValue(JObject.Parse(msg), "data.authNo");
  95. JObject JoAuthCheckInput = new JObject();
  96. JoAuthCheckInput.Add("orgId", Global.inf.hospitalNO);
  97. JoAuthCheckInput.Add("outBizNo", iNum.ToString().Substring(0, 9));
  98. JoAuthCheckInput.Add("authNo", auth);
  99. JoAuthCheckInput.Add("businessType", cc.businessType);
  100. JoAuthCheckInput.Add("operatorId", Global.user.ID);
  101. JoAuthCheckInput.Add("operatorName", Global.user.name);
  102. JoAuthCheckInput.Add("officeId", Global.user.officeID);
  103. JoAuthCheckInput.Add("officeName", Global.user.officeName);
  104. JoAuthCheckInput.Add("extData", "");
  105. JObject JoAuthChck = new JObject();
  106. JoAuthChck.Add("orgId", Global.inf.hospitalNO);
  107. JoAuthChck.Add("transType", "cn.nhsa.auth.check");
  108. JoAuthChck.Add("data", JoAuthCheckInput);
  109. JoAuthChck.Add("extra", "");
  110. //2.调用刷脸认证接口cn.nhsa.ec.auth返回授权信息authNo
  111. reader.ReadECToken(Global.inf.ecURL, JoAuthChck.ToString(), out msg);
  112. if (JsonHelper.getDestValue(JObject.Parse(msg), "code") != "0")
  113. {
  114. outParam = "调用刷脸授权获取医保身份接口cn.nhsa.auth.check失败:" + msg;
  115. return Error(outParam);
  116. }
  117. else
  118. {
  119. Global.pat.ecToken = JsonHelper.getDestValue(JObject.Parse(msg), "data.ecToken");
  120. Global.pat.name = JsonHelper.getDestValue(JObject.Parse(msg), "data.userName");
  121. Global.pat.IDType = JsonHelper.getDestValue(JObject.Parse(msg), "data.idType");
  122. Global.pat.IDNO = JsonHelper.getDestValue(JObject.Parse(msg), "data.idNo");
  123. Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(msg), "data.insuOrg");
  124. Global.pat.mdtrtcertNO = Global.pat.ecToken;
  125. Global.pat.certType = Global.pat.IDType;
  126. Global.pat.certNO = Global.pat.IDNO;
  127. }
  128. }
  129. trade1101(out outParam);
  130. return Parse(outParam);
  131. }
  132. }
  133. else
  134. //身份证
  135. if (cc.cardType == "02")
  136. {
  137. Global.pat.mdtrtcertType = "02";
  138. int rtn = -1;
  139. //手工输入身份证
  140. if (cc.tbID.Text != "")
  141. {
  142. Global.pat.certType = "01";
  143. Global.pat.mdtrtcertNO= Global.pat.certNO = cc.tbID.Text;
  144. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  145. rtn = 0;
  146. }
  147. else
  148. {
  149. CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver();
  150. if (reader.ReadSFZ(out msg) != 0)
  151. {
  152. outParam = "读身份证失败!" + msg;
  153. return Error(outParam);
  154. };
  155. Global.pat.certType = "01";
  156. Global.pat.mdtrtcertNO = tools.getDestPosStrBySpliterCardInfo(msg, 1, "^");
  157. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  158. rtn = 0;
  159. }
  160. if (rtn == 0)
  161. {
  162. trade1101(out outParam);
  163. }
  164. return Parse(outParam);
  165. }
  166. else
  167. //社保卡
  168. if (cc.cardType == "03")
  169. {
  170. #region 调用读卡接口信息
  171. Global.pat.mdtrtcertType = "03";
  172. int rtn = 0;
  173. string basInfo = "", regInfo = "", sOutPar = "";
  174. //重复初始化导致闪退,换一种写法
  175. //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode);
  176. Global.writeLog_debug($"调用单例模式类,程序应用域:{AppDomain.CurrentDomain.FriendlyName}--{AppDomain.CurrentDomain.Id}");
  177. AppDomain currentDomain = AppDomain.CurrentDomain;
  178. CardReader reader = CardReader.Instance;
  179. reader.Initialize(Global.inf.cardURL, Global.inf.areaCode);
  180. string mutexName = "Global\\MyApp_InitializedMutex"; // 确保名称唯一
  181. bool initialized;
  182. //if (Mutex.OpenExisting(mutexName) != null)
  183. //{
  184. // Global.writeLog_debug($"Mutex:检测到已初始化");
  185. //}
  186. //else
  187. //{
  188. // Mutex mutex = new Mutex(true, mutexName, out initialized);
  189. // Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!");
  190. // if (reader.Init() != 0)
  191. // {
  192. // outParam = "读社保卡,初始化失败!";
  193. // return Error("读社保卡");
  194. // };
  195. //}
  196. // 创建新的互斥体
  197. bool createdNew;
  198. //using (Mutex mutex = new Mutex(true, mutexName, out createdNew))
  199. Mutex mutex = new Mutex(true, mutexName, out createdNew);
  200. {
  201. if (createdNew)
  202. {
  203. Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!");
  204. try
  205. {
  206. // 执行初始化逻辑
  207. if (reader.Init() != 0)
  208. {
  209. outParam = "读社保卡,初始化失败!";
  210. return Error("读社保卡");
  211. }
  212. Global.writeLog_debug("Mutex 初始化完成!");
  213. }
  214. catch (Exception ex)
  215. {
  216. Global.writeLog_debug($"初始化过程中发生异常: {ex.Message}");
  217. outParam = "初始化过程中发生错误!";
  218. return Error("初始化失败");
  219. }
  220. }
  221. else
  222. {
  223. Global.writeLog_debug("Mutex 检测到已初始化!");
  224. }
  225. // 确保释放互斥体
  226. mutex.ReleaseMutex();
  227. }
  228. //二代社保卡可用
  229. if (reader.ReadCardBas(out basInfo, out regInfo) != 0)
  230. {
  231. outParam = "读社保卡,获取卡信息失败!" + basInfo;
  232. return Error("读社保卡");
  233. }
  234. else
  235. {
  236. //【社保卡读卡成功返回】
  237. Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  238. Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  239. Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  240. Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  241. Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  242. Global.pat.certType = cc.psnCertType;
  243. Global.pat.mdtrtcertType = "03";
  244. Global.pat.mdtrtcertNO = Global.pat.certNO;
  245. if (cc.bYDSBK)
  246. {
  247. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  248. }
  249. if (Global.pat.insuplc_admdvs .Substring(0,2)!= "44")
  250. {
  251. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  252. }
  253. //清除原数据
  254. trade1101(out outParam);
  255. }
  256. //// 明泰读卡器
  257. //CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver();
  258. //if (reader.ReadCardUnclassifiedBas(3,out basInfo) != 0)
  259. //{
  260. // outParam = "读社保卡,获取卡信息失败!" + basInfo;
  261. // return Error("读社保卡");
  262. //}
  263. //else
  264. //{
  265. // //【社保卡读卡成功返回】
  266. // Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  267. // Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  268. // Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  269. // Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  270. // Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  271. // Global.pat.certType = cc.psnCertType;
  272. // Global.pat.mdtrtcertType = "03";
  273. // Global.pat.mdtrtcertNO = Global.pat.certNO;
  274. // if (cc.bYDSBK)
  275. // {
  276. // Global.pat.mdtrtcertNO = Global.pat.card.NO;
  277. // }
  278. // if (Global.pat.mdtrtcertNO =="")
  279. // {
  280. // Global.pat.mdtrtcertNO = Global.pat.card.NO;
  281. // }
  282. // //清除原数据
  283. // trade1101(out outParam);
  284. //}
  285. return Parse(outParam);
  286. #endregion
  287. }
  288. else
  289. //非中国大陆公民身份证人员
  290. if (cc.cardType == "04")
  291. {
  292. Global.pat.certType = cc.psnCertType;
  293. Global.pat.mdtrtcertNO = Global.pat.certNO = cc.tbID.Text;
  294. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; //非大陆公民读卡参保地传440300
  295. Global.pat.mdtrtcertType = "99";
  296. trade1101(out outParam);
  297. return Parse(outParam);
  298. }
  299. }
  300. else
  301. {
  302. //outParam = JsonHelper.setExceptionJson().ToString();
  303. return Exception(-1, "读卡", "取消读卡");
  304. }
  305. return Success();
  306. }
  307. catch (Exception ex)
  308. {
  309. return Error("异常:" + ex.Message);
  310. }
  311. }
  312. /// <summary>
  313. /// 通过证件号获取基本信息(无卡)
  314. /// </summary>
  315. /// <param name="cardNo"></param>
  316. /// <param name="outParam"></param>
  317. /// <returns></returns>
  318. public int trade1101(out string outParam)
  319. {
  320. outParam = "";
  321. string errorMsg = "";
  322. JObject joInput = new JObject();
  323. JObject joData = new JObject();
  324. joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
  325. joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO); //Utils.ConvertMdtrtcertNo()
  326. joData.Add("card_sn", Global.pat.card.SN);
  327. joData.Add("begntime", Utils.GetShortDateTimeNow());
  328. joData.Add("psn_cert_type", Global.pat.certType);
  329. joData.Add("certno", Global.pat.certNO); //证件号码
  330. joData.Add("psn_name", Global.pat.name);
  331. joInput.Add("data", joData);
  332. InvokeHelper invoker = new InvokeHelper();
  333. JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput);
  334. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  335. {
  336. outParam = "获取病人信息失败:" + errorMsg;
  337. return -1;
  338. }
  339. else
  340. {
  341. //joRtn["output"]["baseinfo"]["bizCode"] = Global.pat.card.BusinessCode;
  342. outParam = joRtn.ToString();
  343. parsePatient(joRtn);
  344. return 0;
  345. }
  346. }
  347. /// <summary>
  348. /// 读卡并获取基本信息
  349. /// </summary>
  350. /// <param name="outParam"></param>
  351. /// <returns></returns>
  352. public int trade1161(out string outParam)
  353. {
  354. outParam = "";
  355. string errorMsg = "";
  356. JObject joInput = new JObject();
  357. joInput.Add("begntime", Utils.GetShortDateTimeNow());
  358. JObject joRtn = new JObject();
  359. joRtn = invoker.invokeCenterService(TradeEnum.ReadCardInfo, joInput);
  360. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  361. {
  362. outParam = "读卡失败:" + errorMsg;
  363. return -1;
  364. }
  365. else
  366. {
  367. parsePatient(joRtn);
  368. outParam = joRtn.ToString();
  369. }
  370. return 0;
  371. }
  372. /// <summary>
  373. /// 解析人员基本信息
  374. /// </summary>
  375. /// <param name="joRtn"></param>
  376. public void parsePatient(JObject joRtn)
  377. {
  378. // 基线版
  379. PersonCardInfo info = JsonHelper.getOutput<PersonCardInfo>(joRtn);
  380. if (info.cardInfo != null)
  381. {
  382. Global.pat.card.NO = info.cardInfo.cardno;
  383. Global.pat.card.SN = info.cardInfo.card_sn;
  384. Global.pat.card.Cardtoken = info.cardInfo.ecToken;
  385. Global.pat.ecToken = info.cardInfo.ecToken;
  386. }
  387. //Global.writeLog("病人信息:"+joRtn.ToString());
  388. if (info.insuInfo != null && info.insuInfo.Length > 0)
  389. {
  390. //参保地
  391. Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs;
  392. Global.pat.insuplc_name = info.insuInfo[0].insuplc_name;
  393. Global.pat.medType = info.insuInfo[0].med_type ?? "C";
  394. }
  395. //证件号
  396. Global.pat.certNO = info.baseInfo?.certno;
  397. // 人员证件类型
  398. Global.pat.certType = info.baseInfo?.psn_cert_type;
  399. // 就诊类型
  400. if (string.IsNullOrEmpty(Global.pat.mdtrtcertType))
  401. {
  402. Global.pat.mdtrtcertType = "03";
  403. }
  404. // 就诊凭证号
  405. if (!string.IsNullOrEmpty(Global.pat.card.NO))
  406. {
  407. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  408. }
  409. // 没有卡号用证件号
  410. if (string.IsNullOrEmpty(Global.pat.mdtrtcertNO))
  411. {
  412. Global.pat.mdtrtcertNO = Global.pat.certNO;
  413. }
  414. Global.pat.payOrdId = info.platformOrderNo;
  415. }
  416. }
  417. }