Compare commits

...

3 Commits

Author SHA1 Message Date
橙子
4b7abd4fe0 feat: 找出问题 2024-10-26 15:11:45 +08:00
橙子
bbc18dcaf9 feat: 简化测试 2024-10-26 01:12:06 +08:00
橙子
7cb78e70cb chorm: 问题复现 2024-10-26 00:40:43 +08:00
11 changed files with 293 additions and 555 deletions

View File

@@ -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; }

View File

@@ -6,8 +6,15 @@ 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>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
// [Permission("bbs:comment:add")]
// [Authorize]
Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input);
}
}

View File

@@ -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,116 +36,50 @@ 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>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
[Permission("bbs:comment:add")]
[Authorize]
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
// [Permission("bbs:comment:add")]
// [Authorize]
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)
{
}
}
}

View File

@@ -1,73 +1,73 @@
using TencentCloud.Tbm.V20180129.Models;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Entities.Forum;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Enums;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Domain.EventHandlers
{
/// <summary>
/// 评论创建的领域事件
/// </summary>
public class CommentCreatedEventHandler : ILocalEventHandler<EntityCreatedEventData<CommentAggregateRoot>>,
ITransientDependency
{
private ILocalEventBus _localEventBus;
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
private ISqlSugarRepository<UserAggregateRoot> _userRepository;
public CommentCreatedEventHandler(ILocalEventBus localEventBus, ISqlSugarRepository<DiscussAggregateRoot> discussRepository, ISqlSugarRepository<UserAggregateRoot> userRepository)
{
_userRepository = userRepository;
_localEventBus = localEventBus;
_discussRepository = discussRepository;
}
public async Task HandleEventAsync(EntityCreatedEventData<CommentAggregateRoot> eventData)
{
var commentEntity = eventData.Entity;
//给创建者发布数量+1
await _userRepository._Db.Updateable<BbsUserExtraInfoEntity>()
.SetColumns(it => it.CommentNumber == it.CommentNumber + 1)
.Where(it => it.UserId == commentEntity.CreatorId)
.ExecuteCommandAsync();
var disucssDto = await _discussRepository._DbQueryable
.Where(x => x.Id == commentEntity.DiscussId)
.LeftJoin<UserAggregateRoot>((dicuss, user) => dicuss.CreatorId == user.Id)
.Select((dicuss, user) =>
new
{
DiscussUserId = user.Id,
DiscussTitle = dicuss.Title,
})
.FirstAsync();
var commentUser = await _userRepository.GetFirstAsync(x => x.Id == commentEntity.CreatorId);
//截取30个长度
var content = commentEntity.Content.Length >= 30 ? commentEntity.Content.Substring(0, 30)+"..." : commentEntity.Content;
//通知主题作者,有人评论
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(disucssDto.DiscussUserId, string.Format(DiscussConst.CommentNotice, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
//如果为空,表示根路径,没有回复者
if (commentEntity.ParentId != Guid.Empty)
{
//通知回复者,有人评论
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(commentEntity.ParentId, string.Format(DiscussConst.CommentNoticeToReply, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
}
//最后发布任务触发事件
await _localEventBus.PublishAsync(
new AssignmentEventArgs(AssignmentRequirementTypeEnum.Comment, commentUser.Id),false);
}
}
}
// using TencentCloud.Tbm.V20180129.Models;
// using Volo.Abp.DependencyInjection;
// using Volo.Abp.Domain.Entities.Events;
// using Volo.Abp.EventBus;
// using Volo.Abp.EventBus.Local;
// using Yi.Framework.Bbs.Domain.Entities;
// using Yi.Framework.Bbs.Domain.Entities.Forum;
// using Yi.Framework.Bbs.Domain.Shared.Consts;
// using Yi.Framework.Bbs.Domain.Shared.Enums;
// using Yi.Framework.Bbs.Domain.Shared.Etos;
// using Yi.Framework.Rbac.Domain.Entities;
// using Yi.Framework.SqlSugarCore.Abstractions;
//
// namespace Yi.Framework.Bbs.Domain.EventHandlers
// {
// /// <summary>
// /// 评论创建的领域事件
// /// </summary>
// public class CommentCreatedEventHandler : ILocalEventHandler<EntityCreatedEventData<CommentAggregateRoot>>,
// ITransientDependency
// {
// private ILocalEventBus _localEventBus;
// private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
// private ISqlSugarRepository<UserAggregateRoot> _userRepository;
// public CommentCreatedEventHandler(ILocalEventBus localEventBus, ISqlSugarRepository<DiscussAggregateRoot> discussRepository, ISqlSugarRepository<UserAggregateRoot> userRepository)
// {
// _userRepository = userRepository;
// _localEventBus = localEventBus;
// _discussRepository = discussRepository;
// }
// public async Task HandleEventAsync(EntityCreatedEventData<CommentAggregateRoot> eventData)
// {
// var commentEntity = eventData.Entity;
//
// //给创建者发布数量+1
// await _userRepository._Db.Updateable<BbsUserExtraInfoEntity>()
// .SetColumns(it => it.CommentNumber == it.CommentNumber + 1)
// .Where(it => it.UserId == commentEntity.CreatorId)
// .ExecuteCommandAsync();
// var disucssDto = await _discussRepository._DbQueryable
// .Where(x => x.Id == commentEntity.DiscussId)
// .LeftJoin<UserAggregateRoot>((dicuss, user) => dicuss.CreatorId == user.Id)
// .Select((dicuss, user) =>
// new
// {
// DiscussUserId = user.Id,
// DiscussTitle = dicuss.Title,
//
// })
// .FirstAsync();
//
// var commentUser = await _userRepository.GetFirstAsync(x => x.Id == commentEntity.CreatorId);
//
// //截取30个长度
// var content = commentEntity.Content.Length >= 30 ? commentEntity.Content.Substring(0, 30)+"..." : commentEntity.Content;
// //通知主题作者,有人评论
// await _localEventBus.PublishAsync(new BbsNoticeEventArgs(disucssDto.DiscussUserId, string.Format(DiscussConst.CommentNotice, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
//
// //如果为空,表示根路径,没有回复者
// if (commentEntity.ParentId != Guid.Empty)
// {
// //通知回复者,有人评论
// await _localEventBus.PublishAsync(new BbsNoticeEventArgs(commentEntity.ParentId, string.Format(DiscussConst.CommentNoticeToReply, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
//
// }
//
// //最后发布任务触发事件
// await _localEventBus.PublishAsync(
// new AssignmentEventArgs(AssignmentRequirementTypeEnum.Comment, commentUser.Id),false);
//
// }
// }
// }

View File

@@ -41,7 +41,8 @@ namespace Yi.Framework.Bbs.Domain.Managers
entity.Content = content;
entity.ParentId = parentId;
entity.RootId = rootId;
return await _commentRepository.InsertReturnEntityAsync(entity);
await _commentRepository.InsertAsync(entity);
return entity;
}
/// <summary>

View File

@@ -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)));

