/// <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.PhoneNo, CodeType.用户注册);
            if (severCode != validateCode)
            {
                return new OperationResult(OperationResultType.ValidError, "验证码错误", 0);
            }

            if (UserInfoRepo.CheckExists(p => p.SysUser.PhoneNumber == dto.PhoneNo || p.SysUser.UserName == dto.PhoneNo))
            {
                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.PhoneNo,
                    NickName = dto.NickName,
                    PhoneNumber = dto.PhoneNo,
                    PhoneNumberConfirmed = true,

                    //密码加密
                    PasswordHash = UserManager.PasswordHasher.HashPassword(dto.Password)
                };
                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);
            }
        }
Exemple #2
0
 public async Task<IHttpActionResult> ValidateRegister(UserInfoRegistDto dto, string validateCode)
 {
     var result = await UserContract.ValidateRegister(dto, validateCode);
     return Json(result.ToApiResult());
 }