| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- 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<tms_waybill_subscription_query_res_Obj>(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<tms_waybill_subscription_query_res_Obj>(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<tms_waybill_subscription_query_res_Obj>(remoteInfo);
- //if (iObj != null)
- //{
- // if (iObj.success=="true")
- // {
- // return remoteInfo;
- // }
- //}
- // process result
- }
- catch (Exception ex)
- {
- // do something...
- }
- }
- return "";
- }
- }
- }
|