using Newtonsoft.Json; using SiteCore.taoObj; using SQLData; using System; using System.Collections.Specialized; using System.Data; using System.Net; using System.Text; using static SiteCore.taoObj.tms_waybill_subscription_query_res_Obj; namespace SiteCore { public class cainiaoLink { //领淘网络新的token//Z0haTGJZTStPL2prMmdEcmFHTklvajVOcDN5T01HVEQvSS9Sci9HUjZob1lGTVd0dUVxUHd5MFNPNEwydm8ycA== private const string logistic_provider_id = "TEZNRDkvTTFSTFVUWm1MeGduS2FQeFF2d0NIbGxIdldoUWtuaXhHVVFYRWJFUkJrTUorSUkyQW45a2Q1Q0NLaA=="; // token //private const string logistic_provider_id = "Z0haTGJZTStPL2prMmdEcmFHTklvajVOcDN5T01HVEQvSS9Sci9HUjZob1lGTVd0dUVxUHd5MFNPNEwydm8ycA=="; // token private const string msg_type = "TMS_WAYBILL_SUBSCRIPTION_QUERY"; // 调用的API private const string to_code = ""; // 被调用方的code private const string appSecret = "7GR2GzO6p2LY4jO9hI5M0htWt28824Df"; // 应用的AppSecret //private const string appSecret = "4U7Xxbnj5SE5V9V045scqLB189M5Et0J"; // 应用的AppSecret private const string url = "http://link.cainiao.com/gateway/link.do"; public static object ShopKeyDics { get; private set; } public static void TestSend() { //json参数 var res_obj = new { }; string ro_json = JsonConvert.SerializeObject(res_obj); string logistics_interface = ro_json; string data_digest = DoSign(logistics_interface, "utf-8", appSecret); WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); wc.Encoding = Encoding.GetEncoding("utf-8"); NameValueCollection PostVars = new NameValueCollection(); PostVars.Add("logistics_interface", logistics_interface); PostVars.Add("logistic_provider_id", logistic_provider_id); PostVars.Add("data_digest", data_digest); PostVars.Add("msg_type", msg_type); if (!string.IsNullOrEmpty(to_code)) { PostVars.Add("to_code", to_code); } try { byte[] ret = wc.UploadValues(url, "POST", PostVars); string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret); // process result } catch (Exception ex) { // do something... } } public static string DoSign(string content, string charset, string appSecret) { string toSignContent = content + appSecret; System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] inputBytes = System.Text.Encoding.GetEncoding(charset).GetBytes(toSignContent); byte[] hash = md5.ComputeHash(inputBytes); return System.Convert.ToBase64String(hash); } //获取订购的地址 public static string TMS_WAYBILL_SUBSCRIPTION_QUERY(string cpcode) { //json参数 var res_obj = new { cpCode = cpcode }; string ro_json = JsonConvert.SerializeObject(res_obj); string logistics_interface = ro_json; string data_digest = DoSign(logistics_interface, "utf-8", appSecret); WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); wc.Encoding = Encoding.GetEncoding("utf-8"); NameValueCollection PostVars = new NameValueCollection(); PostVars.Add("logistics_interface", logistics_interface); PostVars.Add("logistic_provider_id", logistic_provider_id); PostVars.Add("data_digest", data_digest); PostVars.Add("msg_type", "TMS_WAYBILL_SUBSCRIPTION_QUERY"); if (!string.IsNullOrEmpty(to_code)) { PostVars.Add("to_code", to_code); } try { byte[] ret = wc.UploadValues(url, "POST", PostVars); string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret); return remoteInfo; //tms_waybill_subscription_query_res_Obj iObj = null; //iObj = JsonConvert.DeserializeObject(remoteInfo); //if (iObj != null) //{ // if (iObj.success=="true") // { // return remoteInfo; // } //} // process result } catch (Exception ex) { // do something... } return ""; } //云打印获取面单号 public static string TMS_WAYBILL_GET(string cpcode, string cpname, string tempUrl, string ctid) { //json参数 string sql = "select * from view_erptradecell where ctid='" + ctid + "'"; DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0]; if (dt != null && dt.Rows.Count > 0) { //获取订购地址 string add_res = TMS_WAYBILL_SUBSCRIPTION_QUERY(cpcode); if (add_res.Length <= 0) { return "未获取到面单订购地址"; } tms_waybill_subscription_query_res_Obj iObj = null; iObj = JsonConvert.DeserializeObject(add_res); if (iObj != null) { if (iObj.success == "false") { return add_res; } } ShippAddressCols add_obj = null; if (cpname.IndexOf("_") == -1) { cpname = cpname + "_龙岩市"; } ShippAddressCols nullToSel = null; if (iObj != null && iObj.waybillApplySubscriptionCols.Count > 0) { foreach (BranchAccountCols branch in iObj.waybillApplySubscriptionCols[0].branchAccountCols) { if (branch.shippAddressCols.Count > 0) { ShippAddressCols ao = branch.shippAddressCols[0]; if (cpname.IndexOf(ao.province) != -1 || cpname.IndexOf(ao.city) != -1) { add_obj = ao; } nullToSel = ao; } } } if (add_obj == null && nullToSel == null) { return "未获取到面单订购地址"; } else if (add_obj == null) { add_obj = nullToSel; } DataRow dr = dt.Rows[0]; string needId = dr["tid"].ToString(); string lastOutsid = dr["OutSid"].ToString(); if (lastOutsid.Length >= 0) { int last = Convert.ToInt32(needId.Substring(needId.Length - 1, 1)); last = last + lastOutsid.Length; needId = needId.Substring(0, needId.Length - 1) + Convert.ToString(last); } //json参数 var res_obj = new { //param_waybill_cloud_print_apply_new_request cpCode = cpcode, sender = new { name = "孙雯", mobile = "13062207441", address = new { province = add_obj.province, city = add_obj.city, district = add_obj.district, detail = add_obj.detail } }, tradeOrderInfoDtos = new[] { new{ objectId = needId, templateUrl = tempUrl, userId = 2201404768885, orderInfo = new { orderChannelsType = "TB", tradeOrderList = new[]{ needId } }, packageInfo = new { id = 0, items = new[] { new { count = 10, name = "印刷品" } } }, recipient = new { name = dr["receiver_name"].ToString(), mobile = dr["receiver_mobile"].ToString(), address = new { province = dr["receiver_state"].ToString(), city = dr["receiver_city"].ToString(), detail = dr["receiver_address"].ToString() } } } } }; string ro_json = JsonConvert.SerializeObject(res_obj); string logistics_interface = ro_json; string data_digest = DoSign(logistics_interface, "utf-8", appSecret); WebClient wc = new WebClient(); wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); wc.Encoding = Encoding.GetEncoding("utf-8"); NameValueCollection PostVars = new NameValueCollection(); PostVars.Add("logistics_interface", logistics_interface); PostVars.Add("logistic_provider_id", logistic_provider_id); PostVars.Add("data_digest", data_digest); PostVars.Add("msg_type", "TMS_WAYBILL_GET"); if (!string.IsNullOrEmpty(to_code)) { PostVars.Add("to_code", to_code); } try { byte[] ret = wc.UploadValues(url, "POST", PostVars); string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret); return remoteInfo; //tms_waybill_subscription_query_res_Obj iObj = null; //iObj = JsonConvert.DeserializeObject(remoteInfo); //if (iObj != null) //{ // if (iObj.success=="true") // { // return remoteInfo; // } //} // process result } catch (Exception ex) { // do something... } } return ""; } } }