/// <summary>
        /// 保存UserInfo信息(新增/更新)
        /// </summary>
        /// <param name="updateForeignKey">更新时是否更新外键信息</param>
        /// <param name="dtos">要保存的UserInfoDto信息</param>
        /// <returns>业务操作集合</returns>
        public async Task <OperationResult> SaveUserInfos(bool updateForeignKey = false, params UserInfoDto[] dtos)
        {
            try
            {
                dtos.CheckNotNull("dtos");
                var addDtos    = dtos.Where(p => p.Id == 0).ToArray();
                var updateDtos = dtos.Where(p => p.Id != 0).ToArray();

                UserInfoRepo.UnitOfWork.TransactionEnabled = true;

                Action <UserInfoDto> checkAction = null;
                Func <UserInfoDto, UserInfo, UserInfo> updateFunc = (dto, entity) =>
                {
                    if (dto.Id == 0 || updateForeignKey)
                    {
                        entity.Region    = RegionRepo.GetByKey(dto.RegionId);
                        entity.SysUser   = SysUserRepo.GetByKey(dto.SysUserId);
                        entity.Position  = PositionRepo.GetByKey(dto.PositionId);
                        entity.Education = EducationRepo.GetByKey(dto.EducationId);
                    }
                    return(entity);
                };
                if (addDtos.Length > 0)
                {
                    UserInfoRepo.Insert(addDtos, checkAction, updateFunc);
                }
                if (updateDtos.Length > 0)
                {
                    UserInfoRepo.Update(updateDtos, checkAction, updateFunc);
                }
                await UserInfoRepo.UnitOfWork.SaveChangesAsync();

                return(new OperationResult(OperationResultType.Success, "保存成功"));
            }
            catch (Exception e)
            {
                return(new OperationResult(OperationResultType.Error, e.Message));
            }
        }
Beispiel #2
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(BodeResult.ValidError("验证码错误"));
            }
            if (SysUserRepo.CheckExists(p => p.UserName == dto.UserName))
            {
                return(BodeResult.ValidError("账号已被使用"));
            }
            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;
                userInfo.Region    = RegionRepo.GetByKey(dto.RegionId);
                userInfo.Education = EducationRepo.GetByKey(dto.EducationId);
                userInfo.Position  = PositionRepo.GetByKey(dto.PositionId);
                await UserInfoRepo.InsertAsync(userInfo);

                await UserInfoRepo.UnitOfWork.SaveChangesAsync();

                return(BodeResult.Success("注册成功", userInfo.Id));
            }
            catch (Exception e)
            {
                return(BodeResult.NoChanged("注册失败"));
            }
        }