Beispiel #1
0
        /// <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);
            }
        }
        /// <summary>
        /// 获取登录用户按钮操作权限
        /// </summary>
        /// <returns></returns>
        public IEnumerable <SystemButtonRoleViewModel> GetLoginAccountButtonRole(int menuId)
        {
            IEnumerable <SystemButtonRoleViewModel> buttonList = null;
            var token = CookieHelper.GetCookieValue(TianYuConsts.SystemLoginCookieName);

            if (!token.IsNullOrWhiteSpace())
            {
                buttonList = CacheHelper.Get <IEnumerable <SystemButtonRoleViewModel> >(TianYuConsts.GetLoginUserButtonCacheKey(token));

                if (menuId != 0)
                {
                    buttonList = buttonList.Where(x => x.MenuId == menuId).ToList();
                }
            }
            return(buttonList);
        }