添加项目文件。

This commit is contained in:
minjQiu
2024-07-11 23:22:22 +08:00
parent d0652f45fe
commit 71e2904de7
161 changed files with 92663 additions and 0 deletions
@@ -0,0 +1,112 @@
using Swashbuckle.Swagger;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Web.Http.Description;
using System.Xml;
namespace UFIDA.U8.Framework.Api
{
/// <inheritdoc />
public class SwaggerCacheProvider : ISwaggerProvider
{
private readonly ISwaggerProvider _swaggerProvider;
private static readonly ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>();
private readonly string _xmlPath;
/// <summary>
///
/// </summary>
/// <param name="swaggerProvider"></param>
/// <param name="xmlpath">xml文档路径</param>
public SwaggerCacheProvider(ISwaggerProvider swaggerProvider, string xmlpath)
{
_swaggerProvider = swaggerProvider;
_xmlPath = xmlpath;
}
/// <inheritdoc />
public SwaggerDocument GetSwagger(string rootUrl, string apiVersion)
{
var cacheKey = $"{rootUrl}_{apiVersion}";
//只读取一次
if (!_cache.TryGetValue(cacheKey, out var srcDoc))
{
srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion);
srcDoc.vendorExtensions = new Dictionary<string, object>
{
{ "ControllerDesc", GetControllerDesc() }
};
_cache.TryAdd(cacheKey, srcDoc);
}
return srcDoc;
}
/// <summary>
/// 从API文档中读取控制器描述
/// </summary>
/// <returns>所有控制器描述</returns>
public ConcurrentDictionary<string, string> GetControllerDesc()
{
var controllerDescDict = new ConcurrentDictionary<string, string>();
if (File.Exists(_xmlPath))
{
var xmldoc = new XmlDocument();
xmldoc.Load(_xmlPath);
var cCount = "Controller".Length;
var nodes = xmldoc.SelectNodes("//member");
if (nodes == null || nodes.Count <= 0)
return null;
foreach (XmlNode node in nodes)
{
var type = node.Attributes?["name"].Value;
if (type != null && !type.StartsWith("T:")) continue;
var arrPath = type?.Split('.');
var controllerName = arrPath?[arrPath.Length - 1];
if (controllerName != null && controllerName.EndsWith("Controller")) //控制器
{
//获取控制器注释
var summaryNode = node.SelectSingleNode("summary");
var key = controllerName.Remove(controllerName.Length - cCount, cCount);
if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key))
{
controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim());
}
}
}
}
return controllerDescDict;
}
}
/// <inheritdoc />
public class TokenRequiredHeaderParameter : IOperationFilter
{
/// <summary>
/// 参数名称
/// </summary>
public static string HeaderKey { get; set; }
/// <summary>
/// 参数说明
/// </summary>
public static string Description { get; set; }
/// <inheritdoc />
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.parameters == null)
operation.parameters = new List<Parameter>();
operation.parameters.Add(new Parameter
{
name = HeaderKey,
required = false,
description = Description,
type = "string",
@in = "header"
});
}
}
}