Exemplo n.º 1
0
        public async Task <ResultModel <AccountEntity> > Login(LoginModel model)
        {
            var result = new ResultModel <AccountEntity>();

            var verifyCodeKey = CacheKeys.VerifyCodeKey + model.PictureId;
            var systemConfig  = (await _systemService.GetConfig()).Data;

            if (systemConfig.LoginVerifyCode)
            {
                if (model.Code.IsNull())
                {
                    return(result.Failed("请输入验证码"));
                }

                var code = await _cache.GetAsync(verifyCodeKey);

                if (model.PictureId.IsNull() || !model.Code.Equals(code))
                {
                    return(result.Failed("验证码有误"));
                }
            }

            var account = await _accountRepository.GetByUserName(model.UserName);

            if (!CheckAccount(account, out string msg))
            {
                return(result.Failed(msg));
            }

            var password = EncryptPassword(account.UserName.ToLower(), model.Password);

            if (!account.Password.Equals(password))
            {
                return(result.Failed("密码错误"));
            }

            #region ==修改登录信息==

            //是否激活
            var status = account.Status == AccountStatus.Inactive ? AccountStatus.Enabled : AccountStatus.UnKnown;
            await _accountRepository.UpdateLoginInfo(account.Id, model.IP, status);

            #endregion

            //删除验证码缓存
            await _cache.RemoveAsync(verifyCodeKey);

            return(result.Success(account));
        }