Compare commits

..
2 Commits
Author SHA1 Message Date
xrain bbc2806995 add origin response attribute 2024-12-23 20:34:38 +08:00
xrain 404a638d5e add filter 2024-12-23 20:26:21 +08:00
7 changed files with 60 additions and 18 deletions
+8
View File
@@ -0,0 +1,8 @@
using System;
namespace SimApi.Attributes;
[AttributeUsage(AttributeTargets.Method)]
public class OriginResponseAttribute : Attribute
{
}
+2 -9
View File
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using SimApi.Helpers;
namespace SimApi.Communications;
@@ -36,15 +37,7 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
/// <returns></returns>
public override string ToString()
{
return JsonSerializer.Serialize(this, new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter()
}
});
return SimApiUtil.Json(this);
}
}
+5
View File
@@ -36,6 +36,11 @@ public class SimApiOptions
/// default: true
/// </summary>
public bool EnableSimApiException { get; set; } = true;
/// <summary>
/// 启用返回结果拦截
/// </summary>
public bool EnableSimApiResponseFilter { get; set; } = true;
/// <summary>
/// 开启S3兼容的存储系统。
+34
View File
@@ -0,0 +1,34 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using SimApi.Attributes;
using SimApi.Communications;
namespace SimApi.Helpers;
public class SimApiResponseFilter : IResultFilter
{
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.ActionDescriptor.EndpointMetadata.Any(meta => meta is OriginResponseAttribute))
{
return;
}
context.Result = context.Result switch
{
// 检查结果是否为 null
null => new OkObjectResult(new SimApiBaseResponse()),
ObjectResult { Value: SimApiBaseResponse simApiBaseResponse } =>
new OkObjectResult(simApiBaseResponse),
ObjectResult objectResult => new OkObjectResult(new SimApiBaseResponse<object>(objectResult.Value!)),
EmptyResult => new OkObjectResult(new SimApiBaseResponse()),
_ => context.Result
};
}
public void OnResultExecuted(ResultExecutedContext context)
{
}
}
+4 -7
View File
@@ -1,5 +1,5 @@
using System;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using SimApi.Communications;
@@ -27,12 +27,11 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
switch (context.Response.StatusCode)
{
case 200:
break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
case 301:
case 302:
break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
default:
throw new SimApiException(context.Response.StatusCode);
}
@@ -42,7 +41,6 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
response = string.IsNullOrEmpty(ex.Message)
? new SimApiBaseResponse(ex.Code)
: new SimApiBaseResponse(ex.Code, ex.Message);
ErrorResponse(context, response);
}
catch (Exception ex)
@@ -61,9 +59,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
/// <param name="response"></param>
private static void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{
if (context.Response.HasStarted) return;
context.Response.StatusCode = 200;
context.Response.Headers.Append("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString());
context.Response.WriteAsync(response.ToString()).Wait();
}
}
-2
View File
@@ -19,9 +19,7 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Helpers\"/>
<Folder Include="Communications\"/>
<Folder Include="Middlewares\"/>
<Folder Include="Exceptions\"/>
</ItemGroup>
<ItemGroup>
+7
View File
@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using SimApi.Helpers;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
@@ -209,6 +210,12 @@ public static class SimApiExtensions
builder.AddSingleton<SimApiStorage>();
}
if (simApiOptions.EnableSimApiException)
{
builder.AddControllers(opt => opt.Filters.Add<SimApiResponseFilter>())
.AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
}
builder.AddSingleton(simApiOptions);
return builder;
}