apiHelper.cs 17 KB

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