using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace TaskManager.Controllers { /// /// 常熟安通林奇瑞的Http接口 ///// //public static class HttpCheryMOMController //{ // private static string appKey = "8EG"; // private static string appSecret = "3tdmkx&v%TJqTM@GA3Va"; // private static string baseNo = "13"; // public static string DomainUrl = "https://api-mom.mychery.com:8443"; // 正式环境地址 // private static readonly HttpClient httpClient = new HttpClient // { // Timeout = TimeSpan.FromSeconds(30) // }; // /// // /// 通用POST请求方法 // /// // /// 请求地址 // /// JSON请求体 // /// 错误信息 // /// 响应结果 // public static async Task PostAsync(string url, string jsonData, out string error) // { // error = string.Empty; // try // { // // 生成时间戳和签名 // string timeStamp = HttpUtilityAnTongLin.GetCurrentTimestampMillis().ToString(); // string original = $"{appKey}{timeStamp}{appSecret}"; // string sign = HttpUtilityAnTongLin.StringEncryMD5(original); // // 构造请求 // var request = new HttpRequestMessage(HttpMethod.Post, url) // { // Content = new StringContent(jsonData, Encoding.UTF8, "application/json") // }; // request.Headers.Add("appKey", appKey); // request.Headers.Add("timeStamp", timeStamp); // request.Headers.Add("sign", sign); // request.Headers.Add("signType", "MD5"); // request.Headers.Add("baseNo", baseNo); // // 发送请求 // var response = await httpClient.SendAsync(request); // response.EnsureSuccessStatusCode(); // return await response.Content.ReadAsStringAsync(); // } // catch (HttpRequestException ex) // { // error = ex.Message; // return string.Empty; // } // } // #region 配送单接口(看板发货) // private static string delivebillUrl = $"{DomainUrl}/api-gateway/common-api/map/info"; // /// // /// 配送单接口 // /// // /// 配送单号列表 // /// 1=待入库配送单信息,2=待出库配送单信息(默认) // /// 接口响应结果 // public static async Task DeliveBillInfoAsync(List mapSheetNoList, int type = 2) // { // string error = string.Empty; // var req = new SortInputPara // { // base_no = baseNo, // s_datetime = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd HH:mm:ss"), // e_datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // map_sheet_no = mapSheetNoList.ToArray(), // type = type // }; // try // { // string jsonData = JsonConvert.SerializeObject(req); // string result = await PostAsync(delivebillUrl, jsonData, out error); // WebLogbHelper.WriteLog($"接口DeliveBillInfo入参:{jsonData}, 返回:{result}"); // return result; // } // catch (Exception ex) // { // WebLogbHelper.WriteLog($"接口DeliveBillInfo异常:{ex}"); // error = ex.Message; // return string.Empty; // } // } // #endregion // #region 外排配送单接口(排序发货) // private static string jisSheetUrl = $"{DomainUrl}/api-gateway/common-api/map/jisSheet"; // /// // /// 外排配送单接口 // /// // /// 单号列表 // /// 接口响应结果 // public static async Task JisSheetAsync(List mapSheetNoList) // { // string error = string.Empty; // var req = new InputPara // { // base_no = baseNo, // s_datetime = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd HH:mm:ss"), // e_datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // map_sheet_no = mapSheetNoList.ToArray() // }; // try // { // string jsonData = JsonConvert.SerializeObject(req); // string result = await PostAsync(jisSheetUrl, jsonData, out error); // WebLogbHelper.WriteLog($"接口JisSheet入参:{jsonData}, 返回:{result}"); // return result; // } // catch (Exception ex) // { // WebLogbHelper.WriteLog($"接口JisSheet异常:{ex}"); // error = ex.Message; // return string.Empty; // } // } // #endregion // /// // /// 对象转JSON字符串 // /// // public static string ObjectToJson(object obj) // { // return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); // } // /// // /// JSON字符串转对象 // /// // public static T JsonToObject(string json) // { // return JsonConvert.DeserializeObject(json); // } //} ///// ///// 时间戳和MD5加密工具类 ///// //public static class HttpUtilityAnTongLin //{ // /// // /// MD5加密 // /// // public static string StringEncryMD5(string input) // { // using var md5 = MD5.Create(); // byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input)); // return BitConverter.ToString(hashBytes).Replace("-", "").ToUpper(); // } // /// // /// 获取当前时间戳(毫秒) // /// // public static long GetCurrentTimestampMillis() // { // return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; // } //} ///// ///// 接口响应基类 ///// ///// 数据类型 //public class Res where T : class //{ // public int code { get; set; } = -1; // public string message { get; set; } = string.Empty; // public T data { get; set; } = default!; //} //#region 接口请求参数类 //public class InputPara //{ // public string base_no { get; set; } = string.Empty; // public string s_datetime { get; set; } = string.Empty; // public string e_datetime { get; set; } = string.Empty; // public string[] map_sheet_no { get; set; } = Array.Empty(); //} //public class SortInputPara : InputPara //{ // public int type { get; set; } //} //#endregion //#region 日志帮助类 //public static class WebLogbHelper //{ // private static readonly string LogDirectory = Path.Combine(AppContext.BaseDirectory, "logs"); // static WebLogbHelper() // { // Directory.CreateDirectory(LogDirectory); // } // /// // /// 写入日志 // /// // /// 日志内容 // public static void WriteLog(string message) // { // string logPath = Path.Combine(LogDirectory, $"{DateTime.Now:yyyy-MM-dd}.log"); // lock (typeof(WebLogbHelper)) // { // File.AppendAllText(logPath, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} >>> {message}{Environment.NewLine}"); // } // } //} //#endregion }