Skip to content

Commit d5eb7fe

Browse files
committed
分页修改了,不在使用单独的分页返回,httpclient 不需要simaporesponse的包装,报错直接抛出异常
1 parent b04a7c8 commit d5eb7fe

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

Communications/SimApiBaseResponse.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,15 @@ public override string ToString()
4646
}
4747

4848
/// <summary>
49-
/// 动态内容分页
49+
/// 分页内容返回
5050
/// </summary>
5151
/// <typeparam name="T"></typeparam>
52-
public class SimApiBasePageResponse<T>() : SimApiBaseResponse
52+
public class PageResponse<T>
5353
{
5454
public T? List { get; set; }
5555
public int Page { get; set; } = 1;
5656
public int Count { get; set; } = 20;
5757
public int Total { get; set; }
58-
59-
public SimApiBasePageResponse(T list, int page, int count, int total) : this()
60-
{
61-
List = list;
62-
Page = page;
63-
Count = count;
64-
Total = total;
65-
}
6658
}
6759

6860
/// <summary>

Helpers/SimApiHttpClient.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http;
55
using System.Net.Http.Json;
66
using SimApi.Communications;
7+
using SimApi.Exceptions;
78

89
namespace SimApi.Helpers;
910

@@ -37,9 +38,7 @@ public class SimApiHttpClient(string? appId, string appKey)
3738
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
3839
}
3940

40-
var http = new HttpClient();
41-
var resp = http.PostAsJsonAsync(path, body).Result;
42-
return resp.Content.ReadFromJsonAsync<T>().Result;
41+
return Query<T>(path, body);
4342
}
4443

4544
public T? AesQuery<T>(string url, object body)
@@ -54,9 +53,7 @@ public class SimApiHttpClient(string? appId, string appKey)
5453
{
5554
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
5655
};
57-
var http = new HttpClient();
58-
var resp = http.PostAsJsonAsync(url, req).Result;
59-
return resp.Content.ReadFromJsonAsync<T>().Result;
56+
return Query<T>(url, req);
6057
}
6158

6259
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
@@ -67,4 +64,17 @@ public class SimApiHttpClient(string? appId, string appKey)
6764
};
6865
return SignQuery<T>(url, req, queries);
6966
}
67+
68+
private T? Query<T>(string url, object? req)
69+
{
70+
var http = new HttpClient();
71+
var resp = http.PostAsJsonAsync(url, req).Result;
72+
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
73+
if (res == null)
74+
{
75+
throw new SimApiException(500, "请求发生错误");
76+
}
77+
78+
return res.Code != 200 ? throw new SimApiException(res.Code, res.Message) : res.Data;
79+
}
7080
}

0 commit comments

Comments
 (0)