Esempio n. 1
0
        public async Task <ResultModel <LoginResultModel> > Login(LoginModel model, int refreshTokenExpiredTime)
        {
            var result = new ResultModel <LoginResultModel>();

            //检测验证码
            if (model.VerifyCode != null && !await CheckVerifyCode(result, model.VerifyCode))
            {
                return(result);
            }

            //检测用户
            var user = await _userRepository.FirstAsync(m => m.UserCode == model.UserCode && m.IsDel == false);

            var checkUserResult = CheckUser(user);

            if (!checkUserResult.Successful)
            {
                return(result.Failed(checkUserResult.Msg));
            }

            //检测密码
            if (!CheckPassword(result, model, user))
            {
                return(result);
            }

            //更新登录信息
            var loginInfo = await UpdateLoginInfo(user, refreshTokenExpiredTime);

            if (loginInfo == null)
            {
                return(result.Failed("更新登录信息失败"));
            }

            //获取用户角色
            var roles = await _roleUserRepository.QueryByUserId(user.Id);

            //删除验证码缓存
            if (model.VerifyCode != null)
            {
                await _cacheHandler.RemoveAsync(string.Format(CacheKeys.VerifyCodeKey, model.VerifyCode.Id));
            }

            //清除账户的认证信息缓存
            await _cacheHandler.RemoveAsync(string.Format(CacheKeys.UserAuthInfo, user.Id));

            return(result.Success(new LoginResultModel
            {
                User = user,
                AuthInfo = loginInfo,
                RoleIds = string.Join(",", roles.Select(m => m.Id).ToList()),
                RoleCodes = string.Join(",", roles.Select(m => m.RoleCode).ToList()),
                RoleNames = string.Join(",", roles.Select(m => m.RoleName).ToList())
            }));
        }