FLH 2 months ago
parent
commit
1d654967b1

+ 179 - 0
3.Service/XYY.Service.Standard.ChannelApi/DeDhl/DeDhlApi.cs

@@ -0,0 +1,179 @@
+using iTextSharp.text.pdf;
+using RestSharp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Threading.Tasks;
+using XYY.Model.Standard.Channel;
+using XYY.Model.Standard.Order;
+using XYY.Service.Standard.ChannelApi.Base;
+
+namespace XYY.Service.Standard.ChannelApi.DeDhl
+{
+    public class DeDhlApi : IBaseApi
+    {
+        public string ServiceName => "DeDHL";
+
+        public Task<List<BagDeclareErrosResult>> BagDeclare(BagDeclareRequest bagDeclareRequest)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Task CannelOrders(List<string> systemNO)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Task CannelOrdersByModel(logistics_ChannelCancel logistics_ChannelCancel)
+        {
+            throw new NotImplementedException();
+        }
+
+        public Task<List<string>> Confims(List<OrderConfimRequest> order, Logistics_Channel channel)
+        {
+            throw new NotImplementedException();
+        }
+
+        public OrderRequest GetOrderRequest(Order_Order order, List<Order_OrderGoods> goods, Logistics_Channel channel, ExtendArg extendArg)
+        {
+            string billNumber = channel.ApiSecret.Split(":")[1];
+
+            OrderRequest orderRequest = new OrderRequest();
+            orderRequest.profile = "STANDARD_GRUPPENPROFIL";
+            orderRequest.shipments = new List<ShipmentsItem> { new ShipmentsItem {
+
+                billingNumber = billNumber,
+                product=channel.ServiceChannelCode,
+                refNo=order.SystemNo.Replace("XYY","").Replace("YQ",""),
+                shipper = new Shipper{
+                    city=channel.SenderCity,
+                    postalCode=channel.SenderZipCode,
+                    phone=channel.SenderPhone,
+                    country="DEU",
+                    addressHouse=channel.SenderCounty,
+                    addressStreet = channel.SenderStreet,
+                    additionalAddressInformation1 =null,
+                    email = channel.SenderEmail,
+                    name1 = channel.SenderName
+                },consignee = new Consignee
+                {
+                    name1 =order.ReceiverName,
+                    addressStreet=order.ReceiverStreet,
+                    city=order.ReceiverCity,
+                    email=order.ReceiverEmail,
+                    postalCode=order.ReceiverZipCode,
+                    phone=order.ReceiverPhone,
+                    additionalAddressInformation1=null,
+                    country="DEU"
+                },
+                details = new Details
+                {
+                    weight = new Weight
+                    {
+                        uom="g",
+                        value = order.CustomerWeight
+                    }
+                }
+            }
+            };
+
+            return orderRequest;
+
+        }
+        public string Base64Encode(string plainText)
+        {
+            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
+            return System.Convert.ToBase64String(plainTextBytes);
+        }
+        public async Task<UploadResult> DataUpload(Order_Order order, List<Order_OrderGoods> goods, Logistics_Channel channel, ExtendArg arg)
+        {
+            //apikey = gkpuser:gkppass
+            string baseUrl = channel.ApiHost;
+            var client = new HttpClient();
+            string apiKey = channel.ApiSecret.Split(':')[0];
+            var orderRequest = GetOrderRequest(order, goods, channel, arg);
+            string base64Auth = Base64Encode(channel.ApiKey);
+            string json = Newtonsoft.Json.JsonConvert.SerializeObject(orderRequest);
+            RestClient restClient = new RestClient();
+            RestRequest restRequest = new RestRequest($"{baseUrl}orders");
+            restRequest.Method = Method.POST;
+            restRequest.AddHeader("Accept-Language", "en-US");
+            restRequest.AddHeader("Authorization", $"Basic {base64Auth}");
+            restRequest.AddHeader("dhl-api-key", apiKey);
+            restRequest.AddJsonBody(json);
+            var response = restClient.Execute(restRequest);
+            if (response.StatusCode == System.Net.HttpStatusCode.OK)
+            {
+                var resultData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultData>(response.Content);
+                var resultItem = resultData.items[0];
+                return new UploadResult
+                {
+                    IsSuccess = true,
+                    DownloadUrl = resultItem.label.b64,
+                    TrackingNumber = resultItem.shipmentNo
+                };
+            }
+            else
+            {
+                var resultData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultData>(response.Content);
+                if (resultData == null)
+                    throw new Exception("下家服务器访问异常");
+                throw new Exception(resultData.status.detail);
+            }
+            //var request = new HttpRequestMessage
+            //{
+            //    Method = HttpMethod.Post,
+            //    RequestUri = new Uri($"{baseUrl}/orders"),
+            //    Headers =
+            //    {
+            //        { "Accept-Language", "en-US" },
+            //        { "Authorization", $"Basic {channel.ApiKey}"},
+            //        {"dhl-api-key",apiKey}
+            //    },
+            //    Content = new StringContent(json)
+            //    {
+            //        Headers =
+            //        {
+            //            ContentType = new MediaTypeHeaderValue("application/json")
+            //        }
+            //    }
+            //};
+            //try
+            //{
+            //    using (var response = await client.SendAsync(request))
+            //    {
+            //        response.EnsureSuccessStatusCode();
+            //        var body = await response.Content.ReadAsStringAsync();
+            //        Console.WriteLine(body);
+            //    }
+            //}
+            //catch (HttpRequestException ex)
+            //{
+            //    ex.
+            //}
+            //catch (Exception ex)
+            //{
+
+            //}
+        }
+
+        public async Task<byte[]> DrawPdfLabel(UploadResult result, Logistics_Channel channel, Order_Order order, List<Order_OrderGoods> orderGoods)
+        {
+            string pdfURL = result.DownloadUrl;
+            if (string.IsNullOrEmpty(pdfURL))
+            {
+                throw new Exception("PDF 地址不应为空");
+            }
+            byte[] data = Convert.FromBase64String(pdfURL);
+            return data;
+        }
+
+        public Task<string> GetTrackingNumber(string SystemNo, string TrackingNumber, string ServiceOrderNo, Logistics_Channel channel)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 173 - 0
3.Service/XYY.Service.Standard.ChannelApi/DeDhl/RequestData.cs

@@ -0,0 +1,173 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace XYY.Service.Standard.ChannelApi.DeDhl
+{
+    //如果好用,请收藏地址,帮忙分享。
+    public class Shipper
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string name1 { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string addressStreet { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string addressHouse { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string additionalAddressInformation1 { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string postalCode { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string city { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string country { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string email { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string phone { get; set; }
+    }
+
+    public class Consignee
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string name1 { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string addressStreet { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string addressHouse { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string additionalAddressInformation1 { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string postalCode { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string city { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string country { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string email { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string phone { get; set; }
+    }
+
+    public class Dim
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string uom { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int height { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int length { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int width { get; set; }
+    }
+
+    public class Weight
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string uom { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int value { get; set; }
+    }
+
+    public class Details
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public Dim dim { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Weight weight { get; set; }
+    }
+
+    public class ShipmentsItem
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string product { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string billingNumber { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string refNo { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Shipper shipper { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Consignee consignee { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Details details { get; set; }
+    }
+
+    public class OrderRequest
+    { 
+        /// <summary>
+        /// 
+        /// </summary>
+        public string profile { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public List<ShipmentsItem> shipments { get; set; }
+    }
+}

+ 91 - 0
3.Service/XYY.Service.Standard.ChannelApi/DeDhl/ResultData.cs

@@ -0,0 +1,91 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace XYY.Service.Standard.ChannelApi.DeDhl
+{
+    //如果好用,请收藏地址,帮忙分享。
+    public class Status
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string title { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int statusCode { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int status { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string instance { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string detail { get; set; }
+    }
+
+    public class Sstatus
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string title { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public int status { get; set; }
+    }
+
+    public class Label
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string url { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string format { get; set; }
+        public string b64
+        { get; set; }
+    }
+
+    public class LabelItem
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public string shipmentNo { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public string routingCode { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Sstatus sstatus { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public Label label { get; set; }
+    }
+
+    public class ResultData
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public Status status { get; set; }
+        /// <summary>
+        /// 
+        /// </summary>
+        public List<LabelItem> items { get; set; }
+    }
+}