View File

@@ -6,6 +6,8 @@ using Volo.Abp.Application.Services;
using Volo.Abp.Settings;
using Volo.Abp.Uow;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Banner;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Comment;
using Yi.Framework.Bbs.Application.Contracts.IServices;
using Yi.Framework.Bbs.Domain.Entities.Forum;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Extensions;
@@ -19,160 +21,15 @@ namespace Yi.Abp.Application.Services
/// </summary>
public class TestService : ApplicationService
{
/// <summary>
/// 属性注入
/// 不推荐,坑太多,容易把自己玩死,简单的东西可以用一用
/// </summary>
public ISqlSugarRepository<BannerAggregateRoot> sqlSugarRepository { get; set; }
/// <summary>
/// 动态Api
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpGet("hello-world")]
public string GetHelloWorld(string? name)
[HttpGet("hello-world/string")]
public async Task<string> GetHelloWorld1(string? name)
{
//会自动添加前缀,而不是重置,更符合习惯
//如果需要重置以"/"根目录开头即可
//你好世界
return name ?? "HelloWord";
return "1";
}
/// <summary>
/// 异常处理
/// </summary>
/// <returns></returns>
[HttpGet("error")]
public string GetError()
[HttpGet("hello-world/dto")]
public async Task<CommentGetOutputDto> GetHelloWorld2(string? name)
{
throw new UserFriendlyException("业务异常");
throw new Exception("系统异常");
return new CommentGetOutputDto();
}
/// <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;
}
}
}

View 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);
}
}
}

View File

@@ -4,28 +4,30 @@ using Yi.Abp.Web;
//创建日志,可使用{SourceContext}记录
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Error)
.MinimumLevel.Override("Quartz", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Async(c => c.File("logs/all/log-.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Debug))
.WriteTo.Async(c => c.File("logs/error/errorlog-.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error))
.WriteTo.Async(c => c.Console())
.CreateLogger();
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Error)
.MinimumLevel.Override("Quartz", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Async(c => c.File("logs/all/log-.txt", rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Debug))
.WriteTo.Async(c => c.File("logs/error/errorlog-.txt", rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Error))
.WriteTo.Async(c => c.Console())
.CreateLogger();
try
{
Log.Information("""
__ ___ ______ _
\ \ / (_) | ____| | |
\ \_/ / _ | |__ _ __ __ _ _ __ ___ _____ _____ _ __| | __
\ / | | | __| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
__ ___ ______ _
\ \ / (_) | ____| | |
\ \_/ / _ | |__ _ __ __ _ _ __ ___ _____ _____ _ __| | __
\ / | | | __| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
""");
""");
Log.Information("Yi框架-Abp.vNext启动");
var builder = WebApplication.CreateBuilder(args);
@@ -36,6 +38,22 @@ try
builder.Host.UseSerilog();
await builder.Services.AddApplicationAsync<YiAbpWebModule>();
var app = builder.Build();
app.Use(async (http, next) =>
{
var id = Guid.NewGuid();
Console.WriteLine("之前-----" + id);
http.Response.OnStarting(() =>
{
Console.WriteLine("之中-----" + id);
return Task.CompletedTask;
});
await next();
if (http.Request.Path.Value.Contains("hello-world"))
{
Console.WriteLine("之后-----" + id);
}
});
await app.InitializeApplicationAsync();
await app.RunAsync();
}

View 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; }
}

View File

@@ -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 = true;
//审计日志过滤器
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();