jianghaiqing 1 year ago
commit 611f131e58

@ -94,9 +94,6 @@ namespace Myshipping.Application
private readonly ILogger<BookingOrderSFService> _logger;
private readonly ISysCacheService _cache;
public BookingOrderSFService(SqlSugarRepository<BookingOrderSFExpressDelivery> rep, SqlSugarRepository<BookingOrderSFExpressdeliveryBusiness> business,
SqlSugarRepository<BookingOrderSFExpressdeliveryDetail> detail,
ILogger<BookingOrderSFService> logger, ISysCacheService cache)
@ -235,9 +232,6 @@ namespace Myshipping.Application
return Get(entity.Id);
}
/// <summary>
/// 删除单据
/// </summary>
@ -291,12 +285,158 @@ namespace Myshipping.Application
return ordOut;
}
/// <summary>
/// 发送快递
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpPost("/BookingOrderSF/SendBooking")]
public async Task<dynamic> 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<CargoDetailsItem> cargo = new List<CargoDetailsItem>();
cargo.Add(new CargoDetailsItem
{
count = (int)Delivery.KDNum,
name = Delivery.GOODSNAME
});
List<ContactInfoListItem> contactList = new List<ContactInfoListItem>();
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;
}
/// <summary>
/// 获取顺丰token
/// </summary>
/// <returns></returns>
[NonAction]
public async Task<dynamic> 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;
}
}
}

@ -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
{
/// <summary>
/// 货物数量 跨境件报关需要填写
/// </summary>
public int count { get; set; }
/// <summary>
/// 货物名称,如果需要生成电子 运单,则为必填
/// </summary>
public string name { get; set; }
}
public class ContactInfoListItem
{
/// <summary>
/// 详细地址,若有四级行政区划,如镇/街道等信息可拼接至此字段,格式样例:镇/街道+详细地址。若province/city 字段的值不传此字段必须包含省市信息避免影响原寄地代码识别广东省深圳市福田区新洲十一街万基商务大厦10楼此字段地址必须详细否则会影响目的地中转识别
/// </summary>
public string address { get; set; }
/// <summary>
/// 联系人
/// </summary>
public string contact { get; set; }
/// <summary>
/// 地址类型: 1寄件方信息 2到件方信息
/// </summary>
public int contactType { get; set; }
/// <summary>
/// 国家或地区代码 例如内地件CN 香港852 参照附录《城市代码表》
/// </summary>
public string country { get; set; }
/// <summary>
/// 邮编,跨境件必填(中国内地, 港澳台互寄除外)
/// </summary>
public string postCode { get; set; }
/// <summary>
/// 联系电话tel和mobile字段必填其中一个
/// </summary>
public string tel { get; set; }
}
public class SFSendBooking
{
public List<CargoDetailsItem> cargoDetails { get; set; }
/// <summary>
///
/// </summary>
public List<ContactInfoListItem> contactInfoList { get; set; }
/// <summary>
/// 响应报文的语言, 缺省值为zh-CN目前支持以下值zh-CN 表示中文简体, zh-TW或zh-HK或 zh-MO表示中文繁体 en表示英文
/// </summary>
public string language { get; set; }
/// <summary>
/// 客户订单号,重复使用订单号时返回第一次下单成功时的运单信息
/// </summary>
public string orderId { get; set; }
}
}
Loading…
Cancel
Save