Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2d30c031d | ||
|
|
9dc4ef7bce | ||
|
|
d5eb7fe081 | ||
|
|
b04a7c86d7 |
@@ -46,23 +46,15 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态内容分页
|
||||
/// 分页内容返回
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class SimApiBasePageResponse<T>() : SimApiBaseResponse
|
||||
public class PageResponse<T>
|
||||
{
|
||||
public T? List { get; set; }
|
||||
public int Page { get; set; } = 1;
|
||||
public int Count { get; set; } = 20;
|
||||
public int Total { get; set; }
|
||||
|
||||
public SimApiBasePageResponse(T list, int page, int count, int total) : this()
|
||||
{
|
||||
List = list;
|
||||
Page = page;
|
||||
Count = count;
|
||||
Total = total;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -4,10 +4,11 @@ using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Exceptions;
|
||||
|
||||
namespace SimApi.Helpers;
|
||||
|
||||
public class SimApiHttpClient(string? appId, string appKey)
|
||||
public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
|
||||
{
|
||||
public string Server { get; init; } = string.Empty;
|
||||
public string SignName { get; init; } = "sign";
|
||||
@@ -16,8 +17,7 @@ public class SimApiHttpClient(string? appId, string appKey)
|
||||
public string? AppIdName { get; init; } = "appId";
|
||||
public string[] SignFields { get; init; } = [];
|
||||
|
||||
|
||||
public T? SignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
||||
public T? SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
||||
{
|
||||
url = Server + url;
|
||||
var queryUrl = SignFields.Aggregate(string.Empty,
|
||||
@@ -37,9 +37,7 @@ public class SimApiHttpClient(string? appId, string appKey)
|
||||
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
|
||||
}
|
||||
|
||||
var http = new HttpClient();
|
||||
var resp = http.PostAsJsonAsync(path, body).Result;
|
||||
return resp.Content.ReadFromJsonAsync<T>().Result;
|
||||
return Query<T>(path, body);
|
||||
}
|
||||
|
||||
public T? AesQuery<T>(string url, object body)
|
||||
@@ -54,9 +52,7 @@ public class SimApiHttpClient(string? appId, string appKey)
|
||||
{
|
||||
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
|
||||
};
|
||||
var http = new HttpClient();
|
||||
var resp = http.PostAsJsonAsync(url, req).Result;
|
||||
return resp.Content.ReadFromJsonAsync<T>().Result;
|
||||
return Query<T>(url, req);
|
||||
}
|
||||
|
||||
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
||||
@@ -67,4 +63,27 @@ public class SimApiHttpClient(string? appId, string appKey)
|
||||
};
|
||||
return SignQuery<T>(url, req, queries);
|
||||
}
|
||||
|
||||
private T? Query<T>(string url, object? req)
|
||||
{
|
||||
var http = new HttpClient();
|
||||
if (debug)
|
||||
{
|
||||
Console.WriteLine($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
||||
}
|
||||
|
||||
var resp = http.PostAsJsonAsync(url, req).Result;
|
||||
if (debug)
|
||||
{
|
||||
Console.WriteLine($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n");
|
||||
}
|
||||
|
||||
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
|
||||
if (res == null)
|
||||
{
|
||||
throw new SimApiException(500, "请求发生错误");
|
||||
}
|
||||
|
||||
return res.Code != 200 ? throw new SimApiException(res.Code, res.Message) : res.Data;
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -122,12 +122,12 @@ public static class SimApiExtensions
|
||||
Description = group.Description
|
||||
});
|
||||
}
|
||||
|
||||
x.CustomSchemaIds(type => type.FullName?.Replace("+", "."));
|
||||
x.OperationFilter<SimApiResponseOperationFilter>();
|
||||
x.OperationFilter<SimApiSignOperationFilter>();
|
||||
x.OperationFilter<AesBodyOperationFilter>();
|
||||
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
|
||||
x.DocumentFilter<RemoveEmptyTagsFilter>();
|
||||
if (simApiOptions.EnableSimApiAuth)
|
||||
{
|
||||
x.OperationFilter<SimApiAuthOperationFilter>();
|
||||
@@ -392,7 +392,6 @@ public static class SimApiExtensions
|
||||
x.SwaggerEndpoint($"/swagger/{group.Id}.json", name: group.Name);
|
||||
}
|
||||
|
||||
x.EnableValidator();
|
||||
x.SupportedSubmitMethods(docOptions.SupportedMethod);
|
||||
x.DisplayRequestDuration();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Linq;
|
||||
|
||||
namespace SimApi.SwaggerFilters;
|
||||
|
||||
public class RemoveEmptyTagsFilter : IDocumentFilter
|
||||
{
|
||||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
|
||||
{
|
||||
// 步骤1:收集所有有接口的 Tag 名称
|
||||
var tagsWithOperations = swaggerDoc.Paths.Values
|
||||
.SelectMany(path => path.Operations.Values)
|
||||
.SelectMany(op => op.Tags.Select(t => t.Name))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
// 步骤2:移除无接口的空 Tag
|
||||
var emptyTags = swaggerDoc.Tags.Where(t => !tagsWithOperations.Contains(t.Name)).ToList();
|
||||
foreach (var tag in emptyTags)
|
||||
{
|
||||
swaggerDoc.Tags.Remove(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user