mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-02 15:50:54 +08:00
feat: 找出问题
This commit is contained in:
@@ -5,7 +5,7 @@ using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
|
||||
namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Comment
|
||||
{
|
||||
/// <summary>
|
||||
/// 单返回,返回单条评论即可
|
||||
/// 单返回,返回单条评论即可
|
||||
/// </summary>
|
||||
public class CommentGetOutputDto : EntityDto<Guid>
|
||||
{
|
||||
@@ -17,17 +17,17 @@ namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Comment
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用户id联表为用户对象
|
||||
/// 用户id联表为用户对象
|
||||
/// </summary>
|
||||
|
||||
public BbsUserGetOutputDto User { get; set; }
|
||||
/// <summary>
|
||||
/// 根节点的评论id
|
||||
/// 根节点的评论id
|
||||
/// </summary>
|
||||
public Guid RootId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被回复的CommentId
|
||||
/// 被回复的CommentId
|
||||
/// </summary>
|
||||
public Guid ParentId { get; set; }
|
||||
|
||||
|
||||
@@ -6,8 +6,7 @@ namespace Yi.Framework.Bbs.Application.Contracts.IServices
|
||||
/// <summary>
|
||||
/// Comment服务抽象
|
||||
/// </summary>
|
||||
public interface ICommentService : IYiCrudAppService<CommentGetOutputDto, CommentGetListOutputDto, Guid, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>
|
||||
{
|
||||
public interface ICommentService{
|
||||
/// <summary>
|
||||
/// 发表评论
|
||||
/// </summary>
|
||||
|
||||
@@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Yi.Framework.Bbs.Application.Contracts.Dtos.BbsUser;
|
||||
using Yi.Framework.Bbs.Application.Contracts.Dtos.Comment;
|
||||
using Yi.Framework.Bbs.Application.Contracts.IServices;
|
||||
@@ -21,19 +23,11 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
|
||||
/// <summary>
|
||||
/// 评论
|
||||
/// </summary>
|
||||
public class CommentService : YiCrudAppService<CommentAggregateRoot, CommentGetOutputDto, CommentGetListOutputDto, Guid, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
|
||||
public class CommentService : ApplicationService,
|
||||
ICommentService
|
||||
{
|
||||
private readonly ISqlSugarRepository<CommentAggregateRoot, Guid> _repository;
|
||||
private readonly BbsUserManager _bbsUserManager;
|
||||
public CommentService(ForumManager forumManager, ISqlSugarRepository<DiscussAggregateRoot> discussRepository, IDiscussService discussService, ISqlSugarRepository<CommentAggregateRoot, Guid> CommentRepository, BbsUserManager bbsUserManager) : base(CommentRepository)
|
||||
{
|
||||
_forumManager = forumManager;
|
||||
_discussRepository = discussRepository;
|
||||
_discussService = discussService;
|
||||
_repository = CommentRepository;
|
||||
_bbsUserManager = bbsUserManager;
|
||||
}
|
||||
|
||||
private ForumManager _forumManager { get; set; }
|
||||
|
||||
@@ -42,90 +36,20 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
|
||||
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository { get; set; }
|
||||
|
||||
private IDiscussService _discussService { get; set; }
|
||||
/// <summary>
|
||||
/// 获取改主题下的评论,结构为二维列表,该查询无分页
|
||||
/// Todo: 可放入领域层
|
||||
/// </summary>
|
||||
/// <param name="discussId"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<PagedResultDto<CommentGetListOutputDto>> GetDiscussIdAsync([FromRoute] Guid discussId, [FromQuery] CommentGetListInputVo input)
|
||||
|
||||
|
||||
public async Task<CommentGetOutputDto> Create2Async(CommentCreateInputVo input)
|
||||
{
|
||||
await _discussService.VerifyDiscussPermissionAsync(discussId);
|
||||
|
||||
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Content), x => x.Content.Contains(input.Content))
|
||||
.Where(x => x.DiscussId == discussId)
|
||||
.Includes(x => x.CreateUser)
|
||||
.ToListAsync();
|
||||
|
||||
//该实体需要进行转换
|
||||
|
||||
//同时为所有用户id进行bbs的扩展即可
|
||||
List<Guid> userIds = entities.Where(x => x.CreatorId != null).Select(x => x.CreatorId ?? Guid.Empty).ToList();
|
||||
var bbsUserInfoDic = (await _bbsUserManager.GetBbsUserInfoAsync(userIds)).ToDictionary(x => x.Id);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//------数据查询完成------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//从根目录开始组装
|
||||
//结果初始值,第一层等于全部根节点
|
||||
var allOutPut = entities.OrderByDescending(x => x.CreationTime).ToList();
|
||||
|
||||
//获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto
|
||||
List<CommentGetListOutputDto> allOutoutDto = await MapToGetListOutputDtosAsync(allOutPut);
|
||||
|
||||
//开始映射额外用户信息字段
|
||||
allOutoutDto?.ForEach(x => x.CreateUser = bbsUserInfoDic[x.CreatorId ?? Guid.Empty].Adapt<BbsUserGetOutputDto>());
|
||||
|
||||
//开始组装dto的层级关系
|
||||
//将全部数据进行hash
|
||||
var dic = allOutoutDto.ToDictionary(x => x.Id);
|
||||
|
||||
foreach (var comment in allOutoutDto)
|
||||
{
|
||||
//不是根节点,需要赋值 被评论者用户信息等
|
||||
if (comment.ParentId != Guid.Empty)
|
||||
{
|
||||
if (dic.ContainsKey(comment.ParentId))
|
||||
{
|
||||
var parentComment = dic[comment.ParentId];
|
||||
comment.CommentedUser = parentComment.CreateUser;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//root或者parent id,根节点都是等于0的
|
||||
var id = comment.RootId;
|
||||
if (id != Guid.Empty)
|
||||
{
|
||||
dic[id].Children.Add(comment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//子类需要排序
|
||||
var rootOutoutDto = allOutoutDto.Where(x => x.ParentId == Guid.Empty).ToList();
|
||||
rootOutoutDto?.ForEach(x =>
|
||||
{
|
||||
x.Children = x.Children.OrderByDescending(x => x.CreationTime).ToList();
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
return new PagedResultDto<CommentGetListOutputDto>(entities.Count(), rootOutoutDto);
|
||||
var entity = new CommentAggregateRoot(Guid.Empty);
|
||||
return new CommentGetOutputDto();
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("Create22")]
|
||||
public async Task<CommentGetOutputDto> Create22Async(CommentCreateInputVo input)
|
||||
{
|
||||
var entity = new CommentAggregateRoot(Guid.Empty);
|
||||
return new CommentGetOutputDto();
|
||||
}
|
||||
/// <summary>
|
||||
/// 发表评论
|
||||
/// </summary>
|
||||
@@ -134,24 +58,28 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
// [Permission("bbs:comment:add")]
|
||||
// [Authorize]
|
||||
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
|
||||
public async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
|
||||
{
|
||||
var discuess = await _discussRepository.GetFirstAsync(x => x.Id == input.DiscussId);
|
||||
if (discuess is null)
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.No_Exist);
|
||||
}
|
||||
// var discuess = await _discussRepository.GetFirstAsync(x => x.Id == input.DiscussId);
|
||||
// if (discuess is null)
|
||||
// {
|
||||
// throw new UserFriendlyException(DiscussConst.No_Exist);
|
||||
// }
|
||||
//不是超级管理员,且主题开启禁止评论
|
||||
|
||||
if (discuess.IsDisableCreateComment == true && !CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
|
||||
{
|
||||
throw new UserFriendlyException("该主题已禁止评论功能");
|
||||
}
|
||||
// if (discuess.IsDisableCreateComment == true && !CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
|
||||
// {
|
||||
// throw new UserFriendlyException("该主题已禁止评论功能");
|
||||
// }
|
||||
|
||||
|
||||
var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.ParentId, input.RootId, input.Content);
|
||||
return await MapToGetOutputDtoAsync(entity);
|
||||
// var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.ParentId, input.RootId, input.Content);
|
||||
var entity = new CommentAggregateRoot(Guid.Empty);
|
||||
return new CommentGetOutputDto();
|
||||
}
|
||||
|
||||
public CommentService(IRepository<CommentAggregateRoot, Guid> repository)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ namespace Yi.Framework.Rbac.Domain
|
||||
{
|
||||
var service = context.Services;
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
service.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<PermissionGlobalAttribute>();
|
||||
options.Filters.Add<OperLogGlobalAttribute>();
|
||||
});
|
||||
// service.AddControllers(options =>
|
||||
// {
|
||||
// options.Filters.Add<PermissionGlobalAttribute>();
|
||||
// // options.Filters.Add<OperLogGlobalAttribute>();
|
||||
// });
|
||||
|
||||
//配置阿里云短信
|
||||
Configure<AliyunOptions>(configuration.GetSection(nameof(AliyunOptions)));
|
||||
|
||||
@@ -21,178 +21,15 @@ namespace Yi.Abp.Application.Services
|
||||
/// </summary>
|
||||
public class TestService : ApplicationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 属性注入
|
||||
/// 不推荐,坑太多,容易把自己玩死,简单的东西可以用一用
|
||||
/// </summary>
|
||||
public ISqlSugarRepository<BannerAggregateRoot> sqlSugarRepository { get; set; }
|
||||
|
||||
private ICommentService _commentService;
|
||||
public readonly ISqlSugarRepository<CommentAggregateRoot, Guid> _commentRepository;
|
||||
public TestService(ICommentService commentService, ISqlSugarRepository<CommentAggregateRoot, Guid> commentRepository)
|
||||
[HttpGet("hello-world/string")]
|
||||
public async Task<string> GetHelloWorld1(string? name)
|
||||
{
|
||||
_commentService = commentService;
|
||||
_commentRepository = commentRepository;
|
||||
return "1";
|
||||
}
|
||||
|
||||
public async Task<string> GetAbpUnitOfWorkMiddleware()
|
||||
[HttpGet("hello-world/dto")]
|
||||
public async Task<CommentGetOutputDto> GetHelloWorld2(string? name)
|
||||
{
|
||||
var entity = new CommentAggregateRoot(Guid.Empty);
|
||||
entity.Content = "测试";
|
||||
entity.ParentId = Guid.Empty;
|
||||
entity.RootId = Guid.Empty;
|
||||
await _commentRepository.InsertAsync(entity);
|
||||
return "yes";
|
||||
return new CommentGetOutputDto();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态Api
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("hello-world")]
|
||||
public string GetHelloWorld(string? name)
|
||||
{
|
||||
//会自动添加前缀,而不是重置,更符合习惯
|
||||
//如果需要重置以"/"根目录开头即可
|
||||
//你好世界
|
||||
return name ?? "HelloWord";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异常处理
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("error")]
|
||||
public string GetError()
|
||||
{
|
||||
throw new UserFriendlyException("业务异常");
|
||||
throw new Exception("系统异常");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<object> GetSqlSugarDbAsync()
|
||||
{
|
||||
//用户体验优先,可直接使用Db操作,依赖抽象
|
||||
return await sqlSugarRepository._DbQueryable.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工作单元
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task GetUowAsync()
|
||||
{
|
||||
//魔改
|
||||
// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。
|
||||
// 请注意,如果requiresNew: true只有在没有工作单元内使用,嵌套子工作单元,默认值false即可
|
||||
// 自动在各个情况处理db客户端最优解之一
|
||||
int i = 3;
|
||||
List<Task> tasks = new List<Task>();
|
||||
await sqlSugarRepository.GetListAsync();
|
||||
await sqlSugarRepository.GetListAsync();
|
||||
while (i > 0)
|
||||
{
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入2" });
|
||||
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
|
||||
{
|
||||
await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入1" });
|
||||
await uow.CompleteAsync();
|
||||
}
|
||||
}));
|
||||
await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入3" });
|
||||
i--;
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void GetCurrentUser()
|
||||
{
|
||||
//当token鉴权之后,可以直接获取
|
||||
if (CurrentUser.Id is not null)
|
||||
{
|
||||
//权限
|
||||
CurrentUser.GetPermissions();
|
||||
|
||||
//角色信息
|
||||
CurrentUser.GetRoleInfo();
|
||||
|
||||
//部门id
|
||||
CurrentUser.GetDeptId();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据权限
|
||||
/// </summary>
|
||||
public void GetDataFilter()
|
||||
{
|
||||
//这里会数据权限过滤
|
||||
using (DataFilter.DisablePermissionHandler())
|
||||
{
|
||||
//这里不会数据权限过滤
|
||||
}
|
||||
//这里会数据权限过滤
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对象映射
|
||||
/// </summary>
|
||||
public void GetMapper()
|
||||
{
|
||||
//直接无脑Adapt,无需配置
|
||||
var entity = new BannerAggregateRoot();
|
||||
var dto = entity.Adapt<BannerGetListOutputDto>();
|
||||
}
|
||||
|
||||
private static int RequestNumber { get; set; } = 0;
|
||||
/// <summary>
|
||||
/// 速率限制
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
// [DisableRateLimiting]
|
||||
//[EnableRateLimiting("sliding")]
|
||||
public int GetRateLimiting()
|
||||
{
|
||||
RequestNumber++;
|
||||
return RequestNumber;
|
||||
}
|
||||
|
||||
|
||||
public ISettingProvider _settingProvider { get; set; }
|
||||
|
||||
public ISettingManager _settingManager { get; set; }
|
||||
/// <summary>
|
||||
/// 系统配置模块
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetSettingAsync()
|
||||
{
|
||||
//DDD需要提前定义
|
||||
//默认来说,不提供修改操作,配置应该独立
|
||||
var enableOrNull = await _settingProvider.GetOrNullAsync("DDD");
|
||||
|
||||
//如果要进行修改,可使用yi.framework下的ISettingManager
|
||||
await _settingManager.SetGlobalAsync("DDD", "false");
|
||||
|
||||
var enableOrNull2 = await _settingManager.GetOrNullGlobalAsync("DDD");
|
||||
|
||||
//当然,他的独特地方,是支持来自多个模块,例如配置文件?
|
||||
var result = await _settingManager.GetOrNullConfigurationAsync("Test");
|
||||
|
||||
|
||||
return result ?? string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
65
Yi.Abp.Net8/src/Yi.Abp.Web/AbpUnitOfWorkMiddleware2.cs
Normal file
65
Yi.Abp.Net8/src/Yi.Abp.Web/AbpUnitOfWorkMiddleware2.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
// Decompiled with JetBrains decompiler
|
||||
// Type: Volo.Abp.AspNetCore.Uow.AbpUnitOfWorkMiddleware
|
||||
// Assembly: Volo.Abp.AspNetCore, Version=8.2.0.0, Culture=neutral, PublicKeyToken=null
|
||||
// MVID: E24BDAEE-E92D-4420-84F7-3DD088C817A4
|
||||
// Assembly location: C:\Users\Administrator\.nuget\packages\volo.abp.aspnetcore\8.2.0\lib\net8.0\Volo.Abp.AspNetCore.dll
|
||||
// XML documentation location: C:\Users\Administrator\.nuget\packages\volo.abp.aspnetcore\8.2.0\lib\net8.0\Volo.Abp.AspNetCore.xml
|
||||
|
||||
using Microsoft.AspNetCore.Components.Endpoints;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.AspNetCore.Middleware;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Uow;
|
||||
|
||||
#nullable enable
|
||||
namespace Volo.Abp.AspNetCore.Uow
|
||||
{
|
||||
public class AbpUnitOfWorkMiddleware2 : AbpMiddlewareBase, ITransientDependency
|
||||
{
|
||||
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
||||
private readonly AbpAspNetCoreUnitOfWorkOptions _options;
|
||||
|
||||
public AbpUnitOfWorkMiddleware2(
|
||||
IUnitOfWorkManager unitOfWorkManager,
|
||||
IOptions<AbpAspNetCoreUnitOfWorkOptions> options)
|
||||
{
|
||||
this._unitOfWorkManager = unitOfWorkManager;
|
||||
this._options = options.Value;
|
||||
}
|
||||
|
||||
public override async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
||||
{
|
||||
// await next(context);
|
||||
// await next(context).ConfigureAwait(false);
|
||||
if (await ShouldSkipAsync(context, next).ConfigureAwait(false) || IsIgnoredUrl(context))
|
||||
{
|
||||
await next(context).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
using (IUnitOfWork uow = _unitOfWorkManager.Reserve("_AbpActionUnitOfWork"))
|
||||
{
|
||||
await next(context).ConfigureAwait(false);
|
||||
await uow.CompleteAsync(context.RequestAborted).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsIgnoredUrl(HttpContext context)
|
||||
{
|
||||
return context.Request.Path.Value != null && this._options.IgnoredUrls.Any<string>(
|
||||
(Func<string, bool>)(x =>
|
||||
context.Request.Path.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
protected override async Task<bool> ShouldSkipAsync(HttpContext context, RequestDelegate next)
|
||||
{
|
||||
return context.GetEndpoint()?.Metadata?.GetMetadata<RootComponentMetadata>() != null ||
|
||||
await base.ShouldSkipAsync(context, next).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,11 @@ try
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
await next();
|
||||
Console.WriteLine("之后-----" + id);
|
||||
if (http.Request.Path.Value.Contains("hello-world"))
|
||||
{
|
||||
Console.WriteLine("之后-----" + id);
|
||||
}
|
||||
|
||||
});
|
||||
await app.InitializeApplicationAsync();
|
||||
await app.RunAsync();
|
||||
|
||||
42
Yi.Abp.Net8/src/Yi.Abp.Web/TestService.cs
Normal file
42
Yi.Abp.Net8/src/Yi.Abp.Web/TestService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Application.Services;
|
||||
|
||||
namespace Yi.Abp.Application.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 常用魔改及扩展示例
|
||||
/// </summary>
|
||||
public class TestService : ApplicationService
|
||||
{
|
||||
[HttpGet("hello-world/string")]
|
||||
public async Task<string> GetHelloWorld1(string? name)
|
||||
{
|
||||
return "1";
|
||||
}
|
||||
[HttpGet("hello-world/dto")]
|
||||
public async Task<CommentGetOutputDto> GetHelloWorld2(string? name)
|
||||
{
|
||||
return new CommentGetOutputDto();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class CommentGetOutputDto
|
||||
{
|
||||
|
||||
public DateTime? CreateTime { get; set; }
|
||||
public string Content { get; set; }
|
||||
|
||||
public Guid DiscussId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根节点的评论id
|
||||
/// </summary>
|
||||
public Guid RootId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被回复的CommentId
|
||||
/// </summary>
|
||||
public Guid ParentId { get; set; }
|
||||
|
||||
}
|
||||
@@ -42,14 +42,14 @@ using Yi.Framework.TenantManagement.Application;
|
||||
namespace Yi.Abp.Web
|
||||
{
|
||||
[DependsOn(
|
||||
typeof(YiAbpSqlSugarCoreModule),
|
||||
typeof(YiAbpApplicationModule),
|
||||
typeof(AbpAspNetCoreMultiTenancyModule),
|
||||
// typeof(YiAbpSqlSugarCoreModule),
|
||||
// typeof(YiAbpApplicationModule),
|
||||
// typeof(AbpAspNetCoreMultiTenancyModule),
|
||||
typeof(AbpAspNetCoreMvcModule),
|
||||
typeof(AbpAutofacModule),
|
||||
typeof(AbpSwashbuckleModule),
|
||||
typeof(AbpAspNetCoreSerilogModule),
|
||||
typeof(AbpAuditingModule),
|
||||
// typeof(AbpAuditingModule),
|
||||
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
||||
typeof(YiFrameworkAspNetCoreModule),
|
||||
typeof(YiFrameworkAspNetCoreAuthenticationOAuthModule)
|
||||
@@ -63,59 +63,29 @@ namespace Yi.Abp.Web
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
var host = context.Services.GetHostingEnvironment();
|
||||
var service = context.Services;
|
||||
//请求日志
|
||||
Configure<AbpAuditingOptions>(optios =>
|
||||
{
|
||||
//默认关闭,开启会有大量的审计日志
|
||||
optios.IsEnabled = false;
|
||||
//审计日志过滤器
|
||||
optios.AlwaysLogSelectors.Add(x => Task.FromResult(!x.Url.StartsWith("/api/app/file/")));
|
||||
});
|
||||
|
||||
//采用furion格式的规范化api,默认不开启,使用abp优雅的方式
|
||||
//你没看错。。。
|
||||
//service.AddFurionUnifyResultApi();
|
||||
|
||||
//配置错误处理显示详情
|
||||
Configure<AbpExceptionHandlingOptions>(options => { options.SendExceptionsDetailsToClients = true; });
|
||||
|
||||
//动态Api
|
||||
Configure<AbpAspNetCoreMvcOptions>(options =>
|
||||
{
|
||||
options.ConventionalControllers.Create(typeof(YiAbpApplicationModule).Assembly,
|
||||
options.ConventionalControllers.Create(typeof(YiAbpWebModule).Assembly,
|
||||
options => options.RemoteServiceName = "default");
|
||||
options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly,
|
||||
options => options.RemoteServiceName = "rbac");
|
||||
options.ConventionalControllers.Create(typeof(YiFrameworkBbsApplicationModule).Assembly,
|
||||
options => options.RemoteServiceName = "bbs");
|
||||
options.ConventionalControllers.Create(typeof(YiFrameworkChatHubApplicationModule).Assembly,
|
||||
options => options.RemoteServiceName = "chat-hub");
|
||||
options.ConventionalControllers.Create(typeof(YiFrameworkTenantManagementApplicationModule).Assembly,
|
||||
options => options.RemoteServiceName = "tenant-management");
|
||||
options.ConventionalControllers.Create(typeof(YiFrameworkCodeGenApplicationModule).Assembly,
|
||||
options => options.RemoteServiceName = "code-gen");
|
||||
|
||||
|
||||
//统一前缀
|
||||
options.ConventionalControllers.ConventionalControllerSettings.ForEach(x => x.RootPath = "api/app");
|
||||
});
|
||||
|
||||
//设置api格式
|
||||
service.AddControllers().AddNewtonsoftJson(options =>
|
||||
{
|
||||
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
options.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
});
|
||||
// service.AddControllers().AddNewtonsoftJson(options =>
|
||||
// {
|
||||
// options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
// });
|
||||
|
||||
//设置缓存不要过期,默认滑动20分钟
|
||||
Configure<AbpDistributedCacheOptions>(cacheOptions =>
|
||||
{
|
||||
cacheOptions.GlobalCacheEntryOptions.SlidingExpiration = null;
|
||||
//缓存key前缀
|
||||
cacheOptions.KeyPrefix = "Yi:";
|
||||
});
|
||||
|
||||
|
||||
|
||||
Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
|
||||
// Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
|
||||
|
||||
//Swagger
|
||||
context.Services.AddYiSwaggerGen<YiAbpWebModule>(options =>
|
||||
@@ -124,143 +94,9 @@ namespace Yi.Abp.Web
|
||||
new OpenApiInfo { Title = "Yi.Framework.Abp", Version = "v1", Description = "集大成者" });
|
||||
});
|
||||
|
||||
//跨域
|
||||
context.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(DefaultCorsPolicyName, builder =>
|
||||
{
|
||||
builder
|
||||
.WithOrigins(
|
||||
configuration["App:CorsOrigins"]!
|
||||
.Split(";", StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(o => o.RemovePostFix("/"))
|
||||
.ToArray()
|
||||
)
|
||||
.WithAbpExposedHeaders()
|
||||
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
//配置多租户
|
||||
Configure<AbpTenantResolveOptions>(options =>
|
||||
{
|
||||
//基于cookie jwt不好用,有坑
|
||||
options.TenantResolvers.Clear();
|
||||
options.TenantResolvers.Add(new HeaderTenantResolveContributor());
|
||||
//options.TenantResolvers.Add(new HeaderTenantResolveContributor());
|
||||
//options.TenantResolvers.Add(new CookieTenantResolveContributor());
|
||||
|
||||
//options.TenantResolvers.RemoveAll(x => x.Name == CookieTenantResolveContributor.ContributorName);
|
||||
});
|
||||
|
||||
//速率限制
|
||||
//每60秒限制100个请求,滑块添加,分6段
|
||||
service.AddRateLimiter(_ =>
|
||||
{
|
||||
_.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
|
||||
_.OnRejected = (context, _) =>
|
||||
{
|
||||
if (context.Lease.TryGetMetadata(MetadataName.RetryAfter, out var retryAfter))
|
||||
{
|
||||
context.HttpContext.Response.Headers.RetryAfter =
|
||||
((int)retryAfter.TotalSeconds).ToString(NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
|
||||
context.HttpContext.Response.StatusCode = StatusCodes.Status429TooManyRequests;
|
||||
context.HttpContext.Response.WriteAsync("Too many requests. Please try again later.");
|
||||
|
||||
return new ValueTask();
|
||||
};
|
||||
|
||||
//全局使用,链式表达式
|
||||
_.GlobalLimiter = PartitionedRateLimiter.CreateChained(
|
||||
PartitionedRateLimiter.Create<HttpContext, string>(httpContext =>
|
||||
{
|
||||
var userAgent = httpContext.Request.Headers.UserAgent.ToString();
|
||||
|
||||
return RateLimitPartition.GetSlidingWindowLimiter
|
||||
(userAgent, _ =>
|
||||
new SlidingWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = 1000,
|
||||
Window = TimeSpan.FromSeconds(60),
|
||||
SegmentsPerWindow = 6,
|
||||
QueueProcessingOrder = QueueProcessingOrder.OldestFirst
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
//jwt鉴权
|
||||
var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>();
|
||||
var refreshJwtOptions = configuration.GetSection(nameof(RefreshJwtOptions)).Get<RefreshJwtOptions>();
|
||||
|
||||
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = jwtOptions.Issuer,
|
||||
ValidAudience = jwtOptions.Audience,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecurityKey))
|
||||
};
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
var accessToken = context.Request.Query["access_token"];
|
||||
if (!string.IsNullOrEmpty(accessToken))
|
||||
{
|
||||
context.Token = accessToken;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
})
|
||||
.AddJwtBearer(TokenTypeConst.Refresh, options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = refreshJwtOptions.Issuer,
|
||||
ValidAudience = refreshJwtOptions.Audience,
|
||||
IssuerSigningKey =
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(refreshJwtOptions.SecurityKey))
|
||||
};
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
var refresh_token = context.Request.Headers["refresh_token"];
|
||||
if (!string.IsNullOrEmpty(refresh_token))
|
||||
{
|
||||
context.Token = refresh_token;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var refreshToken = context.Request.Query["refresh_token"];
|
||||
if (!string.IsNullOrEmpty(refreshToken))
|
||||
{
|
||||
context.Token = refreshToken;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
})
|
||||
.AddQQ(options => { configuration.GetSection("OAuth:QQ").Bind(options); })
|
||||
.AddGitee(options => { configuration.GetSection("OAuth:Gitee").Bind(options); });
|
||||
|
||||
//授权
|
||||
context.Services.AddAuthorization();
|
||||
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -277,30 +113,17 @@ namespace Yi.Abp.Web
|
||||
//跨域
|
||||
app.UseCors(DefaultCorsPolicyName);
|
||||
|
||||
if (!env.IsDevelopment())
|
||||
{
|
||||
//速率限制
|
||||
app.UseRateLimiter();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//无感token,先刷新再鉴权
|
||||
app.UseRefreshToken();
|
||||
|
||||
//鉴权
|
||||
app.UseAuthentication();
|
||||
|
||||
//多租户
|
||||
app.UseMultiTenancy();
|
||||
|
||||
|
||||
//swagger
|
||||
app.UseYiSwagger();
|
||||
|
||||
//流量访问统计,需redis支持,否则不生效
|
||||
app.UseAccessLog();
|
||||
|
||||
//请求处理
|
||||
app.UseYiApiHandlinge();
|
||||
|
||||
|
||||
//静态资源
|
||||
app.UseStaticFiles("/api/app/wwwroot");
|
||||
@@ -308,15 +131,12 @@ namespace Yi.Abp.Web
|
||||
app.UseDirectoryBrowser("/api/app/wwwroot");
|
||||
|
||||
|
||||
// app.Properties.Add("_AbpExceptionHandlingMiddleware_Added",false);
|
||||
//工作单元
|
||||
app.UseUnitOfWork();
|
||||
|
||||
//授权
|
||||
app.UseAuthorization();
|
||||
|
||||
//审计日志
|
||||
app.UseAuditing();
|
||||
|
||||
//日志记录
|
||||
app.UseAbpSerilogEnrichers();
|
||||
|
||||
Reference in New Issue
Block a user