apiHelper.cs 15 KB

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