apiHelper.cs 16 KB

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