分页修改了,不在使用单独的分页返回,httpclient 不需要simaporesponse的包装,报错直接抛出异常

This commit is contained in:
2025-12-18 14:06:11 +08:00
parent b04a7c86d7
commit d5eb7fe081
2 changed files with 18 additions and 16 deletions
+2 -10
View File
@@ -46,23 +46,15 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
} }
/// <summary> /// <summary>
/// 动态内容分页 /// 分页内容返回
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public class SimApiBasePageResponse<T>() : SimApiBaseResponse public class PageResponse<T>
{ {
public T? List { get; set; } public T? List { get; set; }
public int Page { get; set; } = 1; public int Page { get; set; } = 1;
public int Count { get; set; } = 20; public int Count { get; set; } = 20;
public int Total { get; set; } 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> /// <summary>
+16 -6
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Json; using System.Net.Http.Json;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Exceptions;
namespace SimApi.Helpers; namespace SimApi.Helpers;
@@ -37,9 +38,7 @@ public class SimApiHttpClient(string? appId, string appKey)
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}"); .Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
} }
var http = new HttpClient(); return Query<T>(path, body);
var resp = http.PostAsJsonAsync(path, body).Result;
return resp.Content.ReadFromJsonAsync<T>().Result;
} }
public T? AesQuery<T>(string url, object body) public T? AesQuery<T>(string url, object body)
@@ -54,9 +53,7 @@ public class SimApiHttpClient(string? appId, string appKey)
{ {
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey) Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
}; };
var http = new HttpClient(); return Query<T>(url, req);
var resp = http.PostAsJsonAsync(url, req).Result;
return resp.Content.ReadFromJsonAsync<T>().Result;
} }
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null) public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
@@ -67,4 +64,17 @@ public class SimApiHttpClient(string? appId, string appKey)
}; };
return SignQuery<T>(url, req, queries); return SignQuery<T>(url, req, queries);
} }
private T? Query<T>(string url, object? req)
{
var http = new HttpClient();
var resp = http.PostAsJsonAsync(url, req).Result;
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;
}
} }