/// <summary> /// 获取登录用户菜单 /// </summary> /// <returns></returns> public IEnumerable <SystemMenuRoleViewModel> GetLoginUserMenu(string controllerName = "") { IEnumerable <SystemMenuRoleViewModel> menuList = null; var token = CookieHelper.GetCookieValue(TianYuConsts.SystemLoginCookieName); if (!token.IsNullOrWhiteSpace()) { menuList = CacheHelper.Get <IEnumerable <SystemMenuRoleViewModel> >(TianYuConsts.GetLoginUserMenuCacheKey(token)); if (menuList != null && !controllerName.IsNullOrWhiteSpace()) { menuList = menuList.Where(x => x.MenuUrl != null && x.MenuUrl.Contains(controllerName)).ToList(); } } return(menuList); }
public ActionResult Index(int logInDefault = 0) { //获取该用户拥有的相关模块 var cookie = Request.Cookies.Get(TianYuConsts.SystemLoginCookieName); var menuViewModel = CacheHelper.Get <IEnumerable <SystemMenuRoleViewModel> >(TianYuConsts.GetLoginUserMenuCacheKey(cookie.Value)); //读取一级菜单 var oneLevelMenu = menuViewModel.Where(x => x.Level == 1).OrderBy(x => x.MenuSort).ToList(); ViewBag.OneLevelMenu = oneLevelMenu; if (logInDefault == 0 && oneLevelMenu != null) { logInDefault = oneLevelMenu.FirstOrDefault().Id; } ViewBag.Refres = logInDefault; //读取一级以下菜单 ViewBag.OtherLevelMenuHtml = GetSubMenuHtml(menuViewModel.ToList(), logInDefault); //ViewBag.LogoutUrl = ConfigHelper.GetAppsettingValue(TianYuConsts.SystemManagerLogoutDomain + "/Home/Login"); var loginInfo = CacheHelper.Get <SystemLoginUserInfo>(TianYuConsts.GetLoginUserInfoCacheKey(cookie.Value)); return(View(loginInfo)); }
/// <summary> /// 登录后台 /// </summary> /// <param name="loginName">用户名</param> /// <param name="loginPwd">密码</param> /// <returns>登录凭据</returns> public BusinessBaseViewModel <string> Login(string loginName, string loginPwd) { var response = new BusinessBaseViewModel <string>() { Status = ResponseStatus.Fail }; if (loginName.IsNullOrWhiteSpace() || loginPwd.IsNullOrWhiteSpace()) { response.ErrorMessage = "请输入用户名或密码"; return(response); } var Staff = _staffRepostory.FirstOrDefault(t => t.LoginName == loginName && t.Status != (int)SystemStaffStatus.Del); if (Staff.IsNull()) { response.ErrorMessage = "请输入用户名不存在或密码错误"; return(response); } if (Staff.Status == (int)SystemStaffStatus.Stop) { response.ErrorMessage = "该用户已经被禁用"; return(response); } if (Staff.LoginPwd.Equals((loginPwd + Staff.MaskCode).ToMd5(), StringComparison.InvariantCultureIgnoreCase)) { //更新最近登录时间 Staff.LastLoginTime = DateTime.Now; _staffRepostory.Update(Staff, "LastLoginTime"); _staffRepostory.SaveChanges(); //生成一个登录凭据 var sessionIdString = $"admin:login:{loginName}:{Utils.NewGuid()}"; var sessionId = DESEncrypt.Encrypt(sessionIdString.ToBase64()); string sessionKey = TianYuConsts.GetSessionIdCacheKey(sessionId); if (CacheHelper.Exists(sessionKey)) { CacheHelper.Remove(sessionKey); } //将用户菜单权限缓存到cache var menuList = _systemRoleService.FindStaffMenuRole(Staff.Id); CacheHelper.Insert(TianYuConsts.GetLoginUserMenuCacheKey(sessionId), menuList, true); var buttonList = _systemRoleService.FindStaffRoleNameByStaffId(Staff.Id); CacheHelper.Insert(TianYuConsts.GetLoginUserButtonCacheKey(sessionId), buttonList, true); var loginUserInfo = new SystemLoginUserInfo { Id = Staff.Id, Eamil = Staff.Eamil, LoginName = Staff.LoginName, Mobile = Staff.Mobile, NickName = Staff.NickName, SectionId = Staff.SectionId, Status = Staff.Status, Tel = Staff.Tel }; //存储当前登录用户数据 CacheHelper.Insert(TianYuConsts.GetLoginUserInfoCacheKey(sessionId), loginUserInfo, DateTime.Now.AddHours(1)); response.BusinessData = sessionId; response.Status = ResponseStatus.Success; return(response); } else { response.ErrorMessage = "请输入用户名不存在或密码错误"; return(response); } }