2025-11-02 22:14:34 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using SimApi.Communications;
|
2025-12-18 14:06:11 +08:00
|
|
|
using SimApi.Exceptions;
|
2025-11-02 22:14:34 +08:00
|
|
|
|
|
|
|
|
namespace SimApi.Helpers;
|
|
|
|
|
|
2025-12-18 16:17:18 +08:00
|
|
|
public class SimApiHttpClient(string? appId, string appKey, bool debug = false)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2025-12-18 11:28:47 +08:00
|
|
|
public string Server { get; init; } = string.Empty;
|
2025-11-02 22:14:34 +08:00
|
|
|
public string SignName { get; init; } = "sign";
|
|
|
|
|
public string TimestampName { get; init; } = "timestamp";
|
|
|
|
|
public string NonceName { get; init; } = "nonce";
|
|
|
|
|
public string? AppIdName { get; init; } = "appId";
|
|
|
|
|
public string[] SignFields { get; init; } = [];
|
|
|
|
|
|
2025-12-18 13:05:05 +08:00
|
|
|
public T? SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
2025-11-02 22:14:34 +08:00
|
|
|
{
|
2025-12-18 11:28:47 +08:00
|
|
|
url = Server + url;
|
2025-11-02 22:14:34 +08:00
|
|
|
var queryUrl = SignFields.Aggregate(string.Empty,
|
|
|
|
|
(current, signField) => current + $"{signField}={queries?[signField]}&");
|
|
|
|
|
if (!string.IsNullOrEmpty(AppIdName))
|
|
|
|
|
{
|
|
|
|
|
queryUrl += $"{AppIdName}={appId}&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryUrl += $"{TimestampName}={(int)SimApiUtil.TimestampNow}&{NonceName}={Guid.NewGuid()}";
|
|
|
|
|
var signStr = $"{queryUrl}&{appKey}";
|
|
|
|
|
var path = $"{url}?{queryUrl}&{SignName}={SimApiUtil.Md5(signStr)}";
|
|
|
|
|
|
|
|
|
|
if (queries != null)
|
|
|
|
|
{
|
|
|
|
|
path = queries.Where(q => !SignFields.Contains(q.Key))
|
|
|
|
|
.Aggregate(path, (current, q) => current + $"&{q.Key}={q.Value}");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:06:11 +08:00
|
|
|
return Query<T>(path, body);
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? AesQuery<T>(string url, object body)
|
|
|
|
|
{
|
2025-12-18 11:28:47 +08:00
|
|
|
url = Server + url;
|
2025-11-02 22:14:34 +08:00
|
|
|
if (!string.IsNullOrEmpty(AppIdName))
|
|
|
|
|
{
|
|
|
|
|
url += $"?{AppIdName}={appId}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req = new SimApiOneFieldRequest<string>
|
|
|
|
|
{
|
|
|
|
|
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
|
|
|
|
|
};
|
2025-12-18 14:06:11 +08:00
|
|
|
return Query<T>(url, req);
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
|
|
|
|
{
|
|
|
|
|
var req = new SimApiOneFieldRequest<string>
|
|
|
|
|
{
|
|
|
|
|
Data = SimApiAesUtil.Encrypt(SimApiUtil.Json(body), appKey)
|
|
|
|
|
};
|
|
|
|
|
return SignQuery<T>(url, req, queries);
|
|
|
|
|
}
|
2025-12-18 14:06:11 +08:00
|
|
|
|
|
|
|
|
private T? Query<T>(string url, object? req)
|
|
|
|
|
{
|
|
|
|
|
var http = new HttpClient();
|
2025-12-18 16:17:18 +08:00
|
|
|
if (debug)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:06:11 +08:00
|
|
|
var resp = http.PostAsJsonAsync(url, req).Result;
|
2025-12-18 16:17:18 +08:00
|
|
|
if (debug)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"[HTTPCLIENT响应] {resp.Content.ReadAsStringAsync().Result}\n");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:06:11 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2025-11-02 22:14:34 +08:00
|
|
|
}
|