Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac9fd706f9 | ||
|
|
8b4d551af8 | ||
|
|
3445531972 | ||
|
|
2268ad0c8c | ||
|
|
0b6c6af51d | ||
|
|
1c8ea86fd3 | ||
|
|
f5e4cc97ba | ||
|
|
8550d663de |
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimApi.Communications
|
namespace SimApi.Communications
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -9,6 +10,15 @@ namespace SimApi.Communications
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 动态类型单字段请求
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
public class SimApiOneFieldRequest<T>
|
||||||
|
{
|
||||||
|
public T Data { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 基础分页请求
|
/// 基础分页请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace SimApi.Configs
|
|||||||
{
|
{
|
||||||
public class SimApiOptions
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
|
public bool EnableCors { get; set; } = true;
|
||||||
public bool EnableSimApiAuth { get; set; } = false;
|
public bool EnableSimApiAuth { get; set; } = false;
|
||||||
public bool EnableSimApiDoc { get; set; } = true;
|
public bool EnableSimApiDoc { get; set; } = true;
|
||||||
public bool EnableSimApiException { get; set; } = true;
|
public bool EnableSimApiException { get; set; } = true;
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ namespace SimApi.Helpers
|
|||||||
public class SimApiStorage
|
public class SimApiStorage
|
||||||
{
|
{
|
||||||
private MinioClient Mc { get; }
|
private MinioClient Mc { get; }
|
||||||
|
|
||||||
|
public MinioClient Client => Mc;
|
||||||
|
|
||||||
private string ServeUrl { get; }
|
private string ServeUrl { get; }
|
||||||
private string Bucket { get; }
|
private string Bucket { get; }
|
||||||
private IHttpContextAccessor HttpContextAccessor { get; }
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
||||||
|
|||||||
@@ -37,5 +37,12 @@ namespace SimApi.Helpers
|
|||||||
|
|
||||||
return strbul.ToString();
|
return strbul.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Log(string message, string type = "Information")
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[ SimApi ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {type} ]");
|
||||||
|
Console.WriteLine(message);
|
||||||
|
Console.WriteLine("");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace SimApi.Models
|
||||||
|
{
|
||||||
|
public class SimApiBaseModel
|
||||||
|
{
|
||||||
|
protected virtual string[] MapperIgnoreField { get; set; } = {"Id", "CreatedAt", "UpdatedAt"};
|
||||||
|
protected virtual string UpdatedTimeField { get; set; } = "UpdatedAt";
|
||||||
|
|
||||||
|
public void MapData<TS>(TS source, bool mapAll = false)
|
||||||
|
{
|
||||||
|
//获取要赋值的源数据不为null的项目
|
||||||
|
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
||||||
|
.Select(x => new {x.Name, x.PropertyType})
|
||||||
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
|
||||||
|
//获取目标对象的属性信息
|
||||||
|
var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType})
|
||||||
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
foreach (var sp in sourceProps)
|
||||||
|
{
|
||||||
|
//检查源对象不为空的属性是否在目标对象中存在
|
||||||
|
if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
|
||||||
|
(!MapperIgnoreField.Contains(sp.Key) || mapAll))
|
||||||
|
{
|
||||||
|
GetType().GetProperty(sp.Key)?
|
||||||
|
.SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MapData<TS>(TS source, string[] mapFields)
|
||||||
|
{
|
||||||
|
//获取要赋值的源数据不为null的项目
|
||||||
|
var sourceProps = source.GetType().GetProperties().Where(x => x.GetValue(source) != null)
|
||||||
|
.Select(x => new {x.Name, x.PropertyType})
|
||||||
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
|
||||||
|
//获取目标对象的属性信息
|
||||||
|
var targetProps = GetType().GetProperties().Select(x => new {x.Name, x.PropertyType})
|
||||||
|
.ToDictionary(x => x.Name, x => x.PropertyType);
|
||||||
|
foreach (var sp in sourceProps)
|
||||||
|
{
|
||||||
|
//检查源对象不为空的属性是否在目标对象中存在
|
||||||
|
if (targetProps.ContainsKey(sp.Key) && sp.Value == targetProps[sp.Key] &&
|
||||||
|
mapFields.Contains(sp.Key))
|
||||||
|
{
|
||||||
|
GetType().GetProperty(sp.Key)?
|
||||||
|
.SetValue(this, source.GetType().GetProperty(sp.Key)?.GetValue(source));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新update时间
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public void UpdateTime()
|
||||||
|
{
|
||||||
|
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
-6
@@ -27,6 +27,12 @@ namespace SimApi
|
|||||||
builder.AddScoped<SimApiAuth>();
|
builder.AddScoped<SimApiAuth>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (simApiOptions.EnableCors)
|
||||||
|
{
|
||||||
|
builder.AddCors(cors => cors.AddPolicy("any",
|
||||||
|
policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));
|
||||||
|
}
|
||||||
|
|
||||||
// 使用SimApiDoc
|
// 使用SimApiDoc
|
||||||
if (simApiOptions.EnableSimApiDoc)
|
if (simApiOptions.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
@@ -138,6 +144,7 @@ namespace SimApi
|
|||||||
|
|
||||||
if (simApiOptions.EnableSimApiStorage)
|
if (simApiOptions.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
|
builder.AddHttpContextAccessor();
|
||||||
builder.AddSingleton<SimApiStorage>();
|
builder.AddSingleton<SimApiStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,15 +160,29 @@ namespace SimApi
|
|||||||
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var options = builder.ApplicationServices.GetService<SimApiOptions>();
|
var options = builder.ApplicationServices.GetService<SimApiOptions>();
|
||||||
|
if (options.EnableForwardHeaders)
|
||||||
|
{
|
||||||
|
SimApiUtil.Log("开始配置ForwardedHeaders...");
|
||||||
|
builder.UseForwardedHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.EnableCors)
|
||||||
|
{
|
||||||
|
SimApiUtil.Log("开始配置Cors全部允许...");
|
||||||
|
builder.UseCors("any");
|
||||||
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiAuth)
|
if (options.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
|
SimApiUtil.Log("开始配置SimApiAuth...");
|
||||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiDoc)
|
if (options.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
|
SimApiUtil.Log("开始配置SimApiDoc...");
|
||||||
var docOptions = options.SimApiDocOptions;
|
var docOptions = options.SimApiDocOptions;
|
||||||
return builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
||||||
{
|
{
|
||||||
x.DocumentTitle = docOptions.DocumentTitle;
|
x.DocumentTitle = docOptions.DocumentTitle;
|
||||||
foreach (var group in docOptions.ApiGroups)
|
foreach (var group in docOptions.ApiGroups)
|
||||||
@@ -177,17 +198,20 @@ namespace SimApi
|
|||||||
|
|
||||||
if (options.EnableSimApiException)
|
if (options.EnableSimApiException)
|
||||||
{
|
{
|
||||||
|
SimApiUtil.Log("开始配置SimApiException...");
|
||||||
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableForwardHeaders)
|
//请求一下检测存储错误
|
||||||
|
if (options.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
builder.UseForwardedHeaders();
|
SimApiUtil.Log("开始配置SimApiStorage...");
|
||||||
|
builder.ApplicationServices.GetService<SimApiStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
//请求一下检测存储错误
|
if (options.EnableLowerUrl)
|
||||||
if(options.EnableSimApiStorage){
|
{
|
||||||
builder.ApplicationServices.GetService<SimApiStorage>();
|
SimApiUtil.Log("开始配置使用URL小写...");
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user