apiHelper.cs 14 KB

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