Exemplo n.º 1
0
        /// <summary>
        /// 更新指定字段内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Result <int> > UpdateUserAsync(long id, string field, string value, CancellationToken cancellationToken = default)
        {
            var res = await adminUserService.UpdateAsync(id, field, value, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="status"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Result <int> > UpdateUserAsync(long[] ids, Status status, CancellationToken cancellationToken = default)
        {
            var res = await adminUserService.UpdateAsync(ids, status, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 3
0
        public async Task <Result <int> > UpdateUserAsync([FromQuery] AdminUserUpdateFieldCommand command, CancellationToken cancellationToken = default)
        {
            var res = await bus.SendCommand(command, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 4
0
        public async Task <Result <int> > CreateMenuAsync([FromBody] AdminMenuCreateCommand command, CancellationToken cancellationToken = default)
        {
            var res = await bus.SendCommand(command, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 5
0
        public async Task <Result <int> > UpdateUserAsync([Required][FromBody] AdminUserUpdatePasswordCommand request, CancellationToken cancellationToken = default)
        {
            var res = await adminUserService.UpdateAsync(request, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 6
0
        public async Task <Result <int> > DeleteMenuAsync([Required] long[] ids, CancellationToken cancellationToken = default)
        {
            var res = await adminMenuService.DeleteAsync(ids, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 7
0
        public async Task <Result <int> > CreateMenuAsync([Required][FromBody] AdminMenuCreateCommand request, CancellationToken cancellationToken = default)
        {
            var res = await adminMenuService.CreateAsync(request, cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 8
0
        public async Task <Result <int> > DeleteMenuAsync([Required] long[] ids, CancellationToken cancellationToken = default)
        {
            var res = await db.DeleteAsync <AdminMenuEntity>(c => ids.Contains(c.Id), cancellationToken);

            if (res > 0)
            {
                await db.DeleteAsync <AdminRoleMenuEntity>(c => ids.Contains(c.MenuId), cancellationToken);

                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 9
0
        public async Task <Result <int> > CreateMenuAsync([Required][FromBody] AdminMenuCreateCommand request, CancellationToken cancellationToken = default)
        {
            var entity = mapper.Map <AdminMenuCreateCommand, AdminMenuEntity>(request);

            var res = await db.AddAsync(entity, cancellationToken : cancellationToken);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 10
0
        public async Task <Result <int> > DeleteRoleAsync([Required] long[] ids, CancellationToken cancellationToken = default)
        {
            var res = await db.DeleteAsync <AdminRoleEntity>(c => ids.Contains(c.Id));

            if (res > 0)
            {
                //删除关联的导航
                await db.DeleteAsync <AdminRoleMenuEntity>(c => ids.Contains(c.RoleId));

                //删除用户关联的角色
                await db.DeleteAsync <AdminUserRoleEntity>(c => ids.Contains(c.RoleId));

                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 11
0
        public async Task <Result <int> > DeleteUserAsync([Required] long[] ids, CancellationToken cancellationToken = default)
        {
            var res = await db.DeleteAsync <AdminUserEntity>(c => ids.Contains(c.Id), cancellationToken);

            if (res > 0)
            {
                //删除登录记录
                await db.DeleteAsync <LoginRecordEntity>(c => ids.Contains(c.AdminId), cancellationToken);

                //删除关联的角色
                await db.DeleteAsync <AdminUserRoleEntity>(c => ids.Contains(c.AdminId), cancellationToken);

                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 12
0
        public async Task <Result <int> > UpdateMenuStatusAsync([Required] long[] ids, [Required] Status status, CancellationToken cancellationToken = default)
        {
            var res = 0;

            switch (status)
            {
            case Status.Show:
                res = await db.UpdateAsync <AdminMenuEntity>(c => ids.Contains(c.Id), c => new AdminMenuEntity {
                    Status = Status.Show, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case Status.SoldOut:
                res = await db.UpdateAsync <AdminMenuEntity>(c => ids.Contains(c.Id), c => new AdminMenuEntity {
                    Status = Status.SoldOut, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case Status.Trash:
                res = await db.UpdateAsync <AdminMenuEntity>(c => ids.Contains(c.Id), c => new AdminMenuEntity {
                    Status = Status.Trash, Deleted_At = DateTime.Now
                }, cancellationToken);

                break;

            default:
                res = 0;
                break;
            }

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 13
0
        public async Task <Result <int> > UpdateUserAsync([Required][FromBody] AdminUserUpdateInfoCommand request, CancellationToken cancellationToken = default)
        {
            var entity = await db.Context.AdminUser.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken);

            if (entity == null)
            {
                return(RestFull.Fail(data: 0));
            }

            entity = mapper.Map(request, entity);

            var res = db.Update(entity);

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }
Exemplo n.º 14
0
        public static IServiceCollection AddApplication(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));

            services.AddHttpContextAccessor();

            services.AddAutoMapper(typeof(MappingProfile));

            services.AddMediatR(typeof(IAppService), typeof(IDbService));

            services.Scan(scan =>
                          scan.FromAssemblyOf <IAppService>()
                          .AddClasses(impl => impl.AssignableTo(typeof(IAppService)))
                          .AsImplementedInterfaces()
                          .WithScopedLifetime()
                          );

            services.Scan(scan =>
                          scan.FromAssemblyOf <IDbService>()
                          .AddClasses(impl => impl.AssignableTo(typeof(IDbService)))
                          .AsImplementedInterfaces()
                          .WithScopedLifetime()
                          );

            // 注入redis插件
            //services.AddRedisService().AddJsonRedisSerializer();

            //// 注入缓存拦截器(Redis分布式缓存)
            //services.AddCacheService<RedisCacheService>((option) =>
            //{
            //    option.RedisRead = "cache-read";
            //    option.RedisWrite = "cache-write";
            //});

            // 注入缓存拦截器(内存缓存)
            services.AddCacheService <MemoryCacheService>();

            //添加跨域配置,加载进来,启用的话需要使用Configure
            services.AddCors(options =>
                             options.AddPolicy(policyName, builder =>
            {
                builder.AllowAnyHeader();
                builder.AllowAnyOrigin();
                builder.AllowAnyMethod();
            })
                             );

            // 注册jwt
            services.AddJwt <JwtHandler>(enableGlobalAuthorize: true);//enableGlobalAuthorize: true

            services
            .AddControllers()
            .AddMessagePackFormatters(options =>
            {
                options.JsonSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
                options.JsonSerializerSettings.ContractResolver     = new LimitPropsContractResolver();

                //默认设置MessageagePack的日期序列化格式为时间戳,对外输出一致为时间戳的日期,不需要我们自己去序列化,自动操作。
                //C#实体内仍旧保持DateTime。跨语言MessageagePack没有DateTime类型。
                options.FormatterResolver = MessagePackSerializerResolver.UnixDateTimeFormatter;
                options.Options           = MessagePackSerializerResolver.UnixDateTimeOptions;
            })
            .AddFluentValidation(opt =>
            {
                opt.ValidatorOptions.CascadeMode     = FluentValidation.CascadeMode.Stop;
                opt.DisableDataAnnotationsValidation = false;
                opt.RegisterValidatorsFromAssemblyContaining(typeof(IDbService));
            });

            // 统一返回验证的信息
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = (context) =>
                {
                    var errors = context.ModelState
                                 .Values
                                 .SelectMany(x => x.Errors.Select(p => p.ErrorMessage))
                                 .ToList();

                    var message = errors.Join("");

                    return(new ObjectResult(RestFull.Fail <object>(SubCode.ValidError, message))
                    {
                        StatusCode = StatusCodes.Status200OK
                    });
                };
            });

            // 注册上传服务
            services.AddUploadService();

            // 注册OSS上传服务(阿里Oss)
            services.AddOssClient(
                (
                    "images", new OssOptions
            {
                AccessKey = "xxx",
                AccessKeySecret = "xxx",
                BluckName = "xxx",
                EndPoint = "oss-cn-hangzhou.aliyuncs.com",
                Domain = "https://img.xxx.com"
            }
                )
                );

            return(services);
        }
Exemplo n.º 15
0
        public async Task <Result <int> > UpdateUserFieldAsync([Required] long id, [Required] string field, string value, CancellationToken cancellationToken = default)
        {
            var res = 0;

            switch (field.ToLower())
            {
            case "name":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Name = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "username":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    UserName = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "mobile":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Mobile = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "password":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Password = Encrypt.Md5By32(value), Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "position":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Position = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "location":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Location = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "company":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Company = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            case "picture":
                res = await db.UpdateAsync <AdminUserEntity>(c => c.Id == id, c => new AdminUserEntity()
                {
                    Picture = value, Updated_At = DateTime.Now
                }, cancellationToken);

                break;

            default:
                res = 0;
                break;
            }

            if (res > 0)
            {
                return(RestFull.Success(data: res));
            }
            else
            {
                return(RestFull.Fail(data: res));
            }
        }