ECTokenReader.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Entity;
  3. using PTMedicalInsurance.Helper;
  4. using PTMedicalInsurance.Variables;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace PTMedicalInsurance.Common
  13. {
  14. class ECTokenReader
  15. {
  16. //static string URL = "https://fuwu-test.nhsa.gov.cn/localcfc/api/hsecfc/localQrCodeQuery";
  17. [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
  18. static extern string NationEcTrans(string strUrl,string InData,StringBuilder OutData);
  19. public static string ECQuery(string EcCertDecodeType) {
  20. Random rd = new Random();
  21. int iNum = rd.Next();
  22. string bizNo = DateTime.Now.ToString("yyyyMMddHHmmsss") + Global.inf.hospitalNO + iNum.ToString().Substring(0, 5);
  23. ECTokenData eCToken = new ECTokenData();
  24. eCToken.orgId = Global.inf.hospitalNO;
  25. eCToken.operatorId = Global.user.ID;
  26. eCToken.operatorName = Global.user.name;
  27. eCToken.officeId = Global.user.officeID;
  28. eCToken.officeName = Global.user.officeName;
  29. eCToken.businessType = Global.businessType ;
  30. //eCToken.outBizNo = bizNo;
  31. //eCToken.extData = "";
  32. //认证方式
  33. //二维码
  34. if (EcCertDecodeType == "0")
  35. {
  36. eCToken.deviceType = Global.businessType;
  37. }
  38. //电子凭证
  39. else if (EcCertDecodeType == "1")
  40. {
  41. eCToken.outBizNo = bizNo;
  42. }
  43. //刷脸、先授权后解码
  44. else if (EcCertDecodeType == "2") //刷脸
  45. {
  46. //成功返回授权信息
  47. string auth = "";
  48. faceAuth(out auth, bizNo);
  49. if (!string.IsNullOrEmpty(auth))
  50. {
  51. eCToken.authNo = auth;
  52. }
  53. else {
  54. return "";
  55. }
  56. }
  57. //刷脸
  58. else if (EcCertDecodeType == "3") //刷脸
  59. {
  60. eCToken.obtainWay = "04";
  61. }
  62. string sOutPar = "";
  63. //调用电子凭证动态库NationECCode.dll获取身份信息
  64. //callECQuery(eCToken,"ec.query",out sOutPar);
  65. // 医保接口
  66. int ret = call1162(eCToken, out sOutPar);
  67. if (ret == 0)
  68. {
  69. JObject joRtn = JObject.Parse(sOutPar);
  70. Resp1101 info = JsonHelper.getOutput<Resp1101>(joRtn);
  71. Global.pat.card.NO = info.cardInfo.cardno;
  72. Global.pat.card.SN = info.cardInfo.card_sn;
  73. Global.pat.ecToken = info.cardInfo.ecToken;
  74. Global.pat.name = info.baseInfo.psn_name;
  75. Global.pat.certNO = info.baseInfo.certno;
  76. if (info.insuInfo != null && info.insuInfo.Length > 0)
  77. {
  78. //参保地
  79. Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs;
  80. }
  81. Global.pat.IDType = info.cardInfo.psn_cert_type;
  82. Global.pat.IDNO = info.cardInfo.certno;
  83. Global.pat.mdtrtcertNO = Global.pat.ecToken;
  84. Global.pat.certType = info.cardInfo.psn_cert_type;
  85. }
  86. else
  87. {
  88. sOutPar = new JObject { ["errorCode"] = -1, ["errorMessage"] = sOutPar }.ToString();
  89. }
  90. return sOutPar;
  91. }
  92. public static void faceAuth(out string outParam,string bizNo)
  93. {
  94. ECTokenData data = new ECTokenData();
  95. data.outBizNo = bizNo;
  96. data.extData = "";
  97. callECQuery(data, "cn.nhsa.ec.auth", out outParam);
  98. }
  99. private static int call1162(ECTokenData eCToken, out string sOutPar)
  100. {
  101. InvokeHelper invoker = new InvokeHelper();
  102. ECTokenInput joData = new ECTokenInput();
  103. joData.data = eCToken;
  104. string joInput = JsonHelper.toJsonString(joData);
  105. JObject joRtn = invoker.invokeCenterService("1162", JsonHelper.setCenterInpar("1162", joInput));
  106. string errorMsg = "";
  107. sOutPar = "";
  108. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  109. {
  110. sOutPar = "获取电子凭证信息失败:" + errorMsg;
  111. //Global.writeLog(sOutPar);
  112. return -1;
  113. }
  114. sOutPar = joRtn.ToString();
  115. return 0;
  116. }
  117. private static void callECQuery(ECTokenData eCToken,string transType,out string sOutPar)
  118. {
  119. var input = new ECTokenInput();
  120. input.data = eCToken;
  121. input.orgId = eCToken.orgId;
  122. input.transType = transType; // "ec.query";
  123. input.extra = "";
  124. sOutPar = "";
  125. try
  126. {
  127. string inputParam = JsonHelper.toJsonString(input);
  128. Global.writeLog("开始调用ECToken:"+ inputParam);
  129. StringBuilder sbOut = new StringBuilder(40960);
  130. string strToken = NationEcTrans(Global.inf.ecURL, inputParam, sbOut);
  131. sOutPar = sbOut.ToString();
  132. Global.writeLog("EcToken返回:"+strToken,inputParam,sOutPar);
  133. if (JsonHelper.getDestValue(JObject.Parse(sOutPar), "code") != "0")
  134. {
  135. Global.writeLog("调用认证接口" + transType + "失败:" + sOutPar);
  136. }
  137. }
  138. catch (Exception e)
  139. {
  140. Global.writeLog("调用电子凭证【"+ Global.inf.ecURL + "】异常:"+e.Message);
  141. }
  142. }
  143. }
  144. class ECTokenInput
  145. {
  146. /// <summary>
  147. /// 机构代码
  148. /// </summary>
  149. public string orgId { get; set; }
  150. /// <summary>
  151. /// 交易类型
  152. /// </summary>
  153. public string transType { get; set; }
  154. /// <summary>
  155. /// 请求参数
  156. /// </summary>
  157. public ECTokenData data { get; set; }
  158. /// <summary>
  159. /// 扩展参数
  160. /// </summary>
  161. public string extra { get; set; }
  162. }
  163. class ECTokenData
  164. {
  165. /// <summary>
  166. /// 医保定点机构代码
  167. /// </summary>
  168. public string orgId { get; set; }
  169. /// <summary>
  170. /// 用码业务类型
  171. /// </summary>
  172. public string businessType { get; set; }
  173. /// <summary>
  174. /// 收款员编号
  175. /// </summary>
  176. public object operatorId { get; set; }
  177. /// <summary>
  178. /// 收款员姓名
  179. /// </summary>
  180. public string operatorName { get; set; }
  181. /// <summary>
  182. /// 医保科室编号
  183. /// </summary>
  184. public string officeId { get; set; }
  185. /// <summary>
  186. /// 科室名称
  187. /// </summary>
  188. public string officeName { get; set; }
  189. public string outBizNo { get; set; }
  190. public string authNo { get; set; }
  191. public string deviceType { get; set; }
  192. /// <summary>
  193. /// 获取途径
  194. /// </summary>
  195. public string obtainWay { get; set; }
  196. public string extData { get; set; }
  197. }
  198. }