/// <summary> /// 检查UserInfo信息是否存在 /// </summary> /// <param name="predicate">检查谓语表达式</param> /// <param name="id">更新的UserInfo编号</param> /// <returns>UserInfo信息是否存在</returns> public async Task <bool> CheckUserInfoExists(Expression <Func <UserInfo, bool> > predicate, int id = 0) { return(await Task.Run(() => { return UserInfoRepo.CheckExists(predicate, id); })); }
/// <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)); } }
/// <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 = dto => { if (UserInfoRepo.CheckExists(p => p.PhoneNo == dto.PhoneNo, dto.Id)) { throw new Exception("“{0}”已被使用".FormatWith("手机号")); } }; Func <UserInfoDto, UserInfo, UserInfo> updateFunc = (dto, entity) => { if (dto.Id == 0 || updateForeignKey) { entity.SysUser = SysUserRepo.GetByKey(dto.SysUserId); } 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)); } }
/// <summary> /// 获取手机验证码 /// </summary> /// <param name="phoneNo">手机号</param> /// <param name="codeType"></param> /// <returns>业务操作结果</returns> public async Task <OperationResult> GetSmsValidateCode(string phoneNo, CodeType codeType) { if (codeType == CodeType.用户注册) { if (UserInfoRepo.CheckExists(p => p.SysUser.UserName == phoneNo)) { return(new OperationResult(OperationResultType.ValidError, "手机号已注册,不能获取注册验证码")); } } if (codeType == CodeType.找回密码) { if (!UserInfoRepo.CheckExists(p => p.SysUser.UserName == phoneNo)) { return(new OperationResult(OperationResultType.ValidError, "该用户未注册,不能获取验证码")); } } return(await SendValidateCode(phoneNo, ValidateType.手机, codeType, code => { var smsContent = "您的验证码为:" + code + ",请勿泄露。[不同]"; Sms.Send(phoneNo, 1, smsContent); })); }