upgrade to c#12
This commit is contained in:
@@ -5,8 +5,8 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Exceptions;
|
||||
|
||||
namespace SimApi.Attributes
|
||||
{
|
||||
namespace SimApi.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// 检测登录中间件
|
||||
/// </summary>
|
||||
@@ -19,10 +19,7 @@ namespace SimApi.Attributes
|
||||
//默认是user登录类型
|
||||
public SimApiAuthAttribute()
|
||||
{
|
||||
Types = new[]
|
||||
{
|
||||
"user"
|
||||
};
|
||||
Types = new[] { "user" };
|
||||
}
|
||||
|
||||
//只检测一种用户类型的快捷方式
|
||||
@@ -40,10 +37,7 @@ namespace SimApi.Attributes
|
||||
//只检测一种用户类型的快捷方式
|
||||
public SimApiAuthAttribute(string type, string url)
|
||||
{
|
||||
Types = new[]
|
||||
{
|
||||
type
|
||||
};
|
||||
Types = new[] { type };
|
||||
new HttpPostAttribute(url);
|
||||
}
|
||||
|
||||
@@ -62,4 +56,3 @@ namespace SimApi.Attributes
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
|
||||
namespace SimApi.Attributes
|
||||
{
|
||||
namespace SimApi.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// 快捷自定义接口文档类
|
||||
/// </summary>
|
||||
@@ -22,4 +22,3 @@ namespace SimApi.Attributes
|
||||
// Produces = new[] {"application/json"};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace SimApi.Communications
|
||||
{
|
||||
namespace SimApi.Communications;
|
||||
|
||||
/// <summary>
|
||||
/// 只有ID的请求
|
||||
/// </summary>
|
||||
@@ -20,4 +20,3 @@
|
||||
/// 基础分页请求
|
||||
/// </summary>
|
||||
public record SimApiBasePageRequest(int Page, int Count);
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SimApi.Communications
|
||||
{
|
||||
namespace SimApi.Communications;
|
||||
|
||||
/// <summary>
|
||||
/// 基础相应
|
||||
/// </summary>
|
||||
@@ -14,27 +14,13 @@ namespace SimApi.Communications
|
||||
/// </summary>
|
||||
private static readonly Dictionary<int, string> MsgBox = new()
|
||||
{
|
||||
{
|
||||
200, "成功"
|
||||
},
|
||||
{
|
||||
204, "没有数据"
|
||||
},
|
||||
{
|
||||
400, "参数错误"
|
||||
},
|
||||
{
|
||||
401, "需要登录"
|
||||
},
|
||||
{
|
||||
403, "无权访问"
|
||||
},
|
||||
{
|
||||
404, "接口不存在"
|
||||
},
|
||||
{
|
||||
500, "服务器错误"
|
||||
}
|
||||
{ 200, "成功" },
|
||||
{ 204, "没有数据" },
|
||||
{ 400, "参数错误" },
|
||||
{ 401, "需要登录" },
|
||||
{ 403, "无权访问" },
|
||||
{ 404, "接口不存在" },
|
||||
{ 500, "服务器错误" }
|
||||
};
|
||||
|
||||
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
||||
@@ -63,8 +49,12 @@ namespace SimApi.Communications
|
||||
/// 动态内容分页
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public record SimApiBasePageResponse<T>
|
||||
(T List, int Page = 1, int Count = 1, int Total = 1, int Code = 200,
|
||||
public record SimApiBasePageResponse<T>(
|
||||
T List,
|
||||
int Page = 1,
|
||||
int Count = 1,
|
||||
int Total = 1,
|
||||
int Code = 200,
|
||||
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
||||
|
||||
/// <summary>
|
||||
@@ -73,4 +63,3 @@ namespace SimApi.Communications
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
||||
Message);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace SimApi.Communications
|
||||
{
|
||||
namespace SimApi.Communications;
|
||||
|
||||
/// <summary>
|
||||
/// 登录信息中间件
|
||||
/// </summary>
|
||||
public record SimApiLoginItem(string Id, string[] Type);
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
|
||||
namespace SimApi.Configs
|
||||
{
|
||||
namespace SimApi.Configs;
|
||||
|
||||
/// <summary>
|
||||
/// 文档组配置
|
||||
/// </summary>
|
||||
@@ -31,9 +30,13 @@ namespace SimApi.Configs
|
||||
public class SimApiAuthOption
|
||||
{
|
||||
public string[] Type { get; set; } = new[] { "SimApiAuth" };
|
||||
|
||||
public string Description { get; set; } = "认证服务器颁发的AccessToken";
|
||||
|
||||
public string AuthorizationUrl { get; set; }
|
||||
|
||||
public string TokenUrl { get; set; }
|
||||
|
||||
public Dictionary<string, string> Scopes { get; set; }
|
||||
}
|
||||
|
||||
@@ -70,4 +73,3 @@ namespace SimApi.Configs
|
||||
/// </summary>
|
||||
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post };
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
|
||||
namespace SimApi.Configs
|
||||
{
|
||||
namespace SimApi.Configs;
|
||||
|
||||
public class SimApiOptions
|
||||
{
|
||||
/// <summary>
|
||||
@@ -90,4 +90,3 @@ namespace SimApi.Configs
|
||||
options?.Invoke(SimApiStorageOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace SimApi.Configs
|
||||
{
|
||||
namespace SimApi.Configs;
|
||||
|
||||
public class SimApiStorageOptions
|
||||
{
|
||||
/// <summary>
|
||||
@@ -21,4 +21,3 @@ namespace SimApi.Configs
|
||||
|
||||
public string SecretKey { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,9 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Linq;
|
||||
using SimApi.Exceptions;
|
||||
using SimApi.Attributes;
|
||||
|
||||
namespace SimApi.Controllers
|
||||
{
|
||||
namespace SimApi.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 基础控制器,所有控制器均继承本控制器
|
||||
/// 1. 自动验证请求参数
|
||||
@@ -84,4 +83,3 @@ namespace SimApi.Controllers
|
||||
return new SimApiBaseResponse<string>(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,13 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SimApi.Attributes;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Controllers
|
||||
{
|
||||
namespace SimApi.Controllers;
|
||||
|
||||
[ApiExplorerSettings(GroupName = "api")]
|
||||
public class YYCommonController : SimApiBaseController
|
||||
public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||
{
|
||||
private SimApiAuth Auth { get; }
|
||||
|
||||
public YYCommonController(SimApiAuth auth)
|
||||
{
|
||||
Auth = auth;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 错误回馈页面
|
||||
/// </summary>
|
||||
@@ -53,8 +45,7 @@ namespace SimApi.Controllers
|
||||
token = Request.Headers["Token"];
|
||||
}
|
||||
|
||||
Auth.Logout(token);
|
||||
auth.Logout(token);
|
||||
return new SimApiBaseResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,11 @@
|
||||
using System;
|
||||
namespace SimApi.Exceptions
|
||||
{
|
||||
|
||||
namespace SimApi.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Api错误捕获异常
|
||||
/// </summary>
|
||||
public class SimApiException : Exception
|
||||
public class SimApiException(int code, string message = "") : Exception(message)
|
||||
{
|
||||
public int Code { get; }
|
||||
|
||||
public SimApiException(int code, string message = "") : base(message)
|
||||
{
|
||||
Code = code;
|
||||
}
|
||||
}
|
||||
public int Code { get; } = code;
|
||||
}
|
||||
+6
-18
@@ -1,23 +1,15 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using SimApi.Communications;
|
||||
|
||||
namespace SimApi.Helpers
|
||||
{
|
||||
namespace SimApi.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// 认证助手
|
||||
/// </summary>
|
||||
public class SimApiAuth
|
||||
public class SimApiAuth(IDistributedCache cache)
|
||||
{
|
||||
private IDistributedCache Cache { get; }
|
||||
|
||||
public SimApiAuth(IDistributedCache cache)
|
||||
{
|
||||
Cache = cache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 产生一个Token记录并返回Token
|
||||
/// </summary>
|
||||
@@ -25,10 +17,7 @@ namespace SimApi.Helpers
|
||||
/// <returns></returns>
|
||||
public string Login(string id, string type = "user", string token = null)
|
||||
{
|
||||
return Login(id, new[]
|
||||
{
|
||||
type
|
||||
}, token);
|
||||
return Login(id, new[] { type }, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -41,7 +30,7 @@ namespace SimApi.Helpers
|
||||
{
|
||||
uuid ??= Guid.NewGuid().ToString();
|
||||
var loginItem = new SimApiLoginItem(id, type);
|
||||
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
||||
cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@@ -53,8 +42,7 @@ namespace SimApi.Helpers
|
||||
{
|
||||
if (!string.IsNullOrEmpty(uuid))
|
||||
{
|
||||
Cache.Remove(uuid);
|
||||
}
|
||||
cache.Remove(uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,10 @@
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Minio;
|
||||
using Minio.Exceptions;
|
||||
using SimApi.Configs;
|
||||
|
||||
namespace SimApi.Helpers
|
||||
{
|
||||
namespace SimApi.Helpers;
|
||||
|
||||
public class SimApiStorage
|
||||
{
|
||||
private MinioClient Mc { get; }
|
||||
@@ -97,7 +96,6 @@ namespace SimApi.Helpers
|
||||
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,8 +7,8 @@ using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text.Unicode;
|
||||
|
||||
namespace SimApi.Helpers
|
||||
{
|
||||
namespace SimApi.Helpers;
|
||||
|
||||
public static class SimApiUtil
|
||||
{
|
||||
/// <summary>
|
||||
@@ -72,4 +72,3 @@ namespace SimApi.Helpers
|
||||
return JsonSerializer.Serialize(obj, JsonOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-11
@@ -2,17 +2,10 @@ using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Logger
|
||||
{
|
||||
public class SimApiLogger : ILogger
|
||||
{
|
||||
private string Name { get; }
|
||||
namespace SimApi.Logger;
|
||||
|
||||
public SimApiLogger(string name)
|
||||
public class SimApiLogger(string name) : ILogger
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state) => default!;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
@@ -30,7 +23,7 @@ namespace SimApi.Logger
|
||||
_ => ConsoleColor.White
|
||||
};
|
||||
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)
|
||||
{
|
||||
message += $"{exception}\n";
|
||||
@@ -38,4 +31,3 @@ namespace SimApi.Logger
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SimApi.Logger
|
||||
{
|
||||
namespace SimApi.Logger;
|
||||
|
||||
public class SimApiLoggerProvider : ILoggerProvider
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers =
|
||||
new ConcurrentDictionary<string, SimApiLogger>();
|
||||
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers = new();
|
||||
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
@@ -19,4 +18,3 @@ namespace SimApi.Logger
|
||||
_loggers.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,26 +4,19 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using SimApi.Communications;
|
||||
|
||||
namespace SimApi.Middlewares
|
||||
{
|
||||
namespace SimApi.Middlewares;
|
||||
|
||||
/// <summary>
|
||||
/// 认证信息获取中间件
|
||||
/// </summary>
|
||||
public class SimApiAuthMiddleware
|
||||
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||
{
|
||||
private RequestDelegate Next { get; }
|
||||
|
||||
public SimApiAuthMiddleware(RequestDelegate next)
|
||||
{
|
||||
Next = next;
|
||||
}
|
||||
|
||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
||||
{
|
||||
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))
|
||||
@@ -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 SimApi.Exceptions;
|
||||
|
||||
namespace SimApi.Middlewares
|
||||
{
|
||||
namespace SimApi.Middlewares;
|
||||
|
||||
/// <summary>
|
||||
/// 异常处理中间件
|
||||
/// </summary>
|
||||
public class SimApiExceptionMiddleware
|
||||
public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
{
|
||||
await Next(context);
|
||||
await next(context);
|
||||
if (context.Response.StatusCode != 200)
|
||||
{
|
||||
if (!new[]
|
||||
{
|
||||
301, 302
|
||||
}.Contains(context.Response.StatusCode))
|
||||
if (!new[] { 301, 302 }.Contains(context.Response.StatusCode))
|
||||
{
|
||||
throw new SimApiException(context.Response.StatusCode);
|
||||
}
|
||||
@@ -55,8 +42,8 @@ namespace SimApi.Middlewares
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.LogError(ex.Message);
|
||||
Log.LogError(ex.StackTrace);
|
||||
log.LogError(ex.Message);
|
||||
log.LogError(ex.StackTrace);
|
||||
response = new SimApiBaseResponse(500, ex.Message);
|
||||
ErrorResponse(context, response);
|
||||
}
|
||||
@@ -72,9 +59,8 @@ namespace SimApi.Middlewares
|
||||
if (!context.Response.HasStarted)
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Models
|
||||
{
|
||||
namespace SimApi.Models;
|
||||
|
||||
public class SimApiBaseModel
|
||||
{
|
||||
[Column(Order = 1)]
|
||||
@@ -88,4 +87,3 @@ namespace SimApi.Models
|
||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageVersion>5.0.2</PackageVersion>
|
||||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
+2
-3
@@ -9,8 +9,8 @@ using Microsoft.Extensions.Logging;
|
||||
using SimApi.Configs;
|
||||
using SimApi.Logger;
|
||||
|
||||
namespace SimApi
|
||||
{
|
||||
namespace SimApi;
|
||||
|
||||
/// <summary>
|
||||
/// 加入系统的扩展信息
|
||||
/// </summary>
|
||||
@@ -262,4 +262,3 @@ namespace SimApi
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user