From 8f3304bd019e25ed1bd295ad448f4c3aa2929c13 Mon Sep 17 00:00:00 2001 From: wet <1034391973@qq.com> Date: Fri, 4 Aug 2023 17:47:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=AB=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BookingOrderSF/BookingOrderSFService.cs | 152 +++++++++++++++++- .../BookingOrderSF/Dto/SFSendBooking.cs | 70 ++++++++ 2 files changed, 216 insertions(+), 6 deletions(-) create mode 100644 Myshipping.Application/Service/BookingOrderSF/Dto/SFSendBooking.cs diff --git a/Myshipping.Application/Service/BookingOrderSF/BookingOrderSFService.cs b/Myshipping.Application/Service/BookingOrderSF/BookingOrderSFService.cs index 4df61758..01d1990b 100644 --- a/Myshipping.Application/Service/BookingOrderSF/BookingOrderSFService.cs +++ b/Myshipping.Application/Service/BookingOrderSF/BookingOrderSFService.cs @@ -94,9 +94,6 @@ namespace Myshipping.Application private readonly ILogger _logger; private readonly ISysCacheService _cache; - - - public BookingOrderSFService(SqlSugarRepository rep, SqlSugarRepository business, SqlSugarRepository detail, ILogger logger, ISysCacheService cache) @@ -235,9 +232,6 @@ namespace Myshipping.Application return Get(entity.Id); } - - - /// /// 删除单据 /// @@ -291,12 +285,158 @@ namespace Myshipping.Application return ordOut; } + /// + /// 发送快递 + /// + /// + /// + [HttpPost("/BookingOrderSF/SendBooking")] + public async Task SendBooking(long Id) + { + var rt = String.Empty; + SFSendBooking sFSend = new SFSendBooking(); + var Delivery = _rep.FirstOrDefault(x => x.Id == Id); + if (Delivery==null) { + + throw Oops.Bah("请选择正确数据!"); + } + sFSend.orderId = Id.ToString(); + sFSend.language = "zh_CN"; + List cargo = new List(); + cargo.Add(new CargoDetailsItem + { + count = (int)Delivery.KDNum, + name = Delivery.GOODSNAME + }); + List contactList = new List(); + + contactList.Add(new ContactInfoListItem + { + + address = $"{Delivery.FJProvince}{Delivery.FJCity}{Delivery.FJAddress}", + contact = Delivery.FJPeople, + contactType = 1, + country = "CN", + postCode = Delivery.FJPostCode, + tel = Delivery.FJTel, + }); + + contactList.Add(new ContactInfoListItem + { + + address = $"{Delivery.SJProvince}{Delivery.SJCity}{Delivery.SJAddress}", + contact = Delivery.SJPeople, + contactType = 2, + country = "CN", + postCode = Delivery.SJPostCode, + tel = Delivery.SJTel, + }); + sFSend.contactInfoList = contactList; + sFSend.cargoDetails = cargo; + + var json= sFSend.ToJson(); + + long timeStamp = DateTimeOffset.Now.ToUnixTimeSeconds(); + + + //获取url + var dict = _cache.GetAllDictData().Result; + + var url = dict.Where(x => x.Code == "SFSendBooking").Select(x => x.Value).FirstOrDefault(); + var partnerID = dict.Where(x => x.Code == "customercode").Select(x => x.Value).FirstOrDefault();//顾客编码: + var requestID = dict.Where(x => x.Code == "CheckCode").Select(x => x.Value).FirstOrDefault();//沙箱校验码 + if (!_cache.Exists($"SFToken{UserManager.TENANT_ID}")) { + + await GetToken(); + } + using (var httpClient = new HttpClient()) + { + using (var request = new HttpRequestMessage(new HttpMethod("Post"), url)) + { + request.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"); + var multipartContent = new MultipartFormDataContent(); + multipartContent.Add(new StringContent("partnerID"), partnerID); + multipartContent.Add(new StringContent("serviceCode"), "EXP_RECE_CREATE_ORDER"); + multipartContent.Add(new StringContent("requestID"), requestID); + multipartContent.Add(new StringContent("timestamp"), timeStamp.ToString()); + multipartContent.Add(new StringContent("accessToken"), _cache.Get($"SFToken{UserManager.TENANT_ID}")); + multipartContent.Add(new StringContent("msgData"), json); + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + if (response.IsSuccessStatusCode) + { + var strRtn = response.Content.ReadAsStringAsync().Result; + var jobj = strRtn.ToJObject(); + if (jobj.GetStringValue("apiResultCode") == "A1000") + { + rt = "下单成功!"; + + } + else + { + throw Oops.Bah(jobj.GetStringValue("apiErrorMsg")); + } + } + } + } + return rt; + } + + + + + /// + /// 获取顺丰token + /// + /// + [NonAction] + public async Task GetToken() + { + var rt = String.Empty; + //获取url + var dict = _cache.GetAllDictData().Result; + + var url = dict.Where(x => x.Code == "SFToken").Select(x => x.Value).FirstOrDefault(); + var partnerID = dict.Where(x => x.Code == "customercode").Select(x => x.Value).FirstOrDefault();//顾客编码: + var secret = dict.Where(x => x.Code == "CheckCode").Select(x => x.Value).FirstOrDefault();//沙箱校验码 + + using (var httpClient = new HttpClient()) + { + using (var request = new HttpRequestMessage(new HttpMethod("Post"), url)) + { + request.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"); + var multipartContent = new MultipartFormDataContent(); + multipartContent.Add(new StringContent("partnerID"), partnerID); + multipartContent.Add(new StringContent("grantType"), "password"); + multipartContent.Add(new StringContent("secret"), secret); + request.Content = multipartContent; + var response = await httpClient.SendAsync(request); + if (response.IsSuccessStatusCode) + { + var strRtn = response.Content.ReadAsStringAsync().Result; + var jobj = strRtn.ToJObject(); + if (jobj.GetStringValue("apiResultCode") == "A1000") + { + rt = jobj.GetStringValue("accessToken"); + await _cache.SetTimeoutAsync($"SFToken{UserManager.TENANT_ID}", rt, TimeSpan.FromHours(3)); + } + else + { + throw Oops.Bah(jobj.GetStringValue("apiErrorMsg")); + } + } + } + } + + return rt; + + } } } diff --git a/Myshipping.Application/Service/BookingOrderSF/Dto/SFSendBooking.cs b/Myshipping.Application/Service/BookingOrderSF/Dto/SFSendBooking.cs new file mode 100644 index 00000000..1819f6ef --- /dev/null +++ b/Myshipping.Application/Service/BookingOrderSF/Dto/SFSendBooking.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Myshipping.Application.Service.BookingOrderSF.Dto +{ + + public class CargoDetailsItem + { + /// + /// 货物数量 跨境件报关需要填写 + /// + public int count { get; set; } + + /// + /// 货物名称,如果需要生成电子 运单,则为必填 + /// + public string name { get; set; } + + } + + public class ContactInfoListItem + { + /// + /// 详细地址,若有四级行政区划,如镇/街道等信息可拼接至此字段,格式样例:镇/街道+详细地址。若province/city 字段的值不传,此字段必须包含省市信息,避免影响原寄地代码识别,如:广东省深圳市福田区新洲十一街万基商务大厦10楼;此字段地址必须详细,否则会影响目的地中转识别; + /// + public string address { get; set; } + /// + /// 联系人 + /// + public string contact { get; set; } + /// + /// 地址类型: 1,寄件方信息 2,到件方信息 + /// + public int contactType { get; set; } + /// + /// 国家或地区代码 例如:内地件CN 香港852 参照附录《城市代码表》 + /// + public string country { get; set; } + /// + /// 邮编,跨境件必填(中国内地, 港澳台互寄除外) + /// + public string postCode { get; set; } + /// + /// 联系电话(tel和mobile字段必填其中一个) + /// + public string tel { get; set; } + } + + public class SFSendBooking + { + + public List cargoDetails { get; set; } + /// + /// + /// + public List contactInfoList { get; set; } + /// + /// 响应报文的语言, 缺省值为zh-CN,目前支持以下值zh-CN 表示中文简体, zh-TW或zh-HK或 zh-MO表示中文繁体, en表示英文 + /// + public string language { get; set; } + /// + /// 客户订单号,重复使用订单号时返回第一次下单成功时的运单信息 + /// + public string orderId { get; set; } + } + +}