SsCardBusiness.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace ThCardReader
  6. {
  7. class SsCardBusiness
  8. {
  9. private static SiBusinessAddress address;
  10. #if !DISABLE_SSCARD
  11. [DllImport("SSCard.dll", EntryPoint = "Init", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  12. private extern static Int32 Init(string pUrl, string pUser);
  13. [DllImport("SSCard.dll", EntryPoint = "ReadCardBas", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  14. private extern static Int32 ReadCardBas(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);
  15. [DllImport("SSCard.dll", EntryPoint = "ReadSFZ", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  16. private extern static Int32 ReadSFZ(StringBuilder pOutBuff, int nOutBuffLen, StringBuilder pSignBuff, int nSignBuffLen);
  17. #endif
  18. private static Boolean initialized = false;
  19. public static string Initialize()
  20. {
  21. #if DISABLE_SSCARD
  22. return "SSCard.dll已屏蔽,请使用华视读卡器";
  23. #else
  24. address = MainForm.address;
  25. Int32 initResultCode = Init(address.readSiCardUrl, address.readSiCardUser);
  26. if (initResultCode == 0)
  27. {
  28. initialized = true;
  29. return "初始化成功";
  30. }
  31. return "初始化失败。";
  32. #endif
  33. }
  34. [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
  35. public static JObject ReadSiCard()
  36. {
  37. #if DISABLE_SSCARD
  38. JObject result = new JObject();
  39. result.Add("code", 1001);
  40. result.Add("message", "SSCard.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard");
  41. result.Add("device", "SSCard.dll(已屏蔽)");
  42. result.Add("suggestion", "请使用华视读卡器测试功能");
  43. return result;
  44. #else
  45. if (!initialized)
  46. {
  47. Initialize();
  48. }
  49. StringBuilder outptr = new StringBuilder(address.readSiCardBuffSize);
  50. StringBuilder signptr = new StringBuilder(address.readSiCardBuffSize);
  51. string outstr, signstr;
  52. JObject result = new JObject();
  53. try
  54. {
  55. Int32 businessResultCode = ReadCardBas(outptr, address.readSiCardBuffSize, signptr, address.readSiCardBuffSize);
  56. outstr = outptr.ToString();
  57. signstr = signptr.ToString();
  58. if (businessResultCode == 0)
  59. {
  60. result.Add("code", 200);
  61. result.Add("type", "sicard");
  62. result.Add("data", outstr);
  63. result.Add("sign", signstr);
  64. result.Add("message", "读取社保卡成功。");
  65. }
  66. else
  67. {
  68. result.Add("code", 1001);
  69. result.Add("message", outstr);
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. initialized = false;
  75. result.Add("code", 1001);
  76. result.Add("message", "系统出错,请重试(如此错误频繁出现,可尝试重启读卡服务)。" + e.ToString());
  77. }
  78. finally
  79. {
  80. outptr = null;
  81. signptr = null;
  82. outstr = null;
  83. signstr = null;
  84. GC.Collect();
  85. }
  86. return result;
  87. #endif
  88. }
  89. [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
  90. public static JObject ReadIdCard()
  91. {
  92. #if DISABLE_SSCARD
  93. JObject result = new JObject();
  94. result.Add("code", 1001);
  95. result.Add("message", "SSCard.dll已屏蔽,请使用华视读卡器:http://localhost:8321/readcard/entry?param=huashi_readcard");
  96. result.Add("device", "SSCard.dll(已屏蔽)");
  97. result.Add("suggestion", "请使用华视读卡器测试功能");
  98. return result;
  99. #else
  100. if (!initialized)
  101. {
  102. Initialize();
  103. }
  104. StringBuilder outptr = new StringBuilder(address.readIdCardBuffSize);
  105. StringBuilder signptr = new StringBuilder(address.readIdCardBuffSize);
  106. string outstr, signstr;
  107. JObject result = new JObject();
  108. try
  109. {
  110. Int32 businessResultCode = ReadSFZ(outptr, address.readIdCardBuffSize, signptr, address.readIdCardBuffSize);
  111. outstr = outptr.ToString();
  112. signstr = signptr.ToString();
  113. if (businessResultCode == 0)
  114. {
  115. result.Add("code", 200);
  116. result.Add("type", "idcard");
  117. result.Add("data", outstr);
  118. result.Add("sign", signstr);
  119. result.Add("message", "读取身份证成功。");
  120. }
  121. else
  122. {
  123. result.Add("code", 1001);
  124. result.Add("message", outstr);
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. initialized = false;
  130. result.Add("code", 1001);
  131. result.Add("message", "系统出错,请重试(如此错误频繁出现,可尝试重启读卡服务)。" + e.ToString());
  132. }
  133. finally
  134. {
  135. outptr = null;
  136. signptr = null;
  137. outstr = null;
  138. signstr = null;
  139. GC.Collect();
  140. }
  141. return result;
  142. #endif
  143. }
  144. }
  145. }