Пример #1
0
        public JsonResult Login(Models.LoginModel model)
        {
            if (ModelState.IsValid)
            {
                string validateImgCode = CookieHelper.GetCookie("ValidateImgCode");
                //首先判断下验证码是否正确
                if (!string.IsNullOrEmpty(validateImgCode) && string.Equals(validateImgCode,
                                                                            model.ValidateCode, StringComparison.OrdinalIgnoreCase))
                {
                    Employee adminModel = new Employee();
                    if (new Regex("1[3|5|7|8|][0-9]{9}").IsMatch(model.UserName))
                    {//匹配手机号码
                        adminModel = EnterRepository.GetRepositoryEnter().EmployeeRepository.LoadEntities(m => m.Tel == model.UserName).FirstOrDefault();
                    }
                    else  //匹配用户名
                    {
                        adminModel = EnterRepository.GetRepositoryEnter().EmployeeRepository.LoadEntities(m => m.EmpNo == model.UserName).FirstOrDefault();
                    }
                    if (adminModel == null)
                    {
                        return(Json(new
                        {
                            state = "error",
                            message = "用户名不存在"
                        }));
                    }
                    else
                    {
                        //判断密码是否正确
                        if (adminModel.Pwd == model.Password)
                        {
                            string ip            = IpHelper.GetRealIP();
                            string address       = IpHelper.GetAdrByIp(ip);
                            string brower        = IpHelper.GetBrowerVersion();
                            string adminloginlog = string.Join("|", new string[] { DateTime.Now.ToString(), adminModel.Name, ip, address, brower });
                            log.Info("用户登录后台管理:" + adminloginlog);
                            //添加登录日志并修改上次登录信息

                            //先清除原来的cookie
                            WebCookieHelper.EmployeeLoginOut();
                            //登录成功,保存cookie
                            WebCookieHelper.SetEmployeeCookie(adminModel.ID, adminModel.Name, adminModel.EmpNo, adminModel.Tel, (model.IsRemind != null && model.IsRemind) ? 15 : 0);
                            return(Json(new
                            {
                                state = "success",
                                message = "登录成功"
                            }));
                        }
                        else
                        {
                            return(Json(new
                            {
                                state = "error",
                                message = "密码错误"
                            }));
                        }
                    }
                }
                else
                {
                    return(Json(new
                    {
                        state = "error",
                        message = "验证码错误"
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    state = "error",
                    message = "输入信息不完整"
                }));
            }
        }