123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using CZFW.Core;
- using CZFW.Core.Security;
- using CZFW.Framework.Interface;
- using CZFW.Framework.Interface.Base;
- using CZFW.Framework.Model;
- using CZFW.Framework.Model.Entity;
- using CZFW.Framework.Model.Entity.Base;
- using CZFW.Framework.Model.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CZFW.Framework.Logic
- {
- public class UserInfoLogic : LogicBase<UserInfoEntity>, IUserInfo, IValidate<UserInfoEntity>
- {
- public UserInfoLogic()
- {
- }
- public ResultModel ChangePassword(string oldPassword, string newPassword)
- {
- var res = new ResultModel();
- if (newPassword.Length < 6 || newPassword.Length > 16)
- {
- res.Success = false;
- res.Message = "密码长度为6-16位";
- return res;
- }
- if (oldPassword == newPassword)
- {
- res.Success = false;
- res.Message = "新密码不能与原密码相同";
- return res;
- }
- var user = OperatorProvider.Provider.GetCurrent(); ;
- if (user == null)
- {
- throw new Exception("未登录!");
- }
- var entity = GetEntity(user.UserId);
- if (entity.Password != DesEncrypt.Encrypt(oldPassword, "MD5"))
- {
- res.Success = false;
- res.Message = "原始密码不正确";
- return res;
- }
- entity.Password = DesEncrypt.Encrypt(newPassword, "MD5");
- var tp = Commit();
- return tp;
- }
- public SimpleUserInfoModel GetSimpleUser(int id)
- {
- var res = GetQueryable().Select(x => new SimpleUserInfoModel()
- {
- Id = x.Id,
- Name = x.Name,
- State = x.State,
- UserName = x.UserName,
- }).SingleOrDefault(x => x.Id == id);
- return res;
- }
- public IList<SimpleUserInfoModel> GetSimpleUserList(
- out int rows,
- string name = null,
- string userName = null,
- string mobile = null,
- int pageIndex = 1,
- int pageSize = 10)
- {
- var queryable = GetSortedQueryable();
- var now = DateTime.Now;
- if (!string.IsNullOrWhiteSpace(name))
- queryable = queryable.Where(x => x.Name.Contains(name));
- if (!string.IsNullOrWhiteSpace(userName))
- queryable = queryable.Where(x => x.UserName == userName);
- if (!string.IsNullOrWhiteSpace(mobile))
- queryable = queryable.Where(x => x.Mobile.Contains(mobile));
- rows = queryable.Count();
- var res = queryable.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(
- x => new SimpleUserInfoModel()
- {
- Id = x.Id,
- Name = x.Name,
- SortOrder = x.SortOrder ?? 0,
- State = x.State,
- UserName = x.UserName,
- }).ToList();
- return res;
- }
- public ResultModel Login(string userName, string password)
- {
- return Login1(userName, password, out OperatorModel model);
- }
- public ResultModel Login1(string userName, string password, out OperatorModel model)
- {
- model = null;
- var res = new ResultModel();
- userName = userName.Trim();
- password = DesEncrypt.Encrypt(password, "MD5");
- var user = GetQueryable().Where(x => x.UserName == userName).SingleOrDefault();
- if (user.State == 2)
- {
- return new ResultModel { Success = false, Message = "您的账号已经被禁用!" };
- }
- if (user != null)
- {
- if (user.LockTime != null&&user.LockTime.Value!=0 && (user.StartTime.Value.AddMinutes(user.LockTime.Value) > DateTime.Now))
- {
- return new ResultModel { Success = false, Message = $"由于您连续三次密码输入错误,账号将被冻结{user.LockTime}分钟!" };
- }
- else if (user.LockTime != null && user.LockTime.Value != 0 && (user.StartTime.Value.AddMinutes(user.LockTime.Value) < DateTime.Now))
- {
- user.LockTime = 0;
- user.Error = 0;
- EditEntity(user);
- }
- }
- if (user == null)
- {
- res.Success = false;
- res.Message = "您输入的用户名不存在!";
- }
- else if (user.Password != password)
- {
- res.Success = false;
- res.Message = "您输入的密码有误,请重新输入!";
- ExcuteSql($"INSERT INTO user_operation_record (`Content`,`CreatedTime`,`Type`,`UserId`)VALUES('登陆失败','{Utility.GetNowString()}','密码验证出错',{user.Id})");
- if (user.Error == 2)
- {
- user.Error = 3;
- user.LockTime = ConfigHelper.GetValue<int>("LockTime");
- user.StartTime = DateTime.Now;
- EditEntity(user);
- }
- else
- {
- user.Error = user.Error + 1;
- EditEntity(user);
- // var result1 = ExcuteSql($"UPDATE `gbrs2`.`cz_user_info` SET `Error`={user.Error + 1} Where id={user.Id}");
- }
- }
- else
- {
- res.Success = true;
- res.Data = new OperatorModel()
- {
- DisplayName = user.Name,
- UserId = user.Id,
- UserName = userName,
- IsSystem = user.IsSysAdmin,
- };
- model = new OperatorModel()
- {
- DisplayName = user.Name,
- UserId = user.Id,
- UserName = userName,
- IsSystem = user.IsSysAdmin,
- };
- ExcuteSql($"UPDATE cz_user_info SET `LockTime`=0,`Error`=0 WHERE `Id`={user.Id}");
- ExcuteSql($"INSERT INTO user_operation_record (`Content`,`CreatedTime`,`Type`,`UserId`)VALUES('登陆成功','{Utility.GetNowString()}','正常登录',{user.Id})");
- OperatorProvider.Provider.AddCurrent(model);
- }
- var tp = Commit();
- return res;
- }
-
- public (bool result, string message) Validate(UserInfoEntity entity)
- {
- var queryable = entity.Id == 0 ? Queryable : Queryable.Where(x => x.Id != entity.Id);
- var any = queryable.Any(x => x.UserName == entity.UserName);
- return (!any, any ? "该用户名已存在,换个用户名试试" : null);
- }
- public UserInfoModel GetSiteAdmin(int subsiteId)
- {
- var admin = Queryable.Where(x => x.IsSysAdmin).Select(x => new UserInfoModel
- {
- Id = x.Id,
- Name = x.Name,
- SortOrder = x.SortOrder ?? 0,
- State = x.State,
- UserName = x.UserName,
- Mobile = x.Mobile,
- IsSysAdmin = x.IsSysAdmin
- }).SingleOrDefault();
- return admin;
- }
- //public IList<SimpleUserInfoModel> GetAllCustomerUsers(int entId, bool includeAdmin = false, int pageIndex = 1, int pageSize = 20)
- //{
- // var queryable = Queryable.Where(x => x.SubsiteId == entId);
- // if (!includeAdmin)
- // queryable = queryable.Where(x => !x.IsSubsiteAdmin);
- // 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();
- // return res;
- //}
- public ResultModel AddUser(UserInfoEntity entity)
- {
- entity.Password = DesEncrypt.Encrypt("gbjr123", "MD5");
- return AddEntity(entity);
- }
- public IList<SimpleUserInfoModel> GetAllSystemUsers(int pageIndex = 1, int pageSize = 20)
- {
- var res = Queryable.Where(x => x.IsSysAdmin).Select(x => new SimpleUserInfoModel
- {
- Id = x.Id,
- Name = x.Name,
- SortOrder = x.SortOrder ?? 0,
- State = x.State,
- UserName = x.UserName,
- }).OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
- return res;
- }
- public ResultModel ChangePasswordByAdmin(UserNewPwdModel model)
- {
- var entity = GetEntity(model.Id);
- entity.Password = DesEncrypt.Encrypt(model.NewPassword);
- var res = EditEntity(entity);
- return res;
- }
- public IList<SimpleUserInfoModel> GetAllOrdinaryUsers(int pageIndex = 1, int pageSize = 20)
- {
- var res = Queryable.Where(x => !x.IsSysAdmin).Select(x => new SimpleUserInfoModel
- {
- Id = x.Id,
- Name = x.Name,
- SortOrder = x.SortOrder ?? 0,
- State = x.State,
- UserName = x.UserName,
- }).OrderByDescending(x => x.SortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
- return res;
- }
- }
- }
|