Exemple #1
0
        /// <summary>
        /// 验证用户注册
        /// </summary>
        /// <param name="dto">用户注册信息</param>
        /// <param name="validateCode">验证码</param>
        /// <returns>业务操作结果</returns>

        public async Task <OperationResult> ValidateRegister(UserInfoRegistDto dto, string validateCode)
        {
            dto.CheckNotNull("dto");
            validateCode.CheckNotNullOrEmpty("validateCode");
            //验证码
            var severCode = GetValidateCode(dto.UserName, CodeType.用户注册);

            if (severCode == null || severCode.Code != validateCode)
            {
                return(new OperationResult(OperationResultType.ValidError, "验证码错误", 0));
            }
            if (SysUserRepo.CheckExists(p => p.UserName == dto.UserName))
            {
                return(new OperationResult(OperationResultType.ValidError, "账号已被使用", 0));
            }

            try
            {
                UserInfoRepo.UnitOfWork.TransactionEnabled = true;
                //验证密码格式
                IdentityResult result = await UserManager.PasswordValidator.ValidateAsync(dto.Password);

                if (!result.Succeeded)
                {
                    return(result.ToOperationResult());
                }

                SysUser sUser = new SysUser()
                {
                    UserName     = dto.UserName,
                    NickName     = dto.NickName,
                    PasswordHash = UserManager.PasswordHasher.HashPassword(dto.Password),//密码加密
                    UserType     = UserType.App用户
                };
                if (severCode.ValidateType == ValidateType.手机)
                {
                    sUser.PhoneNumber          = dto.UserName;
                    sUser.PhoneNumberConfirmed = true;
                }
                else
                {
                    sUser.Email          = dto.UserName;
                    sUser.EmailConfirmed = true;
                }
                await UserManager.CreateAsync(sUser);

                var userInfo = Mapper.Map <UserInfo>(dto);
                userInfo.SysUser = sUser;
                await UserInfoRepo.InsertAsync(userInfo);

                await UserInfoRepo.UnitOfWork.SaveChangesAsync();

                return(new OperationResult(OperationResultType.Success, "注册成功", userInfo.Id));
            }
            catch
            {
                return(new OperationResult(OperationResultType.NoChanged, "注册失败", 0));
            }
        }
        /// <summary>
        /// 更改用户名
        /// </summary>
        /// <param name="userName">原用户名</param>
        /// <param name="newUserName">新用户名</param>
        /// <param name="password">登录密码</param>
        /// <param name="validateCode">验证码</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> ChangeUserName(string userName, string newUserName, string password, string validateCode)
        {
            userName.CheckNotNullOrEmpty("userName");
            newUserName.CheckNotNullOrEmpty("newUserName");
            validateCode.CheckNotNullOrEmpty("validateCode");
            //验证码
            var severCode = GetValidateCode(newUserName, CodeType.更换手机);

            if (severCode == null || severCode.Code != validateCode)
            {
                return(await Task.FromResult(new OperationResult(OperationResultType.ValidError, "验证码错误")));
            }

            var sUser = await UserManager.FindByNameAsync(userName);

            if (sUser == null)
            {
                return(new OperationResult(OperationResultType.NoChanged, "用户不存在"));
            }

            if (!await UserManager.CheckPasswordAsync(sUser, password))
            {
                return(new OperationResult(OperationResultType.ValidError, "登录密码错误", null));
            }

            if (SysUserRepo.CheckExists(p => p.UserName == newUserName, sUser.Id))
            {
                return(new OperationResult(OperationResultType.NoChanged, "该用户名已存在"));
            }

            sUser.UserName = userName;
            if (severCode.ValidateType == ValidateType.手机)
            {
                sUser.PhoneNumber          = newUserName;
                sUser.UserName             = newUserName;
                sUser.PhoneNumberConfirmed = true;
            }
            else
            {
                sUser.Email          = newUserName;
                sUser.EmailConfirmed = true;
            }
            await SysUserRepo.UpdateAsync(sUser);

            return(new OperationResult(OperationResultType.Success, "更改成功"));
        }
        /// <summary>
        /// 验证注册码是否正确
        /// </summary>
        /// <param name="phoneNo"></param>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public async Task <OperationResult> ValidateCode(string phoneNo, CodeType codeType, string validateCode)
        {
            var severCode = GetValidateCode(phoneNo, codeType);

            if (severCode == null || severCode.Code != validateCode)
            {
                return(new OperationResult(OperationResultType.ValidError, "验证码错误", 0));
            }
            if (SysUserRepo.CheckExists(p => p.UserName == phoneNo))
            {
                if (codeType == CodeType.用户注册)
                {
                    return(new OperationResult(OperationResultType.ValidError, "账号已被使用", 0));
                }
            }
            else
            {
                if (codeType == CodeType.更换手机 || codeType == CodeType.找回密码 || codeType == CodeType.临时密码)
                {
                    return(CdkxResult.ValidError("帐号不存在."));
                }
            }
            return(CdkxResult.Success());
        }
Exemple #4
0
        /// <summary>
        /// 更改手机号
        /// </summary>
        /// <param name="phoneNo">原手机号</param>
        /// <param name="newPhoneNo">新手机号</param>
        /// <param name="password">登录密码</param>
        /// <param name="validateCode">验证码</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> ChangePhoneNo(string phoneNo, string newPhoneNo, string password, string validateCode)
        {
            phoneNo.CheckNotNullOrEmpty("phoneNo");
            newPhoneNo.CheckNotNullOrEmpty("newPhoneNo");
            validateCode.CheckNotNullOrEmpty("validateCode");
            //验证码
            var severCode = GetValidateCode(newPhoneNo, CodeType.更换手机);

            if (severCode != validateCode)
            {
                return(await Task.FromResult(new OperationResult(OperationResultType.ValidError, "验证码错误")));
            }

            var sUser = await UserManager.FindByNameAsync(phoneNo);

            if (sUser == null)
            {
                return(new OperationResult(OperationResultType.NoChanged, "用户不存在"));
            }

            if (!await UserManager.CheckPasswordAsync(sUser, password))
            {
                return(new OperationResult(OperationResultType.ValidError, "登录密码错误", null));
            }

            if (SysUserRepo.CheckExists(p => p.UserName == newPhoneNo || p.PhoneNumber == newPhoneNo, sUser.Id))
            {
                return(new OperationResult(OperationResultType.NoChanged, "该手机号已注册"));
            }

            sUser.UserName    = newPhoneNo;
            sUser.PhoneNumber = newPhoneNo;
            await SysUserRepo.UpdateAsync(sUser);

            return(new OperationResult(OperationResultType.Success, "手机号更改成功"));
        }