using Newtonsoft.Json; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using YLErp.Helpers; namespace BaseOUDAL { public enum UserCheckResult { 验证通过, 用户密码错误, 用户不存在, 用户登录锁定, 绑定IP错误 } public enum UserState { Enabled, Disabled } public class SystemUser : BaseSystemUser, IDataTrace { [Display(Name = "登录Token")] public string UserToken { get; set; } [DisplayName("绑定IP")] public string BondIP { get; set; } [DisplayName("密码")] [MaxLength(100)] public string Password { get; set; } [DisplayName("角色")] [NotMapped] public string RoleTypeName { get; set; } public string Guid { get; set; } public SystemUser() { Name = ""; Email = ""; } public bool CheckPassword(string password) { var pwd = DataHelper.EncryptPassword(LoginName.ToLower(), password); return Password == pwd; } /// /// 修改密码或用户名时候调用。调用时password应该是加密前password /// public void Save(ErpBaseContext db, SystemUser newValue) { if (Id == 0) { db.SystemUsers.Add(this); } newValue.LoginName = newValue.LoginName.Trim(); if (LoginName != newValue.LoginName || string.IsNullOrEmpty(newValue.Password) == false) { LoginName = newValue.LoginName; Password = DataHelper.EncryptPassword(LoginName.ToLower(), newValue.Password); } Tel = newValue.Tel; Mobile = newValue.Mobile; Name = newValue.Name; Email = newValue.Email; QQ = newValue.QQ; State = newValue.State; BondIP = newValue.BondIP; Guid = newValue.Guid; RoleType = newValue.RoleType; UserGroup = newValue.UserGroup; AccountPost = newValue.AccountPost; ParentId = newValue.ParentId; VarietyIds = newValue.VarietyIds; OaAccount=newValue.OaAccount; db.SaveChanges(); //刷新缓存 UserBLL.UpdateUsers(); } static public UserCheckResult CheckUser(string name, string password, out SystemUser user) { using (var db = new ErpBaseContext()) { user = db.SystemUsers.FirstOrDefault(o => o.State == (int)UserState.Enabled && (name == o.LoginName)); if (user == null) { return UserCheckResult.用户密码错误; } if (user.CheckPassword(password)) { return UserCheckResult.验证通过; } user = null; return UserCheckResult.用户密码错误; } } public string GetDataTraceKeyInfo() { return "系统用户:" + Name; } } [NotMapped] public class SystemUserDto : SystemUser { } public class SystemUserReq : BaseSearchReq { /// /// 用户Id(逗号分隔) /// public string Ids { get; set; } /// /// 用户Id /// public List IdList { get { if (string.IsNullOrWhiteSpace(Ids)) { return new List(); } try { return Ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => int.Parse(d)) .ToList(); } catch (Exception e) { LogFactory.GetLogger().Error("[IdList]Ids参数有误", e); return new List(); } } } public int? State { get; set; } /// /// 部门Id(逗号分隔) /// public string DepartmentIds { get; set; } public List DepartmentIdList { get { if (string.IsNullOrWhiteSpace(DepartmentIds)) { return new List(); } try { return DepartmentIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => int.Parse(d)) .ToList(); } catch (Exception e) { LogFactory.GetLogger().Error("[DepartmentIdList]Departments参数有误", e); return new List(); } } } public int? RoleId { get; set; } /// /// 角色id(逗号分隔) /// public string RoleIds { get; set; } public List RoleIdList { get { if (string.IsNullOrWhiteSpace(RoleIds)) { return new List(); } try { return RoleIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => int.Parse(d)) .ToList(); } catch (Exception e) { LogFactory.GetLogger().Error("RoleIds参数有误", e); return new List(); } } } public string UserGroup { get; set; } } public class SystemUserVarietyReq : BaseSearchReq { public int UserId { get; set; } public string UserName { get; set; } public List VarietyIds { get; set; } } public class SystemUserVariety { public int UserId { get; set; } public string VarietyId { get; set; } public string UserName { get; set; } public string VarietyName { get; set; } } //定义交换数据类型 public class SysUserChangeInfo { [JsonProperty("SysUserId")] public int Id { get; set; } [JsonProperty("Status")] public int? Status { get; set; } [JsonProperty("ForceLoginOut")] public bool? ForceLoginOut { get; set; } } }