| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- using Newtonsoft.Json;
- using NHibernate.Type;
- using SQLData;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- namespace SiteCore
- {
- public class apiHelper
- {
- //正式appkey
- static string appid = "221114";
- static string appSecret = "gc5y5of12pz2idvqx2lh4jo6bj5fn0ud";
- //测试appkey
- //static string appid = "221111";
- //static string appSecret = "xdxkow01r5uvvpn53tojwuer5laba8zb";
- //static string main_url = "http://open_3rd.dev.diansan.com/open/oms/router";
- static string main_url = "http://open_3rd.product.diansan.com/open/oms/router";
- static string printPage_redirectUrl = "http://d3.diansan.com/d3/open/print/express/index.html";
- static HttpHelper tb_http = new HttpHelper();
- private static object obj = new object();
- #region private
- private static string SignTopRequest(IDictionary<string, string> parameters, string jsonPara, string secret, string signMethod, bool notBody = false)
- {
- IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);
- IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
- string str_query = "";
- if (signMethod == "md5")
- {
- //query.Append(secret);
- str_query += secret;
- }
- while (dem.MoveNext())
- {
- string key = dem.Current.Key;
- string value = dem.Current.Value;
- if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
- {
- str_query += key;
- str_query += value;
- }
- }
- if (notBody == false)
- {
- str_query += jsonPara;
- }
- byte[] bytes;
- str_query += secret;
- MD5 md5 = MD5.Create();
- bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str_query));
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- result.Append(bytes[i].ToString("X2"));
- }
- return result.ToString();
- }
- private static string Md5(string s)
- {
- using (var md5 = MD5.Create())
- {
- var result = md5.ComputeHash(Encoding.Default.GetBytes(s));
- var strResult = BitConverter.ToString(result);
- return strResult.Replace("-", "").ToUpper();
- }
- }
- public static int DateTimeToUnixTime(DateTime dateTime)
- {
- return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
- }
- #endregion
- public static string Base_Request(string jsonParams, string methodStr)
- {
- int sc = DateTimeToUnixTime(DateTime.Now);
- Dictionary<string, string> postParamList = new Dictionary<string, string>();
- postParamList.Add("appKey", appid);
- postParamList.Add("timestamp", sc.ToString());
- postParamList.Add("method", methodStr);
- //postParamList.Add("_test", "true");
- //json参数
- List<string> postLst = new List<string>();
- string ro_json = jsonParams;
- string signStr = SignTopRequest(postParamList, ro_json, appSecret, "md5");
- postParamList.Add("sign", signStr);
- //post参数
- //postLst.Add("requestObjectJson=" + HttpUtility.UrlEncode(ro_json));
- postLst.Add("method=" + methodStr);
- postLst.Add("appKey=" + appid);
- postLst.Add("timestamp=" + sc);
- //postLst.Add("_test=true");
- postLst.Add("sign=" + signStr);
- //postLst.Add("requestObjectJson=" + HttpUtility.UrlEncode(ro_json));
- string pUrl = main_url + "?" + string.Join("&", postLst.ToArray());
- HttpItem item = new HttpItem()
- {
- URL = pUrl,
- Method = "POST",
- ContentType = "application/x-www-form-urlencoded",
- Postdata = ro_json
- };
- item.PostEncoding = Encoding.UTF8;
- HttpResult hResult = null;
- lock (obj)
- {
- hResult = tb_http.GetHtml(item);
- }
- return hResult.Html;
- }
- public static string Base_Request_SF(string jsonParams, string methodStr)
- {
- int sc = DateTimeToUnixTime(DateTime.Now);
- Dictionary<string, string> postParamList = new Dictionary<string, string>();
- postParamList.Add("appKey", appid);
- postParamList.Add("timestamp", sc.ToString());
- postParamList.Add("method", methodStr);
- //postParamList.Add("_test", "true");
- List<string> postLst = new List<string>();
- string ro_json = jsonParams;
- string signStr = SignTopRequest(postParamList, ro_json, appSecret, "md5", true);
- postParamList.Add("sign", signStr);
- postLst.Add("method=" + methodStr);
- postLst.Add("appKey=" + appid);
- postLst.Add("timestamp=" + sc);
- //postLst.Add("_test=true");
- postLst.Add("sign=" + signStr);
- string pUrl = "http://d3.diansan.com/app-web/open/router/rest.json" + "?" + string.Join("&", postLst.ToArray());
- HttpItem item = new HttpItem()
- {
- URL = pUrl,
- Method = "POST",
- ContentType = "application/x-www-form-urlencoded",
- Postdata = ro_json
- };
- item.PostEncoding = Encoding.UTF8;
- HttpResult hResult = tb_http.GetHtml(item);
- return hResult.Html;
- }
- public static string API_TradeFullinfoGet(string tid, string sTime = "", string eTime = "")
- {
- //json参数
- List<string> postLst = new List<string>();
- object jsonPara = null;
- if (tid.Length > 0)
- {
- var res_obj = new
- {
- refOid = tid,
- timeType = 2
- };
- jsonPara = res_obj;
- }
- else
- {
- var res_obj = new
- {
- startTime = sTime,
- endTime = eTime,
- timeType = 2
- };
- jsonPara = res_obj;
- }
- string ro_json = JsonConvert.SerializeObject(jsonPara);
- string res = Base_Request(ro_json, "ds.omni.erp.third.order.query");
- return res;
- //return html;
- }
- public static string API_TradeMemoUpdate(string atid, string pCode, string aflag, string amemo)
- {
- var res_obj = new
- {
- refOid = atid,
- flag = aflag,
- memo = amemo,
- posCode = pCode
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.third.order.memo.update");
- return res;
- }
- public static string API_LogisticsOnlineSend(string orderid, string pCode, string comCode, string out_Sid)
- {
- List<string> postLst = new List<string>();
- var res_obj = new
- {
- refOid = orderid,
- posCode = pCode,
- packages = new[]
- {
- new{ outSid=out_Sid,companyCode=comCode}
- }
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.third.order.send");
- return res;
- }
- public static string API_LogisticsDummySend(string orderid, string pCode)
- {
- List<string> postLst = new List<string>();
- var res_obj = new
- {
- refOid = orderid,
- posCode = pCode
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.third.order.dummy.send");
- return res;
- }
- public static string API_PrintTemplate()
- {
- List<string> postLst = new List<string>();
- var res_obj = new
- {
- templateType = "EXPRESS",
- templateSource = "CAINIAO"
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.print.template.query");
- return res;
- }
- public static string API_GetWaybill(string cpCd, string ctid)
- {
- string sql = "select * from view_erptradecell where ctid='" + ctid + "'";
- DataTable dt = DbHelper.DbConn.ExecuteDataset(sql).Tables[0];
- if (dt != null && dt.Rows.Count > 0)
- {
- List<string> postLst = new List<string>();
- Object receipts = null;
- string pos_code = dt.Rows[0]["posCode"].ToString();
- if (ctid.IndexOf("N") != -1)
- {
- pos_code = "guliang";
- receipts = new
- {
- province = dt.Rows[0]["receiver_state"].ToString(),
- city = dt.Rows[0]["receiver_city"].ToString(),
- district = dt.Rows[0]["receiver_district"].ToString(),
- town = dt.Rows[0]["receiver_town"].ToString(),
- detail = dt.Rows[0]["receiver_address"].ToString(),
- name = dt.Rows[0]["receiver_name"].ToString(),
- mobile = dt.Rows[0]["receiver_mobile"].ToString(),
- phone = dt.Rows[0]["receiver_phone"].ToString()
- };
- }
- var res_obj = new
- {
- cpCode = cpCd,
- packages = new[] {
- new{
- outerCode = dt.Rows[0]["ctid"].ToString(),
- posCode= pos_code,
- refOid = dt.Rows[0]["tid"].ToString(),
- items = new[] {
- new{
- name = "印刷品",
- num = 1
- }
- },
- receipt = receipts == null?new{ }:receipts
- }
- }
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.get");
- return res;
- }
- return "";
- }
- public static string API_GetPrintData(string waybill)
- {
- List<string> postLst = new List<string>();
- var res_obj = new
- {
- waybillCode = new[] {
- waybill
- }
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.waybill.printdata.get");
- return res;
- }
- public static string Api_SyncOrderByTime(string posId)
- {
- List<string> postLst = new List<string>();
- string starttime = DateTime.Now.AddMinutes(-35).ToString("yyyy-MM-dd HH:mm:ss");
- string endtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- var res_obj = new
- {
- startTime = starttime,
- endTime = endtime,
- timeType = "CREATE_TIME",
- posId = posId
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.third.order.sync.byTime");
- return res;
- }
- public static string API_GetPrintData_SF(string waybill)
- {
- List<string> postLst = new List<string>();
- var res_obj = new
- {
- request = new
- {
- waybillCodes = new[] {
- waybill
- }
- }
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request_SF(ro_json, "omni.print.open.appOpenPrintData.getExpressList");
- return res;
- }
- public static string API_GetPrintPage(string waybillCode, string tempId, string reqId)
- {
- string appKey = appid;
- string secret = appSecret;
- List<string> postLst = new List<string>();
- postLst.Add("waybillCodeList=" + waybillCode);
- postLst.Add("templateId=" + tempId);
- postLst.Add("requestId=" + reqId);
- postLst.Add("sourceType=THIRD_ORDER");
- int sc = DateTimeToUnixTime(DateTime.Now);
- string redPageUrl = printPage_redirectUrl + "?" + string.Join("&", postLst.ToArray());
- string en_redPageUrl = HttpUtility.UrlEncode(redPageUrl);
- string toSignStr = secret + "appKey" + appKey + "redirectUrl" + en_redPageUrl + "timestamp" + sc + secret;
- MD5 md5 = MD5.Create();
- byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(toSignStr));
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- result.Append(bytes[i].ToString("x2"));
- }
- string sign = result.ToString();
- string toAuthUrl = "http://d3.diansan.com/app-web/open/tenant/auth?appKey=" + appKey + "&redirectUrl=" + en_redPageUrl + "×tamp=" + sc + "&sign=" + sign;
- return toAuthUrl;
- }
- public static string API_CancelPrint(string waybillCode)
- {
- var res_obj = new
- {
- waybillCode
- };
- string ro_json = JsonConvert.SerializeObject(res_obj);
- string res = Base_Request(ro_json, "ds.omni.erp.waybill.third.cancel");
- return res;
- }
- }
- }
|