cainiaoLink.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using Newtonsoft.Json;
  2. using SiteCore.taoObj;
  3. using SQLData;
  4. using System;
  5. using System.Collections.Specialized;
  6. using System.Data;
  7. using System.Net;
  8. using System.Text;
  9. using static SiteCore.taoObj.tms_waybill_subscription_query_res_Obj;
  10. namespace SiteCore
  11. {
  12. public class cainiaoLink
  13. {
  14. //领淘网络新的token//Z0haTGJZTStPL2prMmdEcmFHTklvajVOcDN5T01HVEQvSS9Sci9HUjZob1lGTVd0dUVxUHd5MFNPNEwydm8ycA==
  15. private const string logistic_provider_id = "TEZNRDkvTTFSTFVUWm1MeGduS2FQeFF2d0NIbGxIdldoUWtuaXhHVVFYRWJFUkJrTUorSUkyQW45a2Q1Q0NLaA=="; // token
  16. //private const string logistic_provider_id = "Z0haTGJZTStPL2prMmdEcmFHTklvajVOcDN5T01HVEQvSS9Sci9HUjZob1lGTVd0dUVxUHd5MFNPNEwydm8ycA=="; // token
  17. private const string msg_type = "TMS_WAYBILL_SUBSCRIPTION_QUERY"; // 调用的API
  18. private const string to_code = ""; // 被调用方的code
  19. private const string appSecret = "7GR2GzO6p2LY4jO9hI5M0htWt28824Df"; // 应用的AppSecret
  20. //private const string appSecret = "4U7Xxbnj5SE5V9V045scqLB189M5Et0J"; // 应用的AppSecret
  21. private const string url = "http://link.cainiao.com/gateway/link.do";
  22. public static object ShopKeyDics { get; private set; }
  23. public static void TestSend()
  24. {
  25. //json参数
  26. var res_obj = new
  27. {
  28. };
  29. string ro_json = JsonConvert.SerializeObject(res_obj);
  30. string logistics_interface = ro_json;
  31. string data_digest = DoSign(logistics_interface, "utf-8", appSecret);
  32. WebClient wc = new WebClient();
  33. wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  34. wc.Encoding = Encoding.GetEncoding("utf-8");
  35. NameValueCollection PostVars = new NameValueCollection();
  36. PostVars.Add("logistics_interface", logistics_interface);
  37. PostVars.Add("logistic_provider_id", logistic_provider_id);
  38. PostVars.Add("data_digest", data_digest);
  39. PostVars.Add("msg_type", msg_type);
  40. if (!string.IsNullOrEmpty(to_code))
  41. {
  42. PostVars.Add("to_code", to_code);
  43. }
  44. try
  45. {
  46. byte[] ret = wc.UploadValues(url, "POST", PostVars);
  47. string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret);
  48. // process result
  49. }
  50. catch (Exception ex)
  51. {
  52. // do something...
  53. }
  54. }
  55. public static string DoSign(string content, string charset, string appSecret)
  56. {
  57. string toSignContent = content + appSecret;
  58. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  59. byte[] inputBytes = System.Text.Encoding.GetEncoding(charset).GetBytes(toSignContent);
  60. byte[] hash = md5.ComputeHash(inputBytes);
  61. return System.Convert.ToBase64String(hash);
  62. }
  63. //获取订购的地址
  64. public static string TMS_WAYBILL_SUBSCRIPTION_QUERY(string cpcode)
  65. {
  66. //json参数
  67. var res_obj = new
  68. {
  69. cpCode = cpcode
  70. };
  71. string ro_json = JsonConvert.SerializeObject(res_obj);
  72. string logistics_interface = ro_json;
  73. string data_digest = DoSign(logistics_interface, "utf-8", appSecret);
  74. WebClient wc = new WebClient();
  75. wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  76. wc.Encoding = Encoding.GetEncoding("utf-8");
  77. NameValueCollection PostVars = new NameValueCollection();
  78. PostVars.Add("logistics_interface", logistics_interface);
  79. PostVars.Add("logistic_provider_id", logistic_provider_id);
  80. PostVars.Add("data_digest", data_digest);
  81. PostVars.Add("msg_type", "TMS_WAYBILL_SUBSCRIPTION_QUERY");
  82. if (!string.IsNullOrEmpty(to_code))
  83. {
  84. PostVars.Add("to_code", to_code);
  85. }
  86. try
  87. {
  88. byte[] ret = wc.UploadValues(url, "POST", PostVars);
  89. string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret);
  90. return remoteInfo;
  91. //tms_waybill_subscription_query_res_Obj iObj = null;
  92. //iObj = JsonConvert.DeserializeObject<tms_waybill_subscription_query_res_Obj>(remoteInfo);
  93. //if (iObj != null)
  94. //{
  95. // if (iObj.success=="true")
  96. // {
  97. // return remoteInfo;
  98. // }
  99. //}
  100. // process result
  101. }
  102. catch (Exception ex)
  103. {
  104. // do something...
  105. }
  106. return "";
  107. }
  108. //云打印获取面单号
  109. public static string TMS_WAYBILL_GET(string cpcode, string cpname, string tempUrl, string ctid)
  110. {
  111. //json参数
  112. string sql = "select * from view_erptradecell where ctid='" + ctid + "'";
  113. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  114. if (dt != null && dt.Rows.Count > 0)
  115. {
  116. //获取订购地址
  117. string add_res = TMS_WAYBILL_SUBSCRIPTION_QUERY(cpcode);
  118. if (add_res.Length <= 0)
  119. {
  120. return "未获取到面单订购地址";
  121. }
  122. tms_waybill_subscription_query_res_Obj iObj = null;
  123. iObj = JsonConvert.DeserializeObject<tms_waybill_subscription_query_res_Obj>(add_res);
  124. if (iObj != null)
  125. {
  126. if (iObj.success == "false")
  127. {
  128. return add_res;
  129. }
  130. }
  131. ShippAddressCols add_obj = null;
  132. if (cpname.IndexOf("_") == -1)
  133. {
  134. cpname = cpname + "_龙岩市";
  135. }
  136. ShippAddressCols nullToSel = null;
  137. if (iObj != null && iObj.waybillApplySubscriptionCols.Count > 0)
  138. {
  139. foreach (BranchAccountCols branch in iObj.waybillApplySubscriptionCols[0].branchAccountCols)
  140. {
  141. if (branch.shippAddressCols.Count > 0)
  142. {
  143. ShippAddressCols ao = branch.shippAddressCols[0];
  144. if (cpname.IndexOf(ao.province) != -1 || cpname.IndexOf(ao.city) != -1)
  145. {
  146. add_obj = ao;
  147. }
  148. nullToSel = ao;
  149. }
  150. }
  151. }
  152. if (add_obj == null && nullToSel == null)
  153. {
  154. return "未获取到面单订购地址";
  155. }
  156. else if (add_obj == null)
  157. {
  158. add_obj = nullToSel;
  159. }
  160. DataRow dr = dt.Rows[0];
  161. string needId = dr["tid"].ToString();
  162. string lastOutsid = dr["OutSid"].ToString();
  163. if (lastOutsid.Length >= 0)
  164. {
  165. int last = Convert.ToInt32(needId.Substring(needId.Length - 1, 1));
  166. last = last + lastOutsid.Length;
  167. needId = needId.Substring(0, needId.Length - 1) + Convert.ToString(last);
  168. }
  169. //json参数
  170. var res_obj = new
  171. {
  172. //param_waybill_cloud_print_apply_new_request
  173. cpCode = cpcode,
  174. sender = new
  175. {
  176. name = "孙雯",
  177. mobile = "13062207441",
  178. address = new
  179. {
  180. province = add_obj.province,
  181. city = add_obj.city,
  182. district = add_obj.district,
  183. detail = add_obj.detail
  184. }
  185. },
  186. tradeOrderInfoDtos = new[]
  187. {
  188. new{
  189. objectId = needId,
  190. templateUrl = tempUrl,
  191. userId = 2201404768885,
  192. orderInfo = new
  193. {
  194. orderChannelsType = "TB",
  195. tradeOrderList = new[]{
  196. needId
  197. }
  198. },
  199. packageInfo = new
  200. {
  201. id = 0,
  202. items = new[]
  203. {
  204. new
  205. {
  206. count = 10,
  207. name = "印刷品"
  208. }
  209. }
  210. },
  211. recipient = new
  212. {
  213. name = dr["receiver_name"].ToString(),
  214. mobile = dr["receiver_mobile"].ToString(),
  215. address = new
  216. {
  217. province = dr["receiver_state"].ToString(),
  218. city = dr["receiver_city"].ToString(),
  219. detail = dr["receiver_address"].ToString()
  220. }
  221. }
  222. }
  223. }
  224. };
  225. string ro_json = JsonConvert.SerializeObject(res_obj);
  226. string logistics_interface = ro_json;
  227. string data_digest = DoSign(logistics_interface, "utf-8", appSecret);
  228. WebClient wc = new WebClient();
  229. wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  230. wc.Encoding = Encoding.GetEncoding("utf-8");
  231. NameValueCollection PostVars = new NameValueCollection();
  232. PostVars.Add("logistics_interface", logistics_interface);
  233. PostVars.Add("logistic_provider_id", logistic_provider_id);
  234. PostVars.Add("data_digest", data_digest);
  235. PostVars.Add("msg_type", "TMS_WAYBILL_GET");
  236. if (!string.IsNullOrEmpty(to_code))
  237. {
  238. PostVars.Add("to_code", to_code);
  239. }
  240. try
  241. {
  242. byte[] ret = wc.UploadValues(url, "POST", PostVars);
  243. string remoteInfo = Encoding.GetEncoding("utf-8").GetString(ret);
  244. return remoteInfo;
  245. //tms_waybill_subscription_query_res_Obj iObj = null;
  246. //iObj = JsonConvert.DeserializeObject<tms_waybill_subscription_query_res_Obj>(remoteInfo);
  247. //if (iObj != null)
  248. //{
  249. // if (iObj.success=="true")
  250. // {
  251. // return remoteInfo;
  252. // }
  253. //}
  254. // process result
  255. }
  256. catch (Exception ex)
  257. {
  258. // do something...
  259. }
  260. }
  261. return "";
  262. }
  263. }
  264. }