Ejemplo n.º 1
0
        public JsonResult Login(LoginInfoModel loginInfo)
        {
            loginInfo.SiteUrl = Request.Host.ToString();
            UserMaster userMaster;

            //用户密码登陆
            if (loginInfo.Ip == null)
            {
                userMaster = _userMasterService.GetModel(p => p.UserID == loginInfo.UserId);

                if (userMaster == null)
                {
                    return(Json(new ResultInfo()
                    {
                        ErrorNo = -1, ErrorMsg = ResMessage.AccountNotExist
                    }));
                }

                if (userMaster.Password != DesEncrypt.Encrypt(loginInfo.Password))
                {
                    return(Json(new ResultInfo()
                    {
                        ErrorNo = -1, ErrorMsg = ResMessage.PasswordError + "<a href='javascript:void(0);' onclick='RetrievePassword();'>" + ResIndex.RetrievePassword + "</a>"
                    }));
                }
            }
            else
            {   //IP免登陆
                if (ViewBag.FromIpFreeLogin != true)
                {
                    var dt    = _userMasterService.Login(loginInfo.Ip);
                    var bFind = false;

                    foreach (DataRow row in dt.Rows)
                    {
                        if (row["User_ID"].ToString() == loginInfo.UserId)
                        {
                            bFind = true;
                        }
                    }

                    if (!bFind)
                    {
                        return(Json(new ResultInfo()
                        {
                            ErrorNo = -1, ErrorMsg = ResMessage.AccountInvaliable
                        }));
                    }
                }

                userMaster = _userMasterService.GetCachedModel(p => p.UserID == loginInfo.UserId);
                if (userMaster == null)
                {
                    return(Json(new ResultInfo()
                    {
                        ErrorNo = -1, ErrorMsg = ResMessage.AccountNotExist
                    }));
                }
            }
            //账户是否有效
            if (userMaster.StatusFlag != 0)
            {
                return(Json(new ResultInfo()
                {
                    ErrorNo = -1, ErrorMsg = ResMessage.AccountInvaliable
                }));
            }

            //网址是否有效
            var userSite = _userSiteService.GetModel(userMaster.UserID, loginInfo.SiteUrl);

            //if (userSite == null)
            //    return Json(new ResultInfo() { ErrorNo = -1, ErrorMsg = ResMessage.SiteTips });

            //密码是否过期
            if (userMaster.PasswordExprityDate < DateTime.Now)
            {
                return(Json(new ResultInfo()
                {
                    ErrorNo = -1, ErrorMsg = ResMessage.PasswordExpired + "<a href='javascript:void(0);' onclick='ModifyPassword();'>" + ResIndex.ModifyPassword + "</a>"
                }));
            }

            //清空旧缓存
            SessionManager.Logout();
            CookieManager.RemoveVersion();

            //加入角色和用户信息
            SessionManager.AddRoleUser(_roleUserService.GetCachedModel(p => p.UserID == loginInfo.UserId));
            SessionManager.AddUserMaster(userMaster);
            var domain = _domainService.GetModel(p => p.StatusFlag == 0);

            if (domain != null)
            {
                SessionManager.AddDomain(domain);
                CookieManager.AddVersion(domain.Version.ToString());
            }

            return(Json(new ResultInfo()
            {
                ErrorNo = 0, ErrorMsg = ""
            }));
        }