using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using System.Threading.Tasks; using WpfApp4; namespace TaskManager.Controllers { public class BaseController:IDoExecute { protected string appKey = "8EG566b9bedd2bf46d"; protected string appSecret = "48edc4425647425d87f806a1ba492580"; protected string baseNo = "13"; protected string DomainUrl = App.HangfireConfig.Url; // 正式环境 protected HttpClient _httpClient; public BaseController() { _httpClient =new HttpClient(); } /// /// /// /// 全路径调用 /// /// /// protected async Task Post(string url, string jsonData,string route) { //error = string.Empty; try { // 生成签名参数 string timeStamp = GetCurrentTimestamp(); string nonce = GenerateNonce(); var sign = GenerateSign(HttpMethod.Post.Method, route, appKey, appSecret, timeStamp, nonce, jsonData); // 构建请求 var request = new HttpRequestMessage(HttpMethod.Post, url); request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json"); request.Headers.Add("appKey", appKey); request.Headers.Add("appSecret", appSecret); request.Headers.Add("timestamp", timeStamp); request.Headers.Add("sign", sign); request.Headers.Add("nonce", nonce); // 发送请求 var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); // 抛出异常处理状态码 return await response.Content.ReadAsStringAsync(); } catch (HttpRequestException ex) { //error = ex.Message; return string.Empty; } } private string GenerateSign(string method, string path, string appKey, string appSecret, string timestamp, string nonce, string jsonBody) { // 按照规则拼接参数 string paramStr = $"method={method.ToUpper()}&path={path}&appKey={appKey}&appSecret={appSecret}×tamp={timestamp}&nonce={nonce}&jsonBody={jsonBody}"; return ComputeSHA512(paramStr); } private string ComputeSHA512(string input) { using (SHA512 sha512 = SHA512.Create()) { byte[] bytes = Encoding.UTF8.GetBytes(input); byte[] hash = sha512.ComputeHash(bytes); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); // "x2" 表示小写十六进制 } return builder.ToString(); } } private static string RemoveWhitespace(string input) { if (string.IsNullOrEmpty(input)) return input; // 使用正则表达式移除空格和换行 return Regex.Replace(input, @"[\s]+", ""); } private string GetCurrentTimestamp() { return ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToString(); } private string GenerateNonce() { const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); int length = random.Next(10, 51); return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } public async Task ExecuteAsync() { var str=JsonSerializer.Serialize(new SupplierPlanRequest(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true // 可选,用于格式化输出 } ); str = RemoveWhitespace(str); var ret =await Post("https://ediuat.mychery.com/prod-api/api-apply/v2/get/supplierProPlaning",str, "/v2/get/supplierProPlaning"); } } public class SupplierPlanRequest { public string Date { get; set; } = DateTime.Today.ToString("yyyy-MM-dd"); // 默认当前日期 public int PageSize { get; set; } = 1000; // 最大值 1000 public int PageNum { get; set; } = 1; // 从 1 开始 public bool IsForce { get; set; } = false; // 默认非强制获取 } // 响应数据模型(根据文档示例响应定义) public class SupplierPlanResponse { public int Total { get; set; } public int PageNum { get; set; } public int PageSize { get; set; } public List Rows { get; set; } = new List(); } public class PlanData { public int Id { get; set; } public string ReleaseEdition { get; set; } public string Models { get; set; } public string SalseDepartment { get; set; } public string Type { get; set; } public string Assembly { get; set; } // 按文档响应示例补充其他字段... } }