apiHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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", 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 (res.Contains("上传额外运单信息成功"))
  220. {
  221. res = res.Replace("上传额外运单信息成功", "发货成功true");
  222. }
  223. return res;
  224. }
  225. public static string API_LogisticsDummySend(string orderid, string pCode)
  226. {
  227. List<string> postLst = new List<string>();
  228. var res_obj = new
  229. {
  230. refOid = orderid,
  231. posCode = pCode
  232. };
  233. string ro_json = JsonConvert.SerializeObject(res_obj);
  234. string res = Base_Request(ro_json, "ds.omni.erp.third.order.dummy.send");
  235. return res;
  236. }
  237. public static string API_PrintTemplate()
  238. {
  239. List<string> postLst = new List<string>();
  240. var res_obj = new
  241. {
  242. templateType = "EXPRESS",
  243. templateSource = "CAINIAO"
  244. };
  245. string ro_json = JsonConvert.SerializeObject(res_obj);
  246. string res = Base_Request(ro_json, "ds.omni.erp.print.template.query");
  247. return res;
  248. }
  249. public static string API_GetWaybill(string cpCd, string ctid)
  250. {
  251. string sql = "select * from view_erptradecell where ctid='" + ctid + "'";
  252. DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
  253. if (dt != null && dt.Rows.Count > 0)
  254. {
  255. List<string> postLst = new List<string>();
  256. Object receipts = null;
  257. string pos_code = dt.Rows[0]["posCode"].ToString();
  258. if (ctid.IndexOf("N") != -1)
  259. {
  260. pos_code = "guliang";
  261. receipts = new
  262. {
  263. province = dt.Rows[0]["receiver_state"].ToString(),
  264. city = dt.Rows[0]["receiver_city"].ToString(),
  265. district = dt.Rows[0]["receiver_district"].ToString(),
  266. town = dt.Rows[0]["receiver_town"].ToString(),
  267. detail = dt.Rows[0]["receiver_address"].ToString(),
  268. name = dt.Rows[0]["receiver_name"].ToString(),
  269. mobile = dt.Rows[0]["receiver_mobile"].ToString(),
  270. phone = dt.Rows[0]["receiver_phone"].ToString()
  271. };
  272. }
  273. var res_obj = new
  274. {
  275. cpCode = cpCd,
  276. packages = new[] {
  277. new{
  278. outerCode = dt.Rows[0]["ctid"].ToString(),
  279. posCode= pos_code,
  280. refOid = dt.Rows[0]["tid"].ToString(),
  281. items = new[] {
  282. new{
  283. name = "印刷品",
  284. num = 1
  285. }
  286. },
  287. receipt = receipts == null?new{ }:receipts
  288. }
  289. }
  290. };
  291. string ro_json = JsonConvert.SerializeObject(res_obj);
  292. string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.get");
  293. return res;
  294. }
  295. return "";
  296. }
  297. public static string API_GetPrintData(string waybill)
  298. {
  299. List<string> postLst = new List<string>();
  300. var res_obj = new
  301. {
  302. waybillCode = new[] {
  303. waybill
  304. }
  305. };
  306. string ro_json = JsonConvert.SerializeObject(res_obj);
  307. string res = Base_Request(ro_json, "ds.omni.erp.waybill.printdata.get");
  308. return res;
  309. }
  310. public static string Api_SyncOrderByTime(string posId)
  311. {
  312. List<string> postLst = new List<string>();
  313. string starttime = DateTime.Now.AddMinutes(-35).ToString("yyyy-MM-dd HH:mm:ss");
  314. string endtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  315. var res_obj = new
  316. {
  317. startTime = starttime,
  318. endTime = endtime,
  319. timeType = "CREATE_TIME",
  320. posId = posId
  321. };
  322. string ro_json = JsonConvert.SerializeObject(res_obj);
  323. string res = Base_Request(ro_json, "ds.omni.erp.third.order.sync.byTime");
  324. return res;
  325. }
  326. public static string API_GetPrintData_SF(string waybill)
  327. {
  328. List<string> postLst = new List<string>();
  329. var res_obj = new
  330. {
  331. request = new
  332. {
  333. waybillCodes = new[] {
  334. waybill
  335. }
  336. }
  337. };
  338. string ro_json = JsonConvert.SerializeObject(res_obj);
  339. string res = Base_Request_SF(ro_json, "omni.print.open.appOpenPrintData.getExpressList");
  340. return res;
  341. }
  342. public static string API_GetPrintPage(string waybillCode, string tempId, string reqId)
  343. {
  344. string appKey = appid;
  345. string secret = appSecret;
  346. List<string> postLst = new List<string>();
  347. postLst.Add("waybillCodeList=" + waybillCode);
  348. postLst.Add("templateId=" + tempId);
  349. postLst.Add("requestId=" + reqId);
  350. postLst.Add("sourceType=THIRD_ORDER");
  351. int sc = DateTimeToUnixTime(DateTime.Now);
  352. string redPageUrl = printPage_redirectUrl + "?" + string.Join("&", postLst.ToArray());
  353. string en_redPageUrl = HttpUtility.UrlEncode(redPageUrl);
  354. string toSignStr = secret + "appKey" + appKey + "redirectUrl" + en_redPageUrl + "timestamp" + sc + secret;
  355. MD5 md5 = MD5.Create();
  356. byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(toSignStr));
  357. StringBuilder result = new StringBuilder();
  358. for (int i = 0; i < bytes.Length; i++)
  359. {
  360. result.Append(bytes[i].ToString("x2"));
  361. }
  362. string sign = result.ToString();
  363. string toAuthUrl = "http://d3.diansan.com/app-web/open/tenant/auth?appKey=" + appKey + "&redirectUrl=" + en_redPageUrl + "&timestamp=" + sc + "&sign=" + sign;
  364. return toAuthUrl;
  365. }
  366. public static string API_CancelPrint(string waybillCode)
  367. {
  368. var res_obj = new
  369. {
  370. waybillCode
  371. };
  372. string ro_json = JsonConvert.SerializeObject(res_obj);
  373. string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.cancel");
  374. return res;
  375. }
  376. public static List<ContentItem> API_GetUserId(string waybillCode, string nickName)
  377. {
  378. var res_obj = new
  379. {
  380. posCode = waybillCode,
  381. buyerNicks = nickName
  382. };
  383. string ro_json = JsonConvert.SerializeObject(res_obj);
  384. string res = Base_Request(ro_json, "ds.tb.user.openuid.get");
  385. List<ContentItem> resultList = new List<ContentItem>();
  386. if (res == "")
  387. {
  388. return resultList;
  389. }
  390. try
  391. {
  392. api_userId_response api = JsonConvert.DeserializeObject<api_userId_response>(res);
  393. if (api != null && api.response.code == 200 && api.response.data.content != null && api.response.data.content.Count > 0)
  394. {
  395. resultList = api.response.data.content;
  396. }
  397. }
  398. catch (Exception ex)
  399. {
  400. }
  401. return resultList;
  402. }
  403. }
  404. }