From d51e682110a1b6150dbbb7408d8e738594a7ddac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 30 Dec 2023 16:10:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0swagger=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=8C=E4=BC=98=E5=8C=96=E6=9E=9A=E4=B8=BE=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SwaggerAddExtensions.cs | 55 ++++++++++++++++++- .../Dtos/Article/ArticleImprotDto.cs | 5 +- .../Services/ArticleService.cs | 5 +- .../Enums/ArticleImportTypeEnum.cs | 13 ++--- .../ArticleImport/AbstractArticleImport.cs | 3 +- .../ArticleImport/VuePressArticleImport.cs | 6 +- .../Managers/ForumManager.cs | 7 ++- Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 4 +- 8 files changed, 79 insertions(+), 19 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs index 8afd6878..dc7c5b99 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs @@ -1,7 +1,11 @@ -using System.Diagnostics; +using System.ComponentModel; +using System.Diagnostics; +using System.Text; +using System.Xml.Linq; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using Volo.Abp.AspNetCore.Mvc; @@ -75,12 +79,59 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection { [scheme] = new string[0] }); + + + options.SchemaFilter(); } ); - + return services; } } + + + /// + /// Swagger文档枚举字段显示枚举属性和枚举值,以及枚举描述 + /// + public class EnumSchemaFilter : ISchemaFilter + { + /// + /// 实现接口 + /// + /// + /// + + public void Apply(OpenApiSchema model, SchemaFilterContext context) + { + if (context.Type.IsEnum) + { + model.Enum.Clear(); + model.Type = "string"; + model.Format = null; + + + StringBuilder stringBuilder = new StringBuilder(); + Enum.GetNames(context.Type) + .ToList() + .ForEach(name => + { + Enum e = (Enum)Enum.Parse(context.Type, name); + var descrptionOrNull = GetEnumDescription(e); + model.Enum.Add(new OpenApiString(name)); + stringBuilder.Append($"【枚举:{name}{(descrptionOrNull is null ? string.Empty : $"({descrptionOrNull})")}={Convert.ToInt64(Enum.Parse(context.Type, name))}】
"); + }); + model.Description= stringBuilder.ToString(); + } + } + + private static string? GetEnumDescription(Enum value) + { + var fieldInfo = value.GetType().GetField(value.ToString()); + var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); + return attributes.Length > 0 ? attributes[0].Description : null; + } + + } } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Article/ArticleImprotDto.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Article/ArticleImprotDto.cs index cb078ddd..98e8ba3d 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Article/ArticleImprotDto.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Article/ArticleImprotDto.cs @@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Yi.Framework.Bbs.Domain.Shared.Enums; namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Article @@ -16,6 +17,8 @@ namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Article [Required] public Guid DiscussId { get; set; } + public Guid ArticleParentId { get; set; }= Guid.Empty; + public ArticleImportTypeEnum ImportType { get; set; } = ArticleImportTypeEnum.Defalut; - } +} } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs index 5cbb0787..57de3e2b 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; using Mapster; using Microsoft.AspNetCore.Authorization; @@ -143,7 +144,7 @@ namespace Yi.Framework.Bbs.Application.Services /// 导入文章 /// /// - public async Task PostImportAsync(ArticleImprotDto input, [FromForm] IFormFileCollection file) + public async Task PostImportAsync([FromQuery] ArticleImprotDto input, [FromForm][Required] IFormFileCollection file) { var fileObjs = new List(); if (file.Count > 0) @@ -172,7 +173,7 @@ namespace Yi.Framework.Bbs.Application.Services throw new UserFriendlyException("未选择文件"); } //使用简单工厂根据传入的类型进行判断 - await _forumManager.PostImportAsync(input.DiscussId, fileObjs, input.ImportType); + await _forumManager.PostImportAsync(input.DiscussId, input.ArticleParentId, fileObjs, input.ImportType); } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Enums/ArticleImportTypeEnum.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Enums/ArticleImportTypeEnum.cs index 6c6f2ac3..01686b76 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Enums/ArticleImportTypeEnum.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain.Shared/Enums/ArticleImportTypeEnum.cs @@ -1,17 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace Yi.Framework.Bbs.Domain.Shared.Enums { + [JsonConverter(typeof(StringEnumConverter))] public enum ArticleImportTypeEnum { - //默认导入方式 + [Description("默认导入方式")] Defalut, - //vuePresss方式 + [Description("vuePresss方式")] VuePress } } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs index 3a240256..c39b5dec 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs @@ -10,12 +10,13 @@ namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport { public abstract class AbstractArticleImport { - public virtual List Import(Guid discussId, List fileObjs) + public virtual List Import(Guid discussId,Guid articleParentId, List fileObjs) { var articles = Convert(fileObjs); articles.ForEach(article => { article.DiscussId = discussId; + article.ParentId = articleParentId; }); return articles; } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs index c0f8867d..6ebb0626 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs @@ -46,8 +46,10 @@ namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport } } - lines.ToList().RemoveRange(0, num); - var result = string.Join(Environment.NewLine, lines); + var linesRef = lines.ToList(); + + linesRef.RemoveRange(0, startIndex+1); + var result = string.Join(Environment.NewLine, linesRef); f.Content = result; return f; }); diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs index 5e822fea..7ffd1225 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs @@ -48,10 +48,11 @@ namespace Yi.Framework.Bbs.Domain.Managers /// 导入文章 /// /// + /// /// /// /// - public async Task PostImportAsync(Guid discussId, List fileObjs, ArticleImportTypeEnum importType) + public async Task PostImportAsync(Guid discussId,Guid articleParentId, List fileObjs, ArticleImportTypeEnum importType) { AbstractArticleImport abstractArticleImport = default; switch (importType) @@ -67,9 +68,9 @@ namespace Yi.Framework.Bbs.Domain.Managers default: abstractArticleImport = new DefaultArticleImport(); break; } - var articleHandled = abstractArticleImport.Import(discussId, fileObjs); + var articleHandled = abstractArticleImport.Import(discussId, articleParentId, fileObjs); - await _articleRepository.InsertManyAsync(articleHandled); + //await _articleRepository.InsertManyAsync(articleHandled); } } diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index 83fdecfb..13dc515b 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -1,9 +1,10 @@ using System.Text; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; +using Newtonsoft.Json.Converters; using Volo.Abp; using Volo.Abp.AspNetCore.Authentication.JwtBearer; using Volo.Abp.AspNetCore.Mvc; @@ -65,6 +66,7 @@ namespace Yi.Abp.Web service.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; + options.SerializerSettings.Converters.Add(new StringEnumConverter()); }); Configure(options =>