示例#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="thirdKey">三方key</param>
        /// <param name="thirdProvider">微博?QQ?微信?</param>
        /// <param name="loginDevice">登录设备 IOS?Android?</param>
        /// <param name="clientVersion">客户端版本</param>
        /// <param name="registKey">极光推送Key</param>
        /// <returns></returns>
        public async Task <OperationResult> LoginByThird(string thirdKey, ThirdProvider thirdProvider, LoginDevice loginDevice, string clientVersion, string registKey = "")
        {
            var thirdUser = SysUserLoginRepo.Entities.SingleOrDefault(m => m.ProviderKey == thirdKey && m.ThridProvider == thirdProvider);

            if (thirdUser == null)
            {
                var     userName = "******" + DateTime.Now.ToString("yyyyMMddhhffff");
                SysUser sUser    = new SysUser()
                {
                    UserName = userName,
                    NickName = userName,
                    UserType = UserType.App用户,
                };
                UserInfoRepo.UnitOfWork.TransactionEnabled = true;
                await UserManager.CreateAsync(sUser);

                var userInfo = new UserInfo()
                {
                    SysUser   = sUser,
                    IsDeleted = false,
                    Sex       = Sex.限,
                };
                await UserInfoRepo.InsertAsync(userInfo);

                thirdUser = new SysUserLogin()
                {
                    ProviderKey   = thirdKey,
                    ThridProvider = thirdProvider,
                    User          = sUser,
                };
                await SysUserLoginRepo.InsertAsync(thirdUser);

                await UserInfoRepo.UnitOfWork.SaveChangesAsync();
            }

            var theUser = UserInfoRepo.Entities.SingleOrDefault(m => m.SysUser.Id == thirdUser.User.Id);

            if (theUser.RegistKey != registKey)
            {
                theUser.RegistKey = registKey;
                await UserInfoRepo.UpdateAsync(theUser);
            }

            //变更登录信息
            await ResetToken(theUser, loginDevice, clientVersion);

            var loginInfo = new UserTokenDto()
            {
                Id       = theUser.Id,
                NickName = theUser.SysUser.NickName,
                HeadPic  = theUser.HeadPic,
                Sex      = theUser.Sex,
                Token    = theUser.Token
            };

            return(CdkxResult.Success(loginInfo));
        }