apiHelper.cs 15 KB

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