Ejemplo n.º 1
0
        public async Task <IResponseOutput> LoginAsync(AuthLoginParam param)
        {
            #region 验证码校验
            if (Configs.AppSettings.VarifyCode.Enable)
            {
                var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, param.VerifyCodeKey);
                var exists        = await BaseCache.ExistsAsync(verifyCodeKey);

                if (exists)
                {
                    var verifyCode = await BaseCache.GetAsync(verifyCodeKey);

                    if (string.IsNullOrEmpty(verifyCode))
                    {
                        return(ResponseOutput.NotOk("验证码已过期!"));
                    }
                    if (verifyCode.ToLower() != param.VerifyCode.ToLower())
                    {
                        return(ResponseOutput.NotOk("验证码输入有误!", 2));
                    }
                    await BaseCache.DelAsync(verifyCodeKey);
                }
                else
                {
                    return(ResponseOutput.NotOk("验证码已过期!", 1));
                }
            }
            #endregion

            var user = await _userRepo.FindAsync(a => a.UserName == param.UserName);

            if (string.IsNullOrWhiteSpace(user?.Id))
            {
                return(ResponseOutput.NotOk("账号输入有误!", 3));
            }

            #region 解密
            if (param.PasswordKey.NotNull())
            {
                var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, param.PasswordKey);
                var existsPasswordKey  = await BaseCache.ExistsAsync(passwordEncryptKey);

                if (existsPasswordKey)
                {
                    var secretKey = await BaseCache.GetAsync(passwordEncryptKey);

                    if (secretKey.IsNull())
                    {
                        return(ResponseOutput.NotOk("解密失败!", 1));
                    }
                    param.Password = DesEncrypt.Decrypt(param.Password, secretKey);
                    await BaseCache.DelAsync(passwordEncryptKey);
                }
                else
                {
                    return(ResponseOutput.NotOk("解密失败!", 1));
                }
            }
            #endregion

            var password = MD5Encrypt.Encrypt32(param.Password);
            if (user.Password != password)
            {
                return(ResponseOutput.NotOk("密码输入有误!", 4));
            }

            var authLoginOutput = ObjectMapper.Map <AuthLoginOutput>(user);//user.MapTo<UserEntity, AuthLoginOutput>();

            return(ResponseOutput.Ok(authLoginOutput));
        }