using Cowain.Preheat.Common.Core; using Cowain.Preheat.Common.Enums; using Cowain.Preheat.Model; using Cowain.Preheat.Model.Entity; using Prism.Ioc; using System; using System.Collections.Generic; using System.Data; using System.Linq; using Unity; namespace Cowain.Preheat.BLL { public class UserService : ServiceBase { public UserService(IUnityContainer unityContainer) : base(unityContainer) { } /// /// 登录 /// /// 用户名 /// 密码 /// 登录成功 public bool Login(string name, string pwd) { using (var Context = new PreheatEntities()) { TUserManage ulist = Context.Set().Where(item => item.UserId == name && item.Password == pwd).FirstOrDefault(); if (ulist != null) { var ge = _unityContainer.Resolve(); ge.CurrentUser.Copy(ulist); //重新开辟了内存。 //获取菜单,后期需要修改,根据权限加载,现在是加载所有菜单 var ml = Context.Set().Where(item => item.State == true).OrderBy(x => x.Id).ToList(); if (ml != null && ml.Count > 0) { ge.CurrentUser.Menus.Clear(); ml.ForEach(p => ge.CurrentUser.Menus.Add(p)); } MemoryDataProvider.CurrentUser = name; _unityContainer.Resolve().AddLog(name, E_LogType.Operate.ToString()); return true; } else return false; } } public int ModifyPassword(int id, string newPassword) { using (var Context = new PreheatEntities()) { var ulist = Context.Set().Where(item => item.Id == id).ToList(); if (ulist.Count == 1) { ulist[0].Password = newPassword; return Context.SaveChanges(); } else return 0; } } public List GetAllUsers() { using (var Context = new PreheatEntities()) { return Context.Set().ToList(); } } public List QueryUser(string userEnter) { using (var Context = new PreheatEntities()) { List user = Context.Set().Where(x => x.UserId == userEnter).ToList(); return user; } } public string GetCurrentUserAuthority() { using (var Context = new PreheatEntities()) { if (!string.IsNullOrWhiteSpace(MemoryDataProvider.CurrentUser)) { TUserManage user = Context.Set().Where(x => x.UserId == MemoryDataProvider.CurrentUser).FirstOrDefault(); return Context.Set().Where(a => a.RoleId == user.RoleId).ToList()[0].AccessNode; } return null; } } public bool IsHasAuthority(string target) { string auth = GetCurrentUserAuthority(); if (!string.IsNullOrEmpty(auth)) { if (auth.Contains(target)) { return true; } } return false; } public List GetAllRole() { using (var Context = new PreheatEntities()) { return Context.Set().ToList(); } } public int GetCurrentRoleId() { if (!string.IsNullOrWhiteSpace(MemoryDataProvider.CurrentUser)) { using (var Context = new PreheatEntities()) { TUserManage user = Context.Set().Where(x => x.UserId == MemoryDataProvider.CurrentUser).FirstOrDefault(); return user.RoleId; } } return (int)ERole.Operater; } public bool CheckPermission(ERole permission) { // 获取当前登录用户的权限级别 int userPermissionLevel = GetCurrentRoleId(); // 根据权限级别判断是否具有指定权限 if (userPermissionLevel == (int)ERole.Admin) // 管理员 { return true; } else if (userPermissionLevel == (int)ERole.Mantainer && (permission == ERole.Operater || permission == ERole.Mantainer)) { return true; } else if (userPermissionLevel == (int)ERole.Operater && permission == ERole.Operater) { return true; } return false; } public List GetAllMenuInfo() { using (var Context = new PreheatEntities()) { return Context.Set().ToList(); } } public string GetAuthority(string role) { using (var Context = new PreheatEntities()) { return Context.Set().Where(x => x.RoleName == role).FirstOrDefault().AccessNode; } } public bool ValidPassword(string name, string pwd) //有效密码 { using (var Context = new PreheatEntities()) { if (null == Context.Set().Where(item => item.UserId == name && item.Password == pwd).FirstOrDefault()) { return false; } else { return true; } } } public int ModifyPassword(string newPassword) { using (var Context = new PreheatEntities()) { var userInfo = Context.Set().Where(item => item.UserId == MemoryDataProvider.CurrentUser).FirstOrDefault(); if (null == userInfo) { return 0; } userInfo.Password = newPassword; return Context.SaveChanges(); } } } }