| | |
| | | using MES.Service.util; |
| | | using MySqlConnector; |
| | | using Newtonsoft.Json; |
| | | using RestSharp; |
| | | using SharpCompress.Factories; |
| | | using SqlSugar; |
| | | using System.ComponentModel; |
| | | using System.Data; |
| | | using System.Xml.Linq; |
| | | using ZstdSharp.Unsafe; |
| | | using System.Net.Http; |
| | | using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; |
| | | using DbType = System.Data.DbType; |
| | | using System.Text; |
| | | using System.Security.Cryptography; |
| | | using AngleSharp.Io; |
| | | |
| | | |
| | | namespace MES.Service.service.QC; |
| | |
| | | return msg; |
| | | |
| | | } |
| | | |
| | | |
| | | public class DataAcquisitionConfiguration |
| | | { |
| | | //测试环境 |
| | | public const string TEST_COMPANY_CODE = "55958795"; |
| | | public const string TEST_APP_KEY = "ab2d86b6dffabcc81dca6855c727c246"; |
| | | public const string TEST_APP_SECRET = "01b17babe2a96d5ebd802e67709f33d1"; |
| | | public const string TEST_ERP_CODE = "Z106"; |
| | | |
| | | //正式环境 |
| | | public const string COMPANY_CODE = "72505985"; |
| | | public const string APP_KEY = "a2866f03bb7f76387bfb1a98001f0e31"; |
| | | public const string APP_SECRET = "f13bd1bcb130f0090ed92dc021e5f4e1"; |
| | | public const string ERP_CODE = "Z106"; |
| | | } |
| | | [Serializable] |
| | | public class ApiCommonParam |
| | | { |
| | | //发放给用户的Key |
| | | public string appKey { get; set; } |
| | | //接口版本 |
| | | public string version { get; set; } |
| | | //数据所属公司编码 |
| | | public string ownerCompanyCode { get; set; } |
| | | //操作者所属公司编码 |
| | | public string operateCompanyCode { get; set; } |
| | | //签名 |
| | | public string sign { get; set; } |
| | | //时间戳 |
| | | public long? timestamps { get; set; } |
| | | ////扩展字段 |
| | | //public object Reserver { get; set; } |
| | | } |
| | | [Serializable] |
| | | public class BodyParam |
| | | { |
| | | public string erpCode { get; set; } |
| | | public string dnXkNo { get; set; } |
| | | } |
| | | public static string BuildCurrentSign(string paramJson, string appSecret) |
| | | { |
| | | var commonDict = new Dictionary<string, string>(); |
| | | var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(paramJson); |
| | | |
| | | var keyList = new List<string>(); |
| | | foreach (var entry in jsonDict) |
| | | { |
| | | if (entry.Key.Equals("sign", StringComparison.OrdinalIgnoreCase)) |
| | | continue; |
| | | |
| | | var value = entry.Value?.ToString().Trim() ?? ""; |
| | | commonDict[entry.Key] = value; |
| | | keyList.Add(entry.Key); |
| | | } |
| | | |
| | | // 按字母顺序排序键 |
| | | keyList.Sort((x, y) => string.Compare(x, y, StringComparison.Ordinal)); |
| | | |
| | | // 拼接签名字符串 |
| | | var sb = new StringBuilder(); |
| | | foreach (var key in keyList) |
| | | { |
| | | sb.Append(commonDict[key]).Append(":"); |
| | | } |
| | | sb.Append(appSecret); |
| | | |
| | | // 计算MD5 |
| | | using var md5 = MD5.Create(); |
| | | byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString())); |
| | | |
| | | return BitConverter.ToString(hashBytes) |
| | | .Replace("-", "") |
| | | .ToLowerInvariant(); |
| | | } |
| | | public class XkyCommonParam |
| | | { |
| | | public ApiCommonParam commonParam { get; set; } |
| | | public BodyParam body { get; set; } |
| | | |
| | | public static XkyCommonParam GetInit() |
| | | { |
| | | var apiParam = new ApiCommonParam |
| | | { |
| | | // 发携客云提供的appKey |
| | | appKey = DataAcquisitionConfiguration.APP_KEY, |
| | | // 接口版本 |
| | | version = "1.0", |
| | | // 操作者所属公司编码 |
| | | operateCompanyCode = DataAcquisitionConfiguration.COMPANY_CODE, |
| | | // 数据所属公司编码,非集团公司默认赋值为操作公司 |
| | | ownerCompanyCode = DataAcquisitionConfiguration.COMPANY_CODE, |
| | | // 当前时间对应的时间戳(秒数) |
| | | timestamps = DateTimeOffset.UtcNow.ToUnixTimeSeconds() |
| | | }; |
| | | |
| | | |
| | | string appSecret = DataAcquisitionConfiguration.APP_SECRET; |
| | | |
| | | // 生成签名 |
| | | string sign = BuildCurrentSign( |
| | | JsonConvert.SerializeObject(apiParam), |
| | | appSecret); |
| | | apiParam.sign = sign; |
| | | |
| | | return new XkyCommonParam |
| | | { |
| | | commonParam = apiParam |
| | | }; |
| | | } |
| | | } |
| | | |
| | | public async Task<string[]> SignDelivery(string id) |
| | | { |
| | | string[] msg = new string[2]; |
| | | //发起请求,签收送货单 |
| | | |
| | | try |
| | | { |
| | | var requestData = XkyCommonParam.GetInit(); |
| | | requestData.body = new BodyParam |
| | | { |
| | | erpCode = "Z106", |
| | | dnXkNo = id |
| | | }; |
| | | |
| | | // 序列化为JSON |
| | | string json = JsonConvert.SerializeObject(requestData); |
| | | |
| | | using (var client = new HttpClient()) |
| | | { |
| | | // 设置请求内容 |
| | | var content = new StringContent(json, Encoding.UTF8, "application/json"); |
| | | // 发送POST请求 |
| | | HttpResponseMessage response = await client.PostAsync( |
| | | "https://openapi.xiekeyun.com/delivery/updateDeliveryStatus.json", |
| | | content); |
| | | // 处理响应 |
| | | if (response.IsSuccessStatusCode) |
| | | { |
| | | string responseBody = await response.Content.ReadAsStringAsync(); |
| | | var result = |
| | | JsonConvert.DeserializeObject<XKYApiResponse>(responseBody); |
| | | //成功后手动下推到货单 |
| | | if (result.Result == 1) { |
| | | |
| | | // 定义输出参数 |
| | | |
| | | var outputMessage = new SugarParameter("C_RESULT", null, |
| | | DbType.String, |
| | | ParameterDirection.Output, 4000); |
| | | |
| | | // 定义输入参数 |
| | | var parameters = new List<SugarParameter> |
| | | { |
| | | new("C_IN_STR", "送货单签收[BTNOK[PL017["+id, |
| | | DbType.String, ParameterDirection.Input), |
| | | outputMessage |
| | | }; |
| | | |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | | // 使用 SqlSugar 执行存储过程 |
| | | db.Ado.ExecuteCommand( |
| | | "BEGIN PRC_RF_PDA_RECEIPT_BTN(:C_IN_STR,:C_RESULT); END;", |
| | | parameters.ToArray()); |
| | | |
| | | |
| | | // 获取输出参数的值 |
| | | |
| | | var messageValue = outputMessage.Value?.ToString(); |
| | | |
| | | msg[0] = "0"; |
| | | msg[1] = messageValue; |
| | | } |
| | | else |
| | | { |
| | | if (result.ErrorMsg== "出货单据不是已送货状态!") |
| | | { |
| | | // 定义输出参数 |
| | | |
| | | var outputMessage = new SugarParameter("C_RESULT", null, |
| | | DbType.String, |
| | | ParameterDirection.Output, 4000); |
| | | |
| | | // 定义输入参数 |
| | | var parameters = new List<SugarParameter> |
| | | { |
| | | new("C_IN_STR", "送货单签收[BTNOK[PL017["+id, |
| | | DbType.String, ParameterDirection.Input), |
| | | outputMessage |
| | | }; |
| | | |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | | // 使用 SqlSugar 执行存储过程 |
| | | db.Ado.ExecuteCommand( |
| | | "BEGIN PRC_RF_PDA_RECEIPT_BTN(:C_IN_STR,:C_RESULT); END;", |
| | | parameters.ToArray()); |
| | | |
| | | |
| | | // 获取输出参数的值 |
| | | |
| | | var messageValue = outputMessage.Value?.ToString(); |
| | | |
| | | msg[0] = "0"; |
| | | msg[1] = messageValue; |
| | | } |
| | | else |
| | | { |
| | | msg[0] = "1"; |
| | | msg[1] = "002[" + id + "签收失败," + result.ErrorMsg; |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | else |
| | | { |
| | | |
| | | msg[0] = "1"; |
| | | msg[1] = $"002[{id}签收失败,请求失败,状态码:{response.StatusCode}"; |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | msg[0] = "1"; |
| | | msg[1] = $"002[请求失败,状态码:{ex.Message}"; |
| | | } |
| | | |
| | | |
| | | //msg[0] = "1"; |
| | | //msg[1] = responseBody; |
| | | return msg; |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | private string ExtractSubstring(string input, char startChar, char endChar) |
| | | { |
| | | var startIndex = input.IndexOf(startChar); |
| | |
| | | |
| | | [JsonProperty("totalCount")] public int TotalCount { get; set; } |
| | | } |
| | | /// <summary> |
| | | /// 携客云接口返回对象 |
| | | /// </summary> |
| | | public class XKYApiResponse |
| | | { |
| | | [JsonProperty("errorCode")] |
| | | public string ErrorCode { get; set; } |
| | | |
| | | [JsonProperty("errorMsg")] |
| | | public string ErrorMsg { get; set; } |
| | | |
| | | [JsonProperty("result")] |
| | | public int Result { get; set; } |
| | | |
| | | [JsonProperty("data")] |
| | | public object Data { get; set; } |
| | | |
| | | [JsonProperty("curServerTime")] |
| | | public long CurServerTime { get; set; } |
| | | |
| | | // 如果需要时间转换可以添加这个属性 |
| | | public DateTime ServerTime => DateTimeOffset.FromUnixTimeMilliseconds(CurServerTime).DateTime; |
| | | } |
| | | } |