apiMFHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using Newtonsoft.Json;
  2. using SQLData;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Web;
  9. namespace SiteCore
  10. {
  11. public class apiMFHelper
  12. {
  13. //正式appkey
  14. static string appid = "221296";
  15. static string appSecret = "9dea1dea5b36a2517150388eee8a5b04";
  16. //测试appkey
  17. //static string appid = "221111";
  18. //static string appSecret = "xdxkow01r5uvvpn53tojwuer5laba8zb";
  19. //static string main_url = "http://open_3rd.dev.diansan.com/open/oms/router";
  20. static string main_url = "http://open_3rd.product.diansan.com/open/oms/router";
  21. static string printPage_redirectUrl = "http://d3.diansan.com/d3/open/print/express/index.html";
  22. static HttpHelper tb_http = new HttpHelper();
  23. private static object obj = new object();
  24. #region private
  25. private static string SignTopRequest(IDictionary<string, string> parameters, string jsonPara, string secret, string signMethod, bool notBody = false)
  26. {
  27. IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);
  28. IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
  29. string str_query = "";
  30. if (signMethod == "md5")
  31. {
  32. //query.Append(secret);
  33. str_query += secret;
  34. }
  35. while (dem.MoveNext())
  36. {
  37. string key = dem.Current.Key;
  38. string value = dem.Current.Value;
  39. if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
  40. {
  41. str_query += key;
  42. str_query += value;
  43. }
  44. }
  45. if (notBody == false)
  46. {
  47. str_query += jsonPara;
  48. }
  49. byte[] bytes;
  50. str_query += secret;
  51. MD5 md5 = MD5.Create();
  52. bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str_query));
  53. StringBuilder result = new StringBuilder();
  54. for (int i = 0; i < bytes.Length; i++)
  55. {
  56. result.Append(bytes[i].ToString("X2"));
  57. }
  58. return result.ToString();
  59. }
  60. private static string Md5(string s)
  61. {
  62. using (var md5 = MD5.Create())
  63. {
  64. var result = md5.ComputeHash(Encoding.Default.GetBytes(s));
  65. var strResult = BitConverter.ToString(result);
  66. return strResult.Replace("-", "").ToUpper();
  67. }
  68. }
  69. public static int DateTimeToUnixTime(DateTime dateTime)
  70. {
  71. return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
  72. }
  73. #endregion
  74. public static string Base_Request(string jsonParams, string methodStr)
  75. {
  76. int sc = DateTimeToUnixTime(DateTime.Now);
  77. Dictionary<string, string> postParamList = new Dictionary<string, string>();
  78. postParamList.Add("appKey", appid);
  79. postParamList.Add("timestamp", sc.ToString());
  80. postParamList.Add("method", methodStr);
  81. //postParamList.Add("_test", "true");
  82. //json参数
  83. List<string> postLst = new List<string>();
  84. string ro_json = jsonParams;
  85. string signStr = SignTopRequest(postParamList, ro_json, appSecret, "md5");
  86. postParamList.Add("sign", signStr);
  87. //post参数
  88. //postLst.Add("requestObjectJson=" + HttpUtility.UrlEncode(ro_json));
  89. postLst.Add("method=" + methodStr);
  90. postLst.Add("appKey=" + appid);
  91. postLst.Add("timestamp=" + sc);
  92. //postLst.Add("_test=true");
  93. postLst.Add("sign=" + signStr);
  94. //postLst.Add("requestObjectJson=" + HttpUtility.UrlEncode(ro_json));
  95. string pUrl = main_url + "?" + string.Join("&", postLst.ToArray());
  96. HttpItem item = new HttpItem()
  97. {
  98. URL = pUrl,
  99. Method = "POST",
  100. ContentType = "application/x-www-form-urlencoded",
  101. Postdata = ro_json
  102. };
  103. item.PostEncoding = Encoding.UTF8;
  104. HttpResult hResult = null;
  105. lock (obj)
  106. {
  107. hResult = tb_http.GetHtml(item);
  108. }
  109. return hResult.Html;
  110. }
  111. public static string Base_Request_SF(string jsonParams, string methodStr)
  112. {
  113. int sc = DateTimeToUnixTime(DateTime.Now);
  114. Dictionary<string, string> postParamList = new Dictionary<string, string>();
  115. postParamList.Add("appKey", appid);
  116. postParamList.Add("timestamp", sc.ToString());
  117. postParamList.Add("method", methodStr);
  118. //postParamList.Add("_test", "true");
  119. List<string> postLst = new List<string>();
  120. string ro_json = jsonParams;
  121. string signStr = SignTopRequest(postParamList, ro_json, appSecret, "md5", true);
  122. postParamList.Add("sign", signStr);
  123. postLst.Add("method=" + methodStr);
  124. postLst.Add("appKey=" + appid);
  125. postLst.Add("timestamp=" + sc);
  126. //postLst.Add("_test=true");
  127. postLst.Add("sign=" + signStr);
  128. string pUrl = "http://d3.diansan.com/app-web/open/router/rest.json" + "?" + string.Join("&", postLst.ToArray());
  129. HttpItem item = new HttpItem()
  130. {
  131. URL = pUrl,
  132. Method = "POST",
  133. ContentType = "application/x-www-form-urlencoded",
  134. Postdata = ro_json
  135. };
  136. item.PostEncoding = Encoding.UTF8;
  137. HttpResult hResult = tb_http.GetHtml(item);
  138. return hResult.Html;
  139. }
  140. public static string API_TradeFullinfoGet(string tid, string sTime = "", string eTime = "")
  141. {
  142. //json参数
  143. List<string> postLst = new List<string>();
  144. object jsonPara = null;
  145. if (tid.Length > 0)
  146. {
  147. var res_obj = new
  148. {
  149. refOid = tid,
  150. timeType = 2
  151. };
  152. jsonPara = res_obj;
  153. }
  154. else
  155. {
  156. var res_obj = new
  157. {
  158. startTime = sTime,
  159. endTime = eTime,
  160. timeType = 2
  161. };
  162. jsonPara = res_obj;
  163. }
  164. string ro_json = JsonConvert.SerializeObject(jsonPara);
  165. string res = Base_Request(ro_json, "ds.omni.erp.third.order.query");
  166. return res;
  167. //return html;
  168. }
  169. public static string API_TradeMemoUpdate(string atid, string pCode, string aflag, string amemo)
  170. {
  171. var res_obj = new
  172. {
  173. refOid = atid,
  174. flag = aflag,
  175. memo = amemo,
  176. posCode = pCode
  177. };
  178. string ro_json = JsonConvert.SerializeObject(res_obj);
  179. string res = Base_Request(ro_json, "ds.omni.erp.third.order.memo.update");
  180. return res;
  181. }
  182. public static string API_LogisticsOnlineSend(string orderid, string pCode, string comCode, string out_Sid)
  183. {
  184. List<string> postLst = new List<string>();
  185. var res_obj = new
  186. {
  187. refOid = orderid,
  188. posCode = pCode,
  189. packages = new[]
  190. {
  191. new{ outSid=out_Sid,companyCode=comCode}
  192. }
  193. };
  194. string ro_json = JsonConvert.SerializeObject(res_obj);
  195. string res = Base_Request(ro_json, "ds.omni.erp.third.order.send");
  196. return res;
  197. }
  198. public static string API_LogisticsDummySend(string orderid, string pCode)
  199. {
  200. List<string> postLst = new List<string>();
  201. var res_obj = new
  202. {
  203. refOid = orderid,
  204. posCode = pCode
  205. };
  206. string ro_json = JsonConvert.SerializeObject(res_obj);
  207. string res = Base_Request(ro_json, "ds.omni.erp.third.order.dummy.send");
  208. return res;
  209. }
  210. public static string API_PrintTemplate()
  211. {
  212. List<string> postLst = new List<string>();
  213. var res_obj = new
  214. {
  215. templateType = "EXPRESS",
  216. templateSource = "CAINIAO"
  217. };
  218. string ro_json = JsonConvert.SerializeObject(res_obj);
  219. string res = Base_Request(ro_json, "ds.omni.erp.print.template.query");
  220. return res;
  221. }
  222. public static string API_GetWaybill(string cpCd, string ctid)
  223. {
  224. string sql = "select * from view_erptradecell where ctid='" + ctid + "'";
  225. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  226. if (dt != null && dt.Rows.Count > 0)
  227. {
  228. List<string> postLst = new List<string>();
  229. Object receipts = null;
  230. string pos_code = dt.Rows[0]["posCode"].ToString();
  231. if (ctid.IndexOf("N") != -1)
  232. {
  233. pos_code = "guliang";
  234. receipts = new
  235. {
  236. province = dt.Rows[0]["receiver_state"].ToString(),
  237. city = dt.Rows[0]["receiver_city"].ToString(),
  238. district = dt.Rows[0]["receiver_district"].ToString(),
  239. town = dt.Rows[0]["receiver_town"].ToString(),
  240. detail = dt.Rows[0]["receiver_address"].ToString(),
  241. name = dt.Rows[0]["receiver_name"].ToString(),
  242. mobile = dt.Rows[0]["receiver_mobile"].ToString(),
  243. phone = dt.Rows[0]["receiver_phone"].ToString()
  244. };
  245. }
  246. var res_obj = new
  247. {
  248. cpCode = cpCd,
  249. packages = new[] {
  250. new{
  251. outerCode = dt.Rows[0]["ctid"].ToString(),
  252. posCode= pos_code,
  253. refOid = dt.Rows[0]["tid"].ToString(),
  254. items = new[] {
  255. new{
  256. name = "印刷品",
  257. num = 1
  258. }
  259. },
  260. receipt = receipts == null?new{ }:receipts
  261. }
  262. }
  263. };
  264. string ro_json = JsonConvert.SerializeObject(res_obj);
  265. string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.get");
  266. return res;
  267. }
  268. return "";
  269. }
  270. public static string API_GetPrintData(string waybill)
  271. {
  272. List<string> postLst = new List<string>();
  273. var res_obj = new
  274. {
  275. waybillCode = new[] {
  276. waybill
  277. }
  278. };
  279. string ro_json = JsonConvert.SerializeObject(res_obj);
  280. string res = Base_Request(ro_json, "ds.omni.erp.waybill.printdata.get");
  281. return res;
  282. }
  283. public static string API_GetPrintData_SF(string waybill)
  284. {
  285. List<string> postLst = new List<string>();
  286. var res_obj = new
  287. {
  288. request = new
  289. {
  290. waybillCodes = new[] {
  291. waybill
  292. }
  293. }
  294. };
  295. string ro_json = JsonConvert.SerializeObject(res_obj);
  296. string res = Base_Request_SF(ro_json, "omni.print.open.appOpenPrintData.getExpressList");
  297. return res;
  298. }
  299. public static string API_GetPrintPage(string waybillCode, string tempId, string reqId)
  300. {
  301. string appKey = appid;
  302. string secret = appSecret;
  303. List<string> postLst = new List<string>();
  304. postLst.Add("waybillCodeList=" + waybillCode);
  305. postLst.Add("templateId=" + tempId);
  306. postLst.Add("requestId=" + reqId);
  307. postLst.Add("sourceType=THIRD_ORDER");
  308. int sc = DateTimeToUnixTime(DateTime.Now);
  309. string redPageUrl = printPage_redirectUrl + "?" + string.Join("&", postLst.ToArray());
  310. string en_redPageUrl = HttpUtility.UrlEncode(redPageUrl);
  311. string toSignStr = secret + "appKey" + appKey + "redirectUrl" + en_redPageUrl + "timestamp" + sc + secret;
  312. MD5 md5 = MD5.Create();
  313. byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(toSignStr));
  314. StringBuilder result = new StringBuilder();
  315. for (int i = 0; i < bytes.Length; i++)
  316. {
  317. result.Append(bytes[i].ToString("x2"));
  318. }
  319. string sign = result.ToString();
  320. string toAuthUrl = "http://d3.diansan.com/app-web/open/tenant/auth?appKey=" + appKey + "&redirectUrl=" + en_redPageUrl + "&timestamp=" + sc + "&sign=" + sign;
  321. return toAuthUrl;
  322. }
  323. public static string API_CancelPrint(string waybillCode)
  324. {
  325. var res_obj = new
  326. {
  327. waybillCode
  328. };
  329. string ro_json = JsonConvert.SerializeObject(res_obj);
  330. string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.cancel");
  331. return res;
  332. }
  333. }
  334. }