增加ImageCache微信 图片缓存
This commit is contained in:
1 parent
d6cb5b966b
commit
d0f9f00b76
15 files changed
+518
-30
No files matched your search
@@ -16,37 +16,31 @@ namespace MAX.Framework.Commons
|
||||
{
|
||||
if (context.Result is ObjectResult objResult)
|
||||
{
|
||||
if (!IsAlreadyWrapped(objResult.Value))
|
||||
if (objResult.Value is ApiResultBase)
|
||||
{
|
||||
objResult.StatusCode = 200;
|
||||
objResult.DeclaredType = objResult.Value.GetType();
|
||||
return;
|
||||
}
|
||||
|
||||
// 非成功响应交由状态码中间件处理,避免先包装成业务成功。
|
||||
var statusCode = objResult.StatusCode ?? 200;
|
||||
if (statusCode >= 200 && statusCode < 300)
|
||||
{
|
||||
var apiResult = ApiResult<object>.Ok(objResult.Value);
|
||||
|
||||
objResult.Value = apiResult;
|
||||
objResult.DeclaredType = null;
|
||||
objResult.StatusCode = 200;
|
||||
}
|
||||
}
|
||||
else if (context.Result is EmptyResult || context.Result is NoContentResult)
|
||||
{
|
||||
context.Result = new ObjectResult(ApiResult.Ok());
|
||||
context.Result = new ObjectResult(ApiResult.Ok()) { StatusCode = 200 };
|
||||
}
|
||||
}
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext context) { }
|
||||
private static bool IsAlreadyWrapped(object? value)
|
||||
{
|
||||
if (value is null) return false;
|
||||
|
||||
var type = value.GetType();
|
||||
|
||||
// 递归判断:只要类型链上有任何一层是 ApiResult<> 的开放泛型定义,或直接是 ApiResult,就算已包装
|
||||
while (type is not null)
|
||||
{
|
||||
if (type == typeof(ApiResult)) return true;
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ApiResult<>)) return true;
|
||||
type = type.BaseType;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,7 +59,7 @@ namespace MAX.Framework.Commons
|
||||
}
|
||||
|
||||
var originalBody = context.Response.Body;
|
||||
var buffer = _streamManager.GetStream();
|
||||
using var buffer = _streamManager.GetStream();
|
||||
|
||||
context.Response.Body = buffer;
|
||||
|
||||
@@ -126,7 +126,6 @@ namespace MAX.Framework.Commons
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
|
||||
buffer.Dispose();
|
||||
}
|
||||
|
||||
private static bool IsPassthroughByRequest(HttpContext context)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using MAX.Framework.Commons;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MAX.Framework.Dtos
|
||||
{
|
||||
@@ -29,10 +29,10 @@ namespace MAX.Framework.Dtos
|
||||
public abstract class ApiResultBase
|
||||
{
|
||||
/// <summary>业务状态码</summary>
|
||||
public int Code { get; set; } = ResultCode.Success;
|
||||
public int Code { get; init; } = ResultCode.Success;
|
||||
|
||||
/// <summary>提示信息</summary>
|
||||
public string? Msg { get; set; }
|
||||
public string? Msg { get; init; }
|
||||
|
||||
/// <summary>请求是否成功</summary>
|
||||
public bool Success => Code == ResultCode.Success;
|
||||
@@ -44,13 +44,25 @@ namespace MAX.Framework.Dtos
|
||||
/// </summary>
|
||||
public class ApiResult<T> : ApiResultBase
|
||||
{
|
||||
public T? Data { get; set; }
|
||||
/// <summary>响应数据;失败时不应依赖该值,值类型默认值并非 null。</summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
|
||||
public T? Data { get; init; }
|
||||
|
||||
/// <summary>将无数据响应转换为当前数据类型,保留状态码和提示信息。</summary>
|
||||
public static implicit operator ApiResult<T>(ApiResult result)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(result);
|
||||
return new ApiResult<T> { Code = result.Code, Msg = result.Msg, Data = default };
|
||||
}
|
||||
|
||||
public static ApiResult<T> Ok(T data, string msg = "操作成功")
|
||||
=> new ApiResult<T> { Code = ResultCode.Success, Data = data, Msg = msg };
|
||||
|
||||
public static ApiResult<T> Fail(string msg = "操作失败", int code = ResultCode.Fail, T? data = default)
|
||||
=> new ApiResult<T> { Code = code, Msg = msg, Data = data };
|
||||
{
|
||||
ApiResult.ValidateFailureCode(code);
|
||||
return new ApiResult<T> { Code = code, Msg = msg, Data = data };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -58,11 +70,32 @@ namespace MAX.Framework.Dtos
|
||||
/// </summary>
|
||||
public class ApiResult : ApiResultBase
|
||||
{
|
||||
public static ApiResult Ok(string msg = "操作成功")
|
||||
=> new ApiResult { Code = ResultCode.Success, Msg = msg };
|
||||
/// <summary>无数据响应仍输出 data: null,保持统一的响应字段。</summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
|
||||
public object? Data => null;
|
||||
|
||||
/// <summary>创建带数据的成功响应,自动推断数据类型。</summary>
|
||||
public static ApiResult<T> Ok<T>(T data, string msg = "操作成功")
|
||||
=> ApiResult<T>.Ok(data, msg);
|
||||
|
||||
/// <summary>创建带数据的失败响应,自动推断数据类型。</summary>
|
||||
public static ApiResult<T> Fail<T>(string msg, T data, int code = ResultCode.Fail)
|
||||
=> ApiResult<T>.Fail(msg, code, data);
|
||||
|
||||
public static ApiResult Ok()
|
||||
=> new ApiResult { Code = ResultCode.Success, Msg = "操作成功" };
|
||||
|
||||
public static ApiResult Fail(string msg = "操作失败", int code = ResultCode.Fail)
|
||||
=> new ApiResult { Code = code, Msg = msg };
|
||||
{
|
||||
ValidateFailureCode(code);
|
||||
return new ApiResult { Code = code, Msg = msg };
|
||||
}
|
||||
|
||||
internal static void ValidateFailureCode(int code)
|
||||
{
|
||||
if (code == ResultCode.Success)
|
||||
throw new ArgumentOutOfRangeException(nameof(code), code, "失败响应不能使用成功状态码。");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user