Esempio n. 1
0
        public JsonResult Login(LoginDto loginDto)
        {
            if (string.IsNullOrEmpty(loginDto.Account))
            {
                return(Result(false, "用户名不能为空"));
            }
            if (string.IsNullOrEmpty(loginDto.Password))
            {
                return(Result(false, "密码不能为空"));
            }
            var    output = new LoginOutput();
            SysLog sysLog = new SysLog();

            try
            {
                var user = _sysUserService.LoginValidate(loginDto.Account.Trim(), loginDto.Password.Trim());
                if (user != null)
                {
                    output.Id       = user.Id;
                    output.Account  = user.Account;
                    output.RealName = user.RealName;

                    #region 更新用户登录信息

                    user.Id = user.Id;
                    user.LoginCount++;
                    if (user.FirstVisit == null)
                    {
                        user.FirstVisit = DateTime.Now;
                    }
                    user.LastVisit = DateTime.Now;
                    _sysUserService.UpdateUserLoginInfo(user);

                    #endregion

                    sysLog.LogStatus = (int)LogStatusType.Success;
                }
                else
                {
                    sysLog.LogStatus = (int)LogStatusType.Fail;
                    return(Result(false, "用户名或密码错误"));
                }

                #region 登录日志

                sysLog.IpAddress = "";
                sysLog.LogType   = LogTypeType.Login.ToString();
                sysLog.Remark    = "";
                _sysLogService.InsertLog(sysLog);

                #endregion
            }
            catch (Exception ex)
            {
                return(Result(false, "登录异常" + ex));
            }
            return(GetToken(output));
        }