Exemplo n.º 1
0
        public async Task <AjaxResult> Login(LoginDto dto)
        {
            Check.NotNull(dto, nameof(dto));

            if (!ModelState.IsValid)
            {
                return(new AjaxResult("提交信息验证失败", AjaxResultType.Error));
            }
            //todo: 校验验证码

            dto.Ip        = HttpContext.GetClientIp();
            dto.UserAgent = Request.Headers["User-Agent"].FirstOrDefault();

            OperationResult <User> result = await _identityContract.Login(dto);

            IUnitOfWorkManager unitOfWorkManager = HttpContext.RequestServices.GetRequiredService <IUnitOfWorkManager>();

#if NET5_0
            await unitOfWorkManager.CommitAsync();
#else
            unitOfWorkManager.Commit();
#endif

            if (!result.Succeeded)
            {
                return(result.ToAjaxResult());
            }

            User user = result.Data;
            await _signInManager.SignInAsync(user, dto.Remember);

            return(new AjaxResult("登录成功"));
        }
        public async Task <Unit> Handle(CancelOrderCommand request, CancellationToken cancellationToken)
        {
            var order = await _orderRepository.GetAsync(request.OrderId);

            order.SetCancelledStatus();
            await _unitOfWorkManager.CommitAsync();

            return(Unit.Value);
        }
Exemplo n.º 3
0
        /// <summary>执行拦截业务,并根据返回结果决定提交事务还是回滚</summary>
        /// <param name="context">切面上下文</param>
        /// <param name="next">被拦截并包装的操作</param>
        public override async Task Invoke(AspectContext context, AspectDelegate next)
        {
            IServiceProvider   provider          = context.ServiceProvider;
            IUnitOfWorkManager unitOfWorkManager = provider.GetService <IUnitOfWorkManager>();

            try
            {
                await next(context);

                if (!RequiredCheckReturnValue)
                {
#if NET5_0
                    await unitOfWorkManager.CommitAsync();
#else
                    unitOfWorkManager.Commit();
#endif
                    return;
                }
                Type returnType = context.ProxyMethod.ReturnType;
                ITransactionDecision decision = provider.GetServices <ITransactionDecision>()
                                                .FirstOrDefault(m => m.IsFit(returnType));
                if (decision == null)
                {
                    throw new OsharpException($"无法找到与结果类型 {returnType} 匹配的 {typeof(ITransactionDecision)} 事务裁决器,请继承接口实现一个");
                }
                if (decision.CanCommit(context.ReturnValue))
                {
#if NET5_0
                    await unitOfWorkManager.CommitAsync();
#else
                    unitOfWorkManager.Commit();
#endif
                }
            }
            catch (Exception)
            {
#if NET5_0
                await unitOfWorkManager.CommitAsync();
#else
                unitOfWorkManager.Commit();
#endif
                throw;
            }
        }
        /// <summary>
        /// Handler which processes the command when
        /// customer executes cancel order from app
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task <Unit> Handle(CreateOrderCommand command, CancellationToken cancellationToken)
        {
            var order = new Order(
                command.UserId,
                new Address(command.Street, command.City, command.State, command.Country, command.ZipCode),
                command.Description,
                command.OrderItems.Select(x => x.ToOrderItem()).ToList());
            await _orderRepository.InsertAsync(order);

            await _unitOfWorkManager.CommitAsync();

            return(Unit.Value);
        }
        /// <summary>
        /// Handler which processes the command when
        /// customer executes cancel order from app
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task <Unit> Handle(ChangeOrderAddressCommand command, CancellationToken cancellationToken)
        {
            var order = await _orderRepository.GetAsync(command.OrderId);

            if (order == null)
            {
                return(Unit.Value);
            }

            order.ChangeAddress(command.NewAddress);
            await _unitOfWorkManager.CommitAsync();

            return(Unit.Value);
        }
        /// <summary>
        /// Handler which processes the command when
        /// customer executes cancel order from app
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public async Task <Unit> Handle(DeleteOrderCommand command, CancellationToken cancellationToken)
        {
            var order = await _orderRepository.GetAsync(command.OrderId);

            if (order == null)
            {
                return(Unit.Value);
            }

            await _orderRepository.DeleteAsync(order);

            await _unitOfWorkManager.CommitAsync();

            return(Unit.Value);
        }
Exemplo n.º 7
0
        public async Task <AjaxResult> Token(TokenDto dto)
        {
            string grantType = dto.GrantType?.UpperToLowerAndSplit("_");

            if (grantType == GrantType.Password)
            {
                Check.NotNull(dto.Account, nameof(dto.Account));
                Check.NotNull(dto.Password, nameof(dto.Password));

                LoginDto loginDto = new LoginDto()
                {
                    Account    = dto.Account,
                    Password   = dto.Password,
                    ClientType = dto.ClientType,
                    IsToken    = true,
                    Ip         = HttpContext.GetClientIp(),
                    UserAgent  = Request.Headers["User-Agent"].FirstOrDefault()
                };

                OperationResult <User> result = await _identityContract.Login(loginDto);

                IUnitOfWorkManager unitOfWorkManager = HttpContext.RequestServices.GetService <IUnitOfWorkManager>();
#if NET5_0
                await unitOfWorkManager.CommitAsync();
#else
                unitOfWorkManager.Commit();
#endif
                if (!result.Succeeded)
                {
                    return(result.ToAjaxResult());
                }

                User         user  = result.Data;
                JsonWebToken token = await CreateJwtToken(user, dto.ClientType);

                return(new AjaxResult("登录成功", AjaxResultType.Success, token));
            }

            if (grantType == GrantType.RefreshToken)
            {
                Check.NotNull(dto.RefreshToken, nameof(dto.RefreshToken));
                JsonWebToken token = await CreateJwtToken(dto.RefreshToken);

                return(new AjaxResult("刷新成功", AjaxResultType.Success, token));
            }

            return(new AjaxResult("GrantType错误", AjaxResultType.Error));
        }
Exemplo n.º 8
0
        public async Task <string> LockUser2()
        {
            List <string>      list        = new List <string>();
            UserManager <User> userManager = _provider.GetService <UserManager <User> >();
            User user2 = await userManager.FindByIdAsync("2");

            list.Add($"user2.IsLocked: {user2.IsLocked}");
            user2.IsLocked = !user2.IsLocked;
            await userManager.UpdateAsync(user2);

            IUnitOfWorkManager unitOfWorkManager = _provider.GetService <IUnitOfWorkManager>();

#if NET5_0
            await unitOfWorkManager.CommitAsync();
#else
            unitOfWorkManager.Commit();
#endif
            user2 = await userManager.FindByIdAsync("2");

            list.Add($"user2.IsLocked: {user2.IsLocked}");
            return(list.ExpandAndToString());
        }
Exemplo n.º 9
0
        public async Task <AjaxResult> LoginBind(UserLoginInfoEx loginInfo)
        {
            loginInfo.RegisterIp = HttpContext.GetClientIp();
            OperationResult <User> result = await _identityContract.LoginBind(loginInfo);

            IUnitOfWorkManager unitOfWorkManager = HttpContext.RequestServices.GetRequiredService <IUnitOfWorkManager>();

#if NET5_0
            await unitOfWorkManager.CommitAsync();
#else
            unitOfWorkManager.Commit();
#endif
            if (!result.Succeeded)
            {
                return(result.ToAjaxResult());
            }

            User         user  = result.Data;
            JsonWebToken token = await CreateJwtToken(user);

            return(new AjaxResult("登录成功", AjaxResultType.Success, token));
        }