UserInfoLogic.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using CZFW.Core;
  2. using CZFW.Core.Security;
  3. using CZFW.Framework.Interface;
  4. using CZFW.Framework.Interface.Base;
  5. using CZFW.Framework.Model;
  6. using CZFW.Framework.Model.Entity;
  7. using CZFW.Framework.Model.Entity.Base;
  8. using CZFW.Framework.Model.ViewModel;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace CZFW.Framework.Logic
  13. {
  14. public class UserInfoLogic : LogicBase<UserInfoEntity>, IUserInfo, IValidate<UserInfoEntity>
  15. {
  16. public UserInfoLogic()
  17. {
  18. }
  19. public ResultModel ChangePassword(string oldPassword, string newPassword)
  20. {
  21. var res = new ResultModel();
  22. if (newPassword.Length < 6 || newPassword.Length > 16)
  23. {
  24. res.Success = false;
  25. res.Message = "密码长度为6-16位";
  26. return res;
  27. }
  28. if (oldPassword == newPassword)
  29. {
  30. res.Success = false;
  31. res.Message = "新密码不能与原密码相同";
  32. return res;
  33. }
  34. var user = OperatorProvider.Provider.GetCurrent(); ;
  35. if (user == null)
  36. {
  37. throw new Exception("未登录!");
  38. }
  39. var entity = GetEntity(user.UserId);
  40. if (entity.Password != DesEncrypt.Encrypt(oldPassword, "MD5"))
  41. {
  42. res.Success = false;
  43. res.Message = "原始密码不正确";
  44. return res;
  45. }
  46. entity.Password = DesEncrypt.Encrypt(newPassword, "MD5");
  47. var tp = Commit();
  48. return tp;
  49. }
  50. public SimpleUserInfoModel GetSimpleUser(int id)
  51. {
  52. var res = GetQueryable().Select(x => new SimpleUserInfoModel()
  53. {
  54. Id = x.Id,
  55. Name = x.Name,
  56. State = x.State,
  57. UserName = x.UserName,
  58. }).SingleOrDefault(x => x.Id == id);
  59. return res;
  60. }
  61. public IList<SimpleUserInfoModel> GetSimpleUserList(
  62. out int rows,
  63. string name = null,
  64. string userName = null,
  65. string mobile = null,
  66. int pageIndex = 1,
  67. int pageSize = 10)
  68. {
  69. var queryable = GetSortedQueryable();
  70. var now = DateTime.Now;
  71. if (!string.IsNullOrWhiteSpace(name))
  72. queryable = queryable.Where(x => x.Name.Contains(name));
  73. if (!string.IsNullOrWhiteSpace(userName))
  74. queryable = queryable.Where(x => x.UserName == userName);
  75. if (!string.IsNullOrWhiteSpace(mobile))
  76. queryable = queryable.Where(x => x.Mobile.Contains(mobile));
  77. rows = queryable.Count();
  78. var res = queryable.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(
  79. x => new SimpleUserInfoModel()
  80. {
  81. Id = x.Id,
  82. Name = x.Name,
  83. SortOrder = x.SortOrder ?? 0,
  84. State = x.State,
  85. UserName = x.UserName,
  86. }).ToList();
  87. return res;
  88. }
  89. public ResultModel Login(string userName, string password)
  90. {
  91. return Login1(userName, password, out OperatorModel model);
  92. }
  93. public ResultModel Login1(string userName, string password, out OperatorModel model)
  94. {
  95. model = null;
  96. var res = new ResultModel();
  97. userName = userName.Trim();
  98. password = DesEncrypt.Encrypt(password, "MD5");
  99. var user = GetQueryable().Where(x => x.UserName == userName).SingleOrDefault();
  100. if (user.State == 2)
  101. {
  102. return new ResultModel { Success = false, Message = "您的账号已经被禁用!" };
  103. }
  104. if (user != null)
  105. {
  106. if (user.LockTime != null&&user.LockTime.Value!=0 && (user.StartTime.Value.AddMinutes(user.LockTime.Value) > DateTime.Now))
  107. {
  108. return new ResultModel { Success = false, Message = $"由于您连续三次密码输入错误,账号将被冻结{user.LockTime}分钟!" };
  109. }
  110. else if (user.LockTime != null && user.LockTime.Value != 0 && (user.StartTime.Value.AddMinutes(user.LockTime.Value) < DateTime.Now))
  111. {
  112. user.LockTime = 0;
  113. user.Error = 0;
  114. EditEntity(user);
  115. }
  116. }
  117. if (user == null)
  118. {
  119. res.Success = false;
  120. res.Message = "您输入的用户名不存在!";
  121. }
  122. else if (user.Password != password)
  123. {
  124. res.Success = false;
  125. res.Message = "您输入的密码有误,请重新输入!";
  126. ExcuteSql($"INSERT INTO user_operation_record (`Content`,`CreatedTime`,`Type`,`UserId`)VALUES('登陆失败','{Utility.GetNowString()}','密码验证出错',{user.Id})");
  127. if (user.Error == 2)
  128. {
  129. user.Error = 3;
  130. user.LockTime = ConfigHelper.GetValue<int>("LockTime");
  131. user.StartTime = DateTime.Now;
  132. EditEntity(user);
  133. }
  134. else
  135. {
  136. user.Error = user.Error + 1;
  137. EditEntity(user);
  138. // var result1 = ExcuteSql($"UPDATE `gbrs2`.`cz_user_info` SET `Error`={user.Error + 1} Where id={user.Id}");
  139. }
  140. }
  141. else
  142. {
  143. res.Success = true;
  144. res.Data = new OperatorModel()
  145. {
  146. DisplayName = user.Name,
  147. UserId = user.Id,
  148. UserName = userName,
  149. IsSystem = user.IsSysAdmin,
  150. };
  151. model = new OperatorModel()
  152. {
  153. DisplayName = user.Name,
  154. UserId = user.Id,
  155. UserName = userName,
  156. IsSystem = user.IsSysAdmin,
  157. };
  158. ExcuteSql($"UPDATE cz_user_info SET `LockTime`=0,`Error`=0 WHERE `Id`={user.Id}");
  159. ExcuteSql($"INSERT INTO user_operation_record (`Content`,`CreatedTime`,`Type`,`UserId`)VALUES('登陆成功','{Utility.GetNowString()}','正常登录',{user.Id})");
  160. OperatorProvider.Provider.AddCurrent(model);
  161. }
  162. var tp = Commit();
  163. return res;
  164. }
  165. public (bool result, string message) Validate(UserInfoEntity entity)
  166. {
  167. var queryable = entity.Id == 0 ? Queryable : Queryable.Where(x => x.Id != entity.Id);
  168. var any = queryable.Any(x => x.UserName == entity.UserName);
  169. return (!any, any ? "该用户名已存在,换个用户名试试" : null);
  170. }
  171. public UserInfoModel GetSiteAdmin(int subsiteId)
  172. {
  173. var admin = Queryable.Where(x => x.IsSysAdmin).Select(x => new UserInfoModel
  174. {
  175. Id = x.Id,
  176. Name = x.Name,
  177. SortOrder = x.SortOrder ?? 0,
  178. State = x.State,
  179. UserName = x.UserName,
  180. Mobile = x.Mobile,
  181. IsSysAdmin = x.IsSysAdmin
  182. }).SingleOrDefault();
  183. return admin;
  184. }
  185. //public IList<SimpleUserInfoModel> GetAllCustomerUsers(int entId, bool includeAdmin = false, int pageIndex = 1, int pageSize = 20)
  186. //{
  187. // var queryable = Queryable.Where(x => x.SubsiteId == entId);
  188. // if (!includeAdmin)
  189. // queryable = queryable.Where(x => !x.IsSubsiteAdmin);
  190. // var res = queryable.Select(x => new SimpleUserInfoModel { Id = x.Id, UserName = x.UserName, Name = x.Name, State = x.State, SortOrder = x.SortOrder ?? 0 }).OrderByDescending(x => x.SortOrder).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
  191. // return res;
  192. //}
  193. public ResultModel AddUser(UserInfoEntity entity)
  194. {
  195. entity.Password = DesEncrypt.Encrypt("gbjr123", "MD5");
  196. return AddEntity(entity);
  197. }
  198. public IList<SimpleUserInfoModel> GetAllSystemUsers(int pageIndex = 1, int pageSize = 20)
  199. {
  200. var res = Queryable.Where(x => x.IsSysAdmin).Select(x => new SimpleUserInfoModel
  201. {
  202. Id = x.Id,
  203. Name = x.Name,
  204. SortOrder = x.SortOrder ?? 0,
  205. State = x.State,
  206. UserName = x.UserName,
  207. }).OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  208. return res;
  209. }
  210. public ResultModel ChangePasswordByAdmin(UserNewPwdModel model)
  211. {
  212. var entity = GetEntity(model.Id);
  213. entity.Password = DesEncrypt.Encrypt(model.NewPassword);
  214. var res = EditEntity(entity);
  215. return res;
  216. }
  217. public IList<SimpleUserInfoModel> GetAllOrdinaryUsers(int pageIndex = 1, int pageSize = 20)
  218. {
  219. var res = Queryable.Where(x => !x.IsSysAdmin).Select(x => new SimpleUserInfoModel
  220. {
  221. Id = x.Id,
  222. Name = x.Name,
  223. SortOrder = x.SortOrder ?? 0,
  224. State = x.State,
  225. UserName = x.UserName,
  226. }).OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  227. return res;
  228. }
  229. }
  230. }