diff --git a/Infrastructure/Helpers/DHHeaderUtil.cs b/Infrastructure/Helpers/DHHeaderUtil.cs new file mode 100644 index 0000000..e1f2b46 --- /dev/null +++ b/Infrastructure/Helpers/DHHeaderUtil.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Infrastructure.Helpers +{ + public static class DHHeaderUtil + { + public static Dictionary BuildHeader( + string ak, + string sk, + string httpMethod, + string canonicalizedResource) + { + // 1. UTC 时间格式化 + string utcDate = DateTime.UtcNow.ToString("yyyyMMdd'T'HHmmss'Z'"); + //string utcDate = "20250813T085311Z"; + Console.WriteLine("UTC时间: " + utcDate); + // 2. 拼接签名字符串 + string stringToSign = $"{httpMethod}\n\napplication/json\n{utcDate}\n" + + $"x-lc-date:{utcDate}\n" + + "x-lc-version:1.0\n" + + $"{canonicalizedResource}"; + Console.WriteLine("StringToSign: " + stringToSign); + + // 3. 生成 HMAC-SHA256 + string signature; + using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(sk))) + { + var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); + signature = Convert.ToBase64String(hashBytes); + } + + Console.WriteLine("HmacSHA256+Base64: " + signature); + + // 4. Authorization + string authorization = $"LC-HMAC-SHA256 {ak}:{signature}"; + Console.WriteLine("Authorization: " + authorization); + + // 5. 返回请求头字典 + return new Dictionary + { + { "Authorization", authorization }, + { "X-LC-Date", utcDate }, + { "X-LC-Version", "1.0" } + }; + } + + /// + /// 生成 HMAC-SHA256 签名,并 Base64 编码 + /// + public static string Sign(string stringToSign, string key) + { + if (string.IsNullOrEmpty(stringToSign) || string.IsNullOrEmpty(key)) + return null; + + using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); + byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)); + return Convert.ToBase64String(hashBytes); + } + } +} diff --git a/OpenAuth.App/ServiceApp/Request/DaHuaTaskInfo.cs b/OpenAuth.App/ServiceApp/Request/DaHuaTaskInfo.cs new file mode 100644 index 0000000..4a04566 --- /dev/null +++ b/OpenAuth.App/ServiceApp/Request/DaHuaTaskInfo.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace OpenAuth.App.ServiceApp.Request +{ + public class DaHuaTaskInfo + { + public string channelId { get; set; } + + public string userChannelCode { get; set; } + + public int analysisType { get; set; } + + public string url { get; set; } + + public string memo { get; set; } + + public string config { get; set; } + + public string saasExtParam { get; set; } + + public List algorithms { get; set; } + } + public class AnalysisRequest + { + public string name { get; set; } + + public string edition { get; set; } + + public string algorithmClass { get; set; } + + public string rule { get; set; } + public string ruleType { get; set; } + } + +} diff --git a/OpenAuth.WebApi/Controllers/ServiceControllers/DaHuaAiController.cs b/OpenAuth.WebApi/Controllers/ServiceControllers/DaHuaAiController.cs new file mode 100644 index 0000000..60ec403 --- /dev/null +++ b/OpenAuth.WebApi/Controllers/ServiceControllers/DaHuaAiController.cs @@ -0,0 +1,178 @@ +using DocumentFormat.OpenXml.Math; +using Infrastructure; +using Infrastructure.Helpers; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using OpenAuth.App.ServiceApp.Request; +using OpenAuth.Repository.Domain; +using System.Globalization; +using System.Security.Cryptography; +using System.Text; + +namespace OpenAuth.WebApi.Controllers.ServiceControllers +{ + /// + /// 大华ai接口 + /// + [Route("api/[controller]/[action]")] + [ApiController] + public class DaHuaAiController : ControllerBase + { + private readonly string ak = "e3d019924e7f40089bcffba4"; + private readonly string sk = "cf83a12caa155b994eb34fa9"; + private readonly string baseUrl = "https://123.132.248.154:6405"; + private async Task> PostJsonAsync(string path, object body) + { + var handler = new HttpClientHandler + { + // 忽略服务器证书 + ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true + }; + + var headers = DHHeaderUtil.BuildHeader(ak, sk, "POST", path); + var url = $"{baseUrl}{path}"; + + using var httpClient = new HttpClient(handler); + + foreach (var kv in headers) + { + httpClient.DefaultRequestHeaders.Add(kv.Key, kv.Value); + } + + var jsonBody = JsonConvert.SerializeObject(body ?? new { }); + var content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); + + try + { + var response = await httpClient.PostAsync(url, content); + var responseContent = await response.Content.ReadAsStringAsync(); + + return new Response { Result = responseContent }; + } + catch (Exception ex) + { + return new Response { Result = ex.Message }; + } + } + /// + /// 分页查询车辆分析 + /// + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public Task> GetTasksList(int page, int limit) + { + return PostJsonAsync($"/processing/analysis/vehicle/tasks?start={page}&limit={limit}", new + { + channelIds = new[] { "sdhc" } + }); + } + + [HttpPost] + [AllowAnonymous] + [Obsolete] + public async Task> GetTasksList1(int page, int limit) + { + var handler = new HttpClientHandler(); + // 忽略服务器证书 + handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; + // 假设 DHHeaderUtil 返回 Dictionary + string ak = "e3d019924e7f40089bcffba4"; + string sk = "cf83a12caa155b994eb34fa9"; + var headers = DHHeaderUtil.BuildHeader(ak, sk, "POST", $"/processing/analysis/vehicle/tasks?start={page}&limit={limit}"); + // 要请求的 URL + var url = $"https://123.132.248.154:6405/processing/analysis/vehicle/tasks?start={page}&limit={limit}"; + // 请求体(JSON) + var data = new + { + channelIds = new[] + { + "sdhc" + } + }; + var jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(data); + using var httpClient = new HttpClient(handler); + // 添加请求头 + // 只把非 Content-Type 的头加到 request + foreach (var kv in headers) + { + if (kv.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase)) + continue; + httpClient.DefaultRequestHeaders.Add(kv.Key, kv.Value); + } + // 创建 HttpContent + var content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); + try + { + var response = await httpClient.PostAsync(url, content); + var responseContent = await response.Content.ReadAsStringAsync(); + return new Response() + { + Result = responseContent + }; + } + catch (Exception ex) + { + return new Response() + { + Result = ex.Message + }; + } + } + /// + /// 获取算法列表 + /// + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public Task> GetsupportList(int page, int limit) + { + return PostJsonAsync($"/processing/engine/algorithms/support?currentPage={page}&pageSize={limit}", new { }); + } + /// + /// 查询算法参数描述文件接口 + /// + /// + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public Task> Getconfig(string version, int functionType, string algorithmCode) + { + return PostJsonAsync("/processing/engine/algorithms/isd/config/get", new + { + version, + functionType, + algorithmCode + }); + } + /// + /// 创建行为场景分析任务 + /// + /// + /// + [HttpPost] + [AllowAnonymous] + public Task> CreateTasks(DaHuaTaskInfo data) + { + return PostJsonAsync("/processing/behavior/realtime/tasks", data); + } + /// + /// 删除行为场景分析任务 + /// + /// 行为场景分析任务id + /// + [HttpPost] + [AllowAnonymous] + public Task> DeleteTasks(string taskId) + { + return PostJsonAsync($"/processing/behavior/realtime/tasks/{taskId}", new { }); + } + } +}