UserManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using NEIntelligentControl2.Models.Messages;
  2. using NEIntelligentControl2.Models.User;
  3. using NEIntelligentControl2.Service.WebSocket;
  4. using NEIntelligentControl2.Windows;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. namespace NEIntelligentControl2.Service.User
  14. {
  15. /// <summary>
  16. /// 用户控制
  17. /// </summary>
  18. public class UserManager
  19. {
  20. /// <summary>
  21. /// 是否登录
  22. /// </summary>
  23. public bool IsLogined { get; set; }
  24. /// <summary>
  25. /// 是否临时登录
  26. /// </summary>
  27. public bool IsTempLogin { get; set; }
  28. /// <summary>
  29. /// 用户
  30. /// </summary>
  31. public UserInfo User { get; set; }
  32. private string _Url;
  33. private WEBHelper _WEBHelper;
  34. public UserManager(WEBHelper wh, UrlManager um)
  35. {
  36. _WEBHelper = wh;
  37. _Url = um.ServicePath;
  38. }
  39. /// <summary>
  40. /// 用户名密码登录
  41. /// </summary>
  42. /// <returns></returns>
  43. internal bool Login(string v1, string v2)
  44. {
  45. var v = _WEBHelper.HttpPostBody<UserInfo>($"{_Url}/user/login", new { Username = v1, Password = v2 });
  46. if (v == null) return false;
  47. User = v;
  48. IsLogined = true;
  49. _WEBHelper.Token = v.Token;
  50. CheckPassword();
  51. return true;
  52. }
  53. /// <summary>
  54. /// 指纹登录
  55. /// </summary>
  56. /// <returns></returns>
  57. internal bool Login(string v1)
  58. {
  59. string s = GetMD5(v1);
  60. var v = _WEBHelper.HttpGetJSON<UserInfo>($"{_Url}/user/token?key={s}");
  61. if (v == null) return false;
  62. User = v;
  63. IsLogined = true;
  64. _WEBHelper.Token = v.Token;
  65. return true;
  66. }
  67. private string GetMD5(string v1)
  68. {
  69. MD5 md5 = MD5.Create();
  70. byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(v1));
  71. StringBuilder sb = new StringBuilder();
  72. for (int i = 0; i < s.Length; i++)
  73. {
  74. sb.Append(s[i].ToString("X"));
  75. }
  76. return sb.ToString();
  77. }
  78. /// <summary>
  79. /// 获取所有指纹
  80. /// </summary>
  81. internal List<string> GetAllFingers()
  82. {
  83. return _WEBHelper.HttpGetJSON<List<string>>($"{_Url}/user/fingers");
  84. }
  85. /// <summary>
  86. /// 注销用户
  87. /// </summary>
  88. public void Logout()
  89. {
  90. User = null;
  91. IsLogined = false;
  92. _WEBHelper.Token = null;
  93. }
  94. /// <summary>
  95. /// 临时登录
  96. /// </summary>
  97. internal bool TempLogin()
  98. {
  99. IsTempLogin = true;
  100. UserWindow uw = App.ServiceProvider.GetService(typeof(UserWindow)) as UserWindow;
  101. bool b = uw.Show("User.PageFingerprint");
  102. IsLogined = false;
  103. return b;
  104. }
  105. /// <summary>
  106. /// 检查密码
  107. /// </summary>
  108. private void CheckPassword()
  109. {
  110. if(User == null) return;
  111. if (!User.IsPasswordSecurity)
  112. {
  113. var b = MessageWindow.ShowMessage("密码安全性较低,必须包含大写字母,小写字母,数字以及特殊字符中的3种,并且不能少于8位!是否修改密码?");
  114. if (b)
  115. {
  116. UserEdit();
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// 用户编辑
  122. /// </summary>
  123. /// <exception cref="NotImplementedException"></exception>
  124. internal void UserEdit()
  125. {
  126. UserWindow uw = App.ServiceProvider.GetService(typeof(UserWindow)) as UserWindow;
  127. bool b = uw.Show("User.PageUserEdit", this.User);
  128. }
  129. internal void UserAdd()
  130. {
  131. UserWindow uw = App.ServiceProvider.GetService(typeof(UserWindow)) as UserWindow;
  132. bool b = uw.Show("User.PageUserEdit");
  133. }
  134. /// <summary>
  135. /// 注册用户
  136. /// </summary>
  137. internal string RegisterUser(UserInfo userInfo)
  138. {
  139. bool b = CheckPassword(userInfo.Password);
  140. if (!b)
  141. {
  142. return "密码必须大于8位并且包含大写字母,小写字母,数字以及特殊字符中的任意3种!";
  143. }
  144. try
  145. {
  146. var str = _WEBHelper.HttpPostBodyString($"{_Url}/user/register", userInfo);
  147. return str;
  148. }
  149. catch (Exception e)
  150. {
  151. return "注册用户出现错误:" + e.Message;
  152. }
  153. }
  154. /// <summary>
  155. /// 更新用户
  156. /// </summary>
  157. /// <param name="userInfo">用户信息</param>
  158. /// <returns></returns>
  159. internal string UpdateUser(UserInfo userInfo)
  160. {
  161. bool b = CheckPassword(userInfo.Password);
  162. if (!b)
  163. {
  164. return "密码必须大于8位并且包含大写字母,小写字母,数字以及特殊字符中的任意3种!";
  165. }
  166. try
  167. {
  168. var str = _WEBHelper.HttpPostBodyString($"{_Url}/user/update", userInfo);
  169. return str;
  170. }
  171. catch (Exception e)
  172. {
  173. return "修改用户出现错误:" + e.Message;
  174. }
  175. }
  176. private bool CheckPassword(string psd)
  177. {
  178. if (psd.Length < 8)
  179. {
  180. return false;
  181. }
  182. string[] ps = new string[] { "(?=.*[a-z])", "(?=.*[A-Z])", "(?=.*\\d)", "(?=.*[ !\"\"#$%&'()*+,-./:;<=>?@\\[\\]\\^_`{|}~])" };
  183. int count = 0;
  184. foreach (var s in ps)
  185. {
  186. if (Regex.IsMatch(psd, s))
  187. {
  188. ++count;
  189. }
  190. }
  191. return count > 2;
  192. }
  193. }
  194. }