Пример #1
0
        public async Task <LoginResultDto> Login(LoginDto dto)
        {
            CheckParam.NotNull(dto, nameof(LoginDto));

            var user = await UserRepository.GetFirstAsync(u => u.Phone == dto.Username);

            if (user == null)
            {
                return(new LoginResultDto {
                    ErrCode = -1
                });
            }
            if (user.IsLock)
            {
                return(new LoginResultDto {
                    ErrCode = 1
                });
            }

            if (user.AuditState != UserAuditType.Success)
            {
                return(new LoginResultDto {
                    ErrCode = (int)user.AuditState
                });
            }

            if (EncryptProvider.GetMd5Password(dto.Password, user.PasswordSalt).Equals(user.Password))
            {
                var roleIds = UserRoleRepository.Query.Where(u => u.UserId == user.Id).Select(u => u.RoleId).ToList();
                var roles   = RoleRepository.Query.Where(u => roleIds.Contains(u.Id)).Select(u => u.Name).ToArray();

                user.RefreshToken  = new Random().NextLetterAndNumberString(32);
                user.LastLoginTime = DateTime.Now;
                await UserRepository.UpdateAsync(user);

                return(new LoginResultDto
                {
                    Id = user.Id,
                    RefreshToken = user.RefreshToken,
                    Avatar = user.HeadImg,
                    Name = user.NickName,
                    Roles = roles,
                    OrganizationId = user.OrganizationId,
                    Phone = user.Phone
                });
            }
            return(new LoginResultDto {
                ErrCode = -2
            });
        }
Пример #2
0
        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="dto">注册信息</param>
        /// <returns>业务操作结果</returns>
        public async Task RegisterWithOrganization(RegisterWithOrganizationDto dto, bool isLock)
        {
            CheckParam.NotNull(dto, nameof(RegisterWithOrganizationDto));

            if (await UserRepository.CheckExistsAsync(u => u.Phone == dto.Phone))
            {
                throw new BadRequestException(OperationResultType.IsExist, "电话号码");
            }

            if (await CompanyRepository.CheckExistsAsync(u => u.Name == dto.OrganizationName))
            {
                throw new BadRequestException(OperationResultType.IsExist, "公司名称");
            }

            UserOrganization company = new UserOrganization
            {
                Name     = dto.OrganizationName,
                Address  = dto.Address,
                District = dto.District,
                IsLock   = isLock
            };

            await CompanyRepository.InsertAsync(company);

            User user = new User
            {
                Phone          = dto.Phone,
                NickName       = dto.NickName,
                UserName       = dto.Phone,
                OrganizationId = company.Id,
                IsLock         = isLock,
                AuditState     = dto.AuditType
            };

            user.Password = EncryptProvider.GetMd5Password(dto.Password, user.PasswordSalt);

            var count = await UserRepository.InsertAsync(user);
        }