UserSvc.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using EntityDataSet;
  8. using libzkfpcsharp;
  9. namespace IntelligentControlForsx.Service.FingerSvc
  10. {
  11. public class UserSvc
  12. {
  13. /// <summary>
  14. /// 存储所有用户信息,key为用户指纹模板,value为用户实体
  15. /// </summary>
  16. public static Dictionary<byte[], user> userDic = new Dictionary<byte[], user>();
  17. private static UserSvc svc = new UserSvc();
  18. private UserSvc()
  19. {
  20. }
  21. public static UserSvc GetUserSvc()
  22. {
  23. return svc;
  24. }
  25. /// <summary>
  26. /// 管理员登录验证
  27. /// </summary>
  28. /// <param name="mDBHandle">验证句柄</param>
  29. /// <param name="fingerTemp">输入的指纹</param>
  30. /// <returns></returns>
  31. public bool AdminUserCheck(IntPtr mDBHandle, byte[] fingerTemp)
  32. {
  33. bool IsCheckSuccess = false;
  34. foreach (KeyValuePair<byte[], user> kv in userDic)
  35. {
  36. int checkScore = zkfp2.DBMatch(mDBHandle, fingerTemp, kv.Key);
  37. if (checkScore > 0)
  38. {
  39. if (kv.Value.user_name == "admin")
  40. {
  41. IsCheckSuccess = true;
  42. }
  43. }
  44. }
  45. return IsCheckSuccess;
  46. }
  47. /// <summary>
  48. /// 登录验证
  49. /// </summary>
  50. /// <param name="mDBHandle">句柄</param>
  51. /// <param name="fingerTemp">输入指纹</param>
  52. /// <returns>符合输入指纹的用户</returns>
  53. public user LoginUserCheck(IntPtr mDBHandle, byte[] fingerTemp)
  54. {
  55. user loginUser = null;
  56. foreach (KeyValuePair<byte[], user> kv in userDic)
  57. {
  58. int checkScore = zkfp2.DBMatch(mDBHandle, fingerTemp, kv.Key);
  59. if (checkScore > 0)
  60. {
  61. loginUser = kv.Value;
  62. break;
  63. }
  64. }
  65. return loginUser;
  66. }
  67. /// <summary>
  68. /// 用户初始化(将关系库中所有的用户添加到缓存)
  69. /// </summary>
  70. public void InitUser()
  71. {
  72. Dictionary<byte[], user> dic = new Dictionary<byte[], user>();
  73. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  74. {
  75. IList<user> list = ctx.user.Select(s => s).ToList();
  76. for (int i = 0; i < list.Count; i++)
  77. {
  78. if (list[i].finger1_template != null)
  79. dic.Add(list[i].finger1_template, list[i]);
  80. if (list[i].finger2_template != null)
  81. dic.Add(list[i].finger2_template, list[i]);
  82. if (list[i].finger3_template != null)
  83. dic.Add(list[i].finger3_template, list[i]);
  84. if (list[i].finger4_template != null)
  85. dic.Add(list[i].finger4_template, list[i]);
  86. if (list[i].finger5_template != null)
  87. dic.Add(list[i].finger5_template, list[i]);
  88. if (list[i].finger6_template != null)
  89. dic.Add(list[i].finger6_template, list[i]);
  90. if (list[i].finger7_template != null)
  91. dic.Add(list[i].finger7_template, list[i]);
  92. if (list[i].finger8_template != null)
  93. dic.Add(list[i].finger8_template, list[i]);
  94. if (list[i].finger9_template != null)
  95. dic.Add(list[i].finger9_template, list[i]);
  96. if (list[i].finger10_template != null)
  97. dic.Add(list[i].finger10_template, list[i]);
  98. }
  99. userDic = dic;
  100. }
  101. }
  102. /// <summary>
  103. /// 注册用户
  104. /// </summary>
  105. /// <param name="user"></param>
  106. /// <returns></returns>
  107. public int RegisterUser(user user)
  108. {
  109. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  110. {
  111. ctx.Entry(user).State = EntityState.Added;
  112. int count = ctx.SaveChanges();
  113. return count;
  114. }
  115. }
  116. }
  117. }