upgrade to c#12
This commit is contained in:
@@ -5,24 +5,21 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
|||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
namespace SimApi.Attributes
|
namespace SimApi.Attributes;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测登录中间件
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||||
|
public class SimApiAuthAttribute : ActionFilterAttribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 检测登录中间件
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
|
||||||
public class SimApiAuthAttribute : ActionFilterAttribute
|
|
||||||
{
|
|
||||||
private string[] Types { get; }
|
private string[] Types { get; }
|
||||||
|
|
||||||
|
|
||||||
//默认是user登录类型
|
//默认是user登录类型
|
||||||
public SimApiAuthAttribute()
|
public SimApiAuthAttribute()
|
||||||
{
|
{
|
||||||
Types = new[]
|
Types = new[] { "user" };
|
||||||
{
|
|
||||||
"user"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//只检测一种用户类型的快捷方式
|
//只检测一种用户类型的快捷方式
|
||||||
@@ -40,10 +37,7 @@ namespace SimApi.Attributes
|
|||||||
//只检测一种用户类型的快捷方式
|
//只检测一种用户类型的快捷方式
|
||||||
public SimApiAuthAttribute(string type, string url)
|
public SimApiAuthAttribute(string type, string url)
|
||||||
{
|
{
|
||||||
Types = new[]
|
Types = new[] { type };
|
||||||
{
|
|
||||||
type
|
|
||||||
};
|
|
||||||
new HttpPostAttribute(url);
|
new HttpPostAttribute(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,5 +55,4 @@ namespace SimApi.Attributes
|
|||||||
throw new SimApiException(403);
|
throw new SimApiException(403);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using Swashbuckle.AspNetCore.Annotations;
|
using Swashbuckle.AspNetCore.Annotations;
|
||||||
|
|
||||||
namespace SimApi.Attributes
|
namespace SimApi.Attributes;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 快捷自定义接口文档类
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||||
|
public class SimApiDocAttribute : SwaggerOperationAttribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 快捷自定义接口文档类
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
|
||||||
public class SimApiDocAttribute : SwaggerOperationAttribute
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义接口说明
|
/// 定义接口说明
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -21,5 +21,4 @@ namespace SimApi.Attributes
|
|||||||
// Consumes = new[] {"application/json"};
|
// Consumes = new[] {"application/json"};
|
||||||
// Produces = new[] {"application/json"};
|
// Produces = new[] {"application/json"};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,22 @@
|
|||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 只有ID的请求
|
|
||||||
/// </summary>
|
|
||||||
public record SimApiIdOnlyRequest(int Id);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 只有ID的请求(字符串)
|
/// 只有ID的请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SimApiStringIdOnlyRequest(string Id);
|
public record SimApiIdOnlyRequest(int Id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态类型单字段请求
|
/// 只有ID的请求(字符串)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
public record SimApiStringIdOnlyRequest(string Id);
|
||||||
public record SimApiOneFieldRequest<T>(T Data);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 基础分页请求
|
/// 动态类型单字段请求
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SimApiBasePageRequest(int Page, int Count);
|
/// <typeparam name="T"></typeparam>
|
||||||
}
|
public record SimApiOneFieldRequest<T>(T Data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 基础分页请求
|
||||||
|
/// </summary>
|
||||||
|
public record SimApiBasePageRequest(int Page, int Count);
|
||||||
@@ -2,39 +2,25 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 基础相应
|
||||||
|
/// </summary>
|
||||||
|
public record SimApiBaseResponse(int Code = 200, string Message = "成功")
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 基础相应
|
|
||||||
/// </summary>
|
|
||||||
public record SimApiBaseResponse(int Code = 200, string Message = "成功")
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 默认错误代码对应提示信息
|
/// 默认错误代码对应提示信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Dictionary<int, string> MsgBox = new()
|
private static readonly Dictionary<int, string> MsgBox = new()
|
||||||
{
|
{
|
||||||
{
|
{ 200, "成功" },
|
||||||
200, "成功"
|
{ 204, "没有数据" },
|
||||||
},
|
{ 400, "参数错误" },
|
||||||
{
|
{ 401, "需要登录" },
|
||||||
204, "没有数据"
|
{ 403, "无权访问" },
|
||||||
},
|
{ 404, "接口不存在" },
|
||||||
{
|
{ 500, "服务器错误" }
|
||||||
400, "参数错误"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
401, "需要登录"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
403, "无权访问"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
404, "接口不存在"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
500, "服务器错误"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
||||||
@@ -57,20 +43,23 @@ namespace SimApi.Communications
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态内容分页
|
/// 动态内容分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public record SimApiBasePageResponse<T>
|
public record SimApiBasePageResponse<T>(
|
||||||
(T List, int Page = 1, int Count = 1, int Total = 1, int Code = 200,
|
T List,
|
||||||
|
int Page = 1,
|
||||||
|
int Count = 1,
|
||||||
|
int Total = 1,
|
||||||
|
int Code = 200,
|
||||||
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态Data返回
|
/// 动态Data返回
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
||||||
Message);
|
Message);
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
namespace SimApi.Communications
|
namespace SimApi.Communications;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 登录信息中间件
|
/// 登录信息中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record SimApiLoginItem(string Id, string[] Type);
|
public record SimApiLoginItem(string Id, string[] Type);
|
||||||
}
|
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||||
|
|
||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文档组配置
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiDocGroupOption
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 文档组配置
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiDocGroupOption
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文档标识
|
/// 文档标识
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -23,25 +22,29 @@ namespace SimApi.Configs
|
|||||||
/// 文档描述
|
/// 文档描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 授权配置, Type支持 "SimApiAuth","ClientCredentials","Implicit","AuthorizationCode"
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiAuthOption
|
||||||
|
{
|
||||||
|
public string[] Type { get; set; } = new[] { "SimApiAuth" };
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 授权配置, Type支持 "SimApiAuth","ClientCredentials","Implicit","AuthorizationCode"
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiAuthOption
|
|
||||||
{
|
|
||||||
public string[] Type { get; set; } = new[] {"SimApiAuth"};
|
|
||||||
public string Description { get; set; } = "认证服务器颁发的AccessToken";
|
public string Description { get; set; } = "认证服务器颁发的AccessToken";
|
||||||
public string AuthorizationUrl { get; set; }
|
|
||||||
public string TokenUrl { get; set; }
|
|
||||||
public Dictionary<string, string> Scopes { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
public string AuthorizationUrl { get; set; }
|
||||||
/// 文档配置
|
|
||||||
/// </summary>
|
public string TokenUrl { get; set; }
|
||||||
public class SimApiDocOptions
|
|
||||||
{
|
public Dictionary<string, string> Scopes { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文档配置
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiDocOptions
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文档组配置
|
/// 文档组配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -68,6 +71,5 @@ namespace SimApi.Configs
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接口支持的调用方式
|
/// 接口支持的调用方式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SubmitMethod[] SupportedMethod { get; set; } = new[] {SubmitMethod.Post};
|
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post };
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
|
|
||||||
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
public class SimApiOptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启用全部Cors,对于开发前后分离的时候很有用。
|
/// 启用全部Cors,对于开发前后分离的时候很有用。
|
||||||
/// default: true
|
/// default: true
|
||||||
@@ -89,5 +89,4 @@ namespace SimApi.Configs
|
|||||||
{
|
{
|
||||||
options?.Invoke(SimApiStorageOptions);
|
options?.Invoke(SimApiStorageOptions);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
namespace SimApi.Configs
|
namespace SimApi.Configs;
|
||||||
|
|
||||||
|
public class SimApiStorageOptions
|
||||||
{
|
{
|
||||||
public class SimApiStorageOptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// S3 服务器入口地址
|
/// S3 服务器入口地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -20,5 +20,4 @@ namespace SimApi.Configs
|
|||||||
public string AccessKey { get; set; }
|
public string AccessKey { get; set; }
|
||||||
|
|
||||||
public string SecretKey { get; set; }
|
public string SecretKey { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -4,18 +4,17 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
|||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
using SimApi.Attributes;
|
|
||||||
|
|
||||||
namespace SimApi.Controllers
|
namespace SimApi.Controllers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 基础控制器,所有控制器均继承本控制器
|
||||||
|
/// 1. 自动验证请求参数
|
||||||
|
/// 2. 报错返回
|
||||||
|
/// 3. 错误回馈页面
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiBaseController : Controller
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 基础控制器,所有控制器均继承本控制器
|
|
||||||
/// 1. 自动验证请求参数
|
|
||||||
/// 2. 报错返回
|
|
||||||
/// 3. 错误回馈页面
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiBaseController : Controller
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前登录用户的ID
|
/// 当前登录用户的ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -83,5 +82,4 @@ namespace SimApi.Controllers
|
|||||||
{
|
{
|
||||||
return new SimApiBaseResponse<string>(null);
|
return new SimApiBaseResponse<string>(null);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,13 @@
|
|||||||
using System;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Controllers
|
namespace SimApi.Controllers;
|
||||||
|
|
||||||
|
[ApiExplorerSettings(GroupName = "api")]
|
||||||
|
public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||||
{
|
{
|
||||||
[ApiExplorerSettings(GroupName = "api")]
|
|
||||||
public class YYCommonController : SimApiBaseController
|
|
||||||
{
|
|
||||||
private SimApiAuth Auth { get; }
|
|
||||||
|
|
||||||
public YYCommonController(SimApiAuth auth)
|
|
||||||
{
|
|
||||||
Auth = auth;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 错误回馈页面
|
/// 错误回馈页面
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -53,8 +45,7 @@ namespace SimApi.Controllers
|
|||||||
token = Request.Headers["Token"];
|
token = Request.Headers["Token"];
|
||||||
}
|
}
|
||||||
|
|
||||||
Auth.Logout(token);
|
auth.Logout(token);
|
||||||
return new SimApiBaseResponse();
|
return new SimApiBaseResponse();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
namespace SimApi.Exceptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Api错误捕获异常
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiException : Exception
|
|
||||||
{
|
|
||||||
public int Code { get; }
|
|
||||||
|
|
||||||
public SimApiException(int code, string message = "") : base(message)
|
namespace SimApi.Exceptions;
|
||||||
{
|
|
||||||
Code = code;
|
/// <summary>
|
||||||
}
|
/// Api错误捕获异常
|
||||||
}
|
/// </summary>
|
||||||
|
public class SimApiException(int code, string message = "") : Exception(message)
|
||||||
|
{
|
||||||
|
public int Code { get; } = code;
|
||||||
}
|
}
|
||||||
+9
-21
@@ -1,23 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 认证助手
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiAuth(IDistributedCache cache)
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 认证助手
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiAuth
|
|
||||||
{
|
|
||||||
private IDistributedCache Cache { get; }
|
|
||||||
|
|
||||||
public SimApiAuth(IDistributedCache cache)
|
|
||||||
{
|
|
||||||
Cache = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 产生一个Token记录并返回Token
|
/// 产生一个Token记录并返回Token
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -25,10 +17,7 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Login(string id, string type = "user", string token = null)
|
public string Login(string id, string type = "user", string token = null)
|
||||||
{
|
{
|
||||||
return Login(id, new[]
|
return Login(id, new[] { type }, token);
|
||||||
{
|
|
||||||
type
|
|
||||||
}, token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -41,7 +30,7 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
uuid ??= Guid.NewGuid().ToString();
|
uuid ??= Guid.NewGuid().ToString();
|
||||||
var loginItem = new SimApiLoginItem(id, type);
|
var loginItem = new SimApiLoginItem(id, type);
|
||||||
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +42,7 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(uuid))
|
if (!string.IsNullOrEmpty(uuid))
|
||||||
{
|
{
|
||||||
Cache.Remove(uuid);
|
cache.Remove(uuid);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,13 +2,12 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Minio;
|
using Minio;
|
||||||
using Minio.Exceptions;
|
|
||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
|
public class SimApiStorage
|
||||||
{
|
{
|
||||||
public class SimApiStorage
|
|
||||||
{
|
|
||||||
private MinioClient Mc { get; }
|
private MinioClient Mc { get; }
|
||||||
|
|
||||||
public MinioClient Client => Mc;
|
public MinioClient Client => Mc;
|
||||||
@@ -97,7 +96,6 @@ namespace SimApi.Helpers
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.StartsWith("~") ? url + path.Substring(1, path.Length - 1) : $"{ServeUrl}{path}";
|
return path.StartsWith('~') ? string.Concat(url, path.AsSpan(1, path.Length - 1)) : $"{ServeUrl}{path}";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,10 +7,10 @@ using System.Text.Json.Serialization;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
|
|
||||||
namespace SimApi.Helpers
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
|
public static class SimApiUtil
|
||||||
{
|
{
|
||||||
public static class SimApiUtil
|
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前CST时间
|
/// 当前CST时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -71,5 +71,4 @@ namespace SimApi.Helpers
|
|||||||
{
|
{
|
||||||
return JsonSerializer.Serialize(obj, JsonOption);
|
return JsonSerializer.Serialize(obj, JsonOption);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+4
-12
@@ -2,17 +2,10 @@ using System;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Logger
|
namespace SimApi.Logger;
|
||||||
|
|
||||||
|
public class SimApiLogger(string name) : ILogger
|
||||||
{
|
{
|
||||||
public class SimApiLogger : ILogger
|
|
||||||
{
|
|
||||||
private string Name { get; }
|
|
||||||
|
|
||||||
public SimApiLogger(string name)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDisposable BeginScope<TState>(TState state) => default!;
|
public IDisposable BeginScope<TState>(TState state) => default!;
|
||||||
|
|
||||||
public bool IsEnabled(LogLevel logLevel) => true;
|
public bool IsEnabled(LogLevel logLevel) => true;
|
||||||
@@ -30,12 +23,11 @@ namespace SimApi.Logger
|
|||||||
_ => ConsoleColor.White
|
_ => ConsoleColor.White
|
||||||
};
|
};
|
||||||
var message =
|
var message =
|
||||||
$"[ {Name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
$"[ {name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
{
|
{
|
||||||
message += $"{exception}\n";
|
message += $"{exception}\n";
|
||||||
}
|
}
|
||||||
Console.WriteLine(message);
|
Console.WriteLine(message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace SimApi.Logger
|
namespace SimApi.Logger;
|
||||||
|
|
||||||
|
public class SimApiLoggerProvider : ILoggerProvider
|
||||||
{
|
{
|
||||||
public class SimApiLoggerProvider : ILoggerProvider
|
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers = new();
|
||||||
{
|
|
||||||
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers =
|
|
||||||
new ConcurrentDictionary<string, SimApiLogger>();
|
|
||||||
|
|
||||||
|
|
||||||
public ILogger CreateLogger(string categoryName)
|
public ILogger CreateLogger(string categoryName)
|
||||||
@@ -18,5 +17,4 @@ namespace SimApi.Logger
|
|||||||
{
|
{
|
||||||
_loggers.Clear();
|
_loggers.Clear();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -4,26 +4,19 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.Extensions.Caching.Distributed;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
|
|
||||||
namespace SimApi.Middlewares
|
namespace SimApi.Middlewares;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 认证信息获取中间件
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 认证信息获取中间件
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiAuthMiddleware
|
|
||||||
{
|
|
||||||
private RequestDelegate Next { get; }
|
|
||||||
|
|
||||||
public SimApiAuthMiddleware(RequestDelegate next)
|
|
||||||
{
|
|
||||||
Next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
||||||
{
|
{
|
||||||
string token = null;
|
string token = null;
|
||||||
if (httpContext.Request.Headers.ContainsKey("Token"))
|
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
||||||
{
|
{
|
||||||
token = httpContext.Request.Headers["Token"];
|
token = header;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(token))
|
if (!string.IsNullOrEmpty(token))
|
||||||
@@ -35,7 +28,6 @@ namespace SimApi.Middlewares
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Next(httpContext);
|
return next(httpContext);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,40 +6,27 @@ using SimApi.Communications;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Exceptions;
|
using SimApi.Exceptions;
|
||||||
|
|
||||||
namespace SimApi.Middlewares
|
namespace SimApi.Middlewares;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异常处理中间件
|
||||||
|
/// </summary>
|
||||||
|
public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 异常处理中间件
|
|
||||||
/// </summary>
|
|
||||||
public class SimApiExceptionMiddleware
|
|
||||||
{
|
|
||||||
private RequestDelegate Next { get; }
|
|
||||||
|
|
||||||
private ILogger<SimApiExceptionMiddleware> Log { get; }
|
|
||||||
|
|
||||||
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
|
||||||
{
|
|
||||||
Log = log;
|
|
||||||
Next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task InvokeAsync(HttpContext context)
|
public async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
if (context.Request.Headers.ContainsKey("Query-Id"))
|
if (context.Request.Headers.TryGetValue("Query-Id", out var header))
|
||||||
{
|
{
|
||||||
context.Response.Headers["Query-Id"] = context.Request.Headers["Query-Id"];
|
context.Response.Headers["Query-Id"] = header;
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = new SimApiBaseResponse();
|
SimApiBaseResponse response;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Next(context);
|
await next(context);
|
||||||
if (context.Response.StatusCode != 200)
|
if (context.Response.StatusCode != 200)
|
||||||
{
|
{
|
||||||
if (!new[]
|
if (!new[] { 301, 302 }.Contains(context.Response.StatusCode))
|
||||||
{
|
|
||||||
301, 302
|
|
||||||
}.Contains(context.Response.StatusCode))
|
|
||||||
{
|
{
|
||||||
throw new SimApiException(context.Response.StatusCode);
|
throw new SimApiException(context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
@@ -55,8 +42,8 @@ namespace SimApi.Middlewares
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.LogError(ex.Message);
|
log.LogError(ex.Message);
|
||||||
Log.LogError(ex.StackTrace);
|
log.LogError(ex.StackTrace);
|
||||||
response = new SimApiBaseResponse(500, ex.Message);
|
response = new SimApiBaseResponse(500, ex.Message);
|
||||||
ErrorResponse(context, response);
|
ErrorResponse(context, response);
|
||||||
}
|
}
|
||||||
@@ -72,9 +59,8 @@ namespace SimApi.Middlewares
|
|||||||
if (!context.Response.HasStarted)
|
if (!context.Response.HasStarted)
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = 200;
|
context.Response.StatusCode = 200;
|
||||||
context.Response.Headers.Add("Content-Type", "application/json");
|
context.Response.Headers.Append("Content-Type", "application/json");
|
||||||
context.Response.WriteAsync(response.ToString());
|
context.Response.WriteAsync(response.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.Models
|
namespace SimApi.Models;
|
||||||
|
|
||||||
|
public class SimApiBaseModel
|
||||||
{
|
{
|
||||||
public class SimApiBaseModel
|
|
||||||
{
|
|
||||||
[Column(Order = 1)]
|
[Column(Order = 1)]
|
||||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
@@ -87,5 +86,4 @@ namespace SimApi.Models
|
|||||||
{
|
{
|
||||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+11
-11
@@ -14,27 +14,27 @@
|
|||||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<PackageVersion>5.0.2</PackageVersion>
|
<PackageVersion>5.0.2</PackageVersion>
|
||||||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Helpers\" />
|
<Folder Include="Helpers\"/>
|
||||||
<Folder Include="Communications\" />
|
<Folder Include="Communications\"/>
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Controllers\"/>
|
||||||
<Folder Include="Middlewares\" />
|
<Folder Include="Middlewares\"/>
|
||||||
<Folder Include="Exceptions\" />
|
<Folder Include="Exceptions\"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="4.0.7" />
|
<PackageReference Include="Minio" Version="4.0.7"/>
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="6.6.0" />
|
<PackageReference Include="RabbitMQ.Client" Version="6.6.0"/>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0"/>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
<Properties>
|
<Properties>
|
||||||
<Policies>
|
<Policies>
|
||||||
<DotNetNamingPolicy ResourceNamePolicy="FileFormatDefault" DirectoryNamespaceAssociation="PrefixedHierarchical" />
|
<DotNetNamingPolicy ResourceNamePolicy="FileFormatDefault" DirectoryNamespaceAssociation="PrefixedHierarchical"/>
|
||||||
</Policies>
|
</Policies>
|
||||||
</Properties>
|
</Properties>
|
||||||
</MonoDevelop>
|
</MonoDevelop>
|
||||||
|
|||||||
+6
-7
@@ -9,13 +9,13 @@ using Microsoft.Extensions.Logging;
|
|||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
using SimApi.Logger;
|
using SimApi.Logger;
|
||||||
|
|
||||||
namespace SimApi
|
namespace SimApi;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加入系统的扩展信息
|
||||||
|
/// </summary>
|
||||||
|
public static class SimApiExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 加入系统的扩展信息
|
|
||||||
/// </summary>
|
|
||||||
public static class SimApiExtensions
|
|
||||||
{
|
|
||||||
//**********快捷添加**************
|
//**********快捷添加**************
|
||||||
public static IServiceCollection AddSimApi(this IServiceCollection builder,
|
public static IServiceCollection AddSimApi(this IServiceCollection builder,
|
||||||
Action<SimApiOptions> options = null)
|
Action<SimApiOptions> options = null)
|
||||||
@@ -261,5 +261,4 @@ namespace SimApi
|
|||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user