public string GetAlibabaOrder(string Base_APIAccountID, string GFF_CustomerID, string StrDate, string EndDate) { string retstr = ""; string aop_signature = ""; try { ArrayList arr = new ArrayList(); ArrayList arrs = new ArrayList(); Order_API_Field api = new Order_API_Field(); int okcount = 0; int nocount = 0; ArrayList arrp = new ArrayList(); ArrayList arrpn = new ArrayList(); var client = new WebClient(); string ret = ""; DataTable dt = C_c.SelectT("select * from dbo.Base_APIAccount where Base_APIAccountID='" + Base_APIAccountID + "'"); if (dt.Rows.Count > 0) { if (dt.Rows[0]["isStock"].ToString().Trim() != "" && dt.Rows[0]["isStock"].ToString() != "请选择") { api.Style = "1"; api.WarehouseID = dt.Rows[0]["isStock"].ToString(); } else { api.Style = "2"; api.WarehouseID = ""; } string userid = dt.Rows[0]["CreateUser"].ToString(); string username = dt.Rows[0]["DevAccount"].ToString(); string api_key = dt.Rows[0]["DevAccountPWD"].ToString(); string AccountName = dt.Rows[0]["AccountName"].ToString(); string token = GetAlibabaToken(username, api_key, dt.Rows[0]["Field1"].ToString());//; api.GFF_CustomerID = dt.Rows[0]["GFF_CustomerID"].ToString(); api.Weight = dt.Rows[0]["Weight"].ToString(); api.Price = dt.Rows[0]["Price"].ToString(); if (StrDate == "" || StrDate == "1900-01-01 00:00:00.000" || StrDate == "1900/1/1 0:00:00") { StrDate = ""; } if (EndDate == "" || EndDate == "1900-01-01 00:00:00.000" || EndDate == "1900/1/1 0:00:00") { EndDate = ""; } string url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findOrderListQuery/" + username + "?page=1&pageSize=100&orderStatus=WAIT_SELLER_SEND_GOODS&access_token=" + token + ""; if (StrDate != "" && EndDate != "") { Dictionary paramDic = new Dictionary(); string sign_str = "param2/1/aliexpress.open/api.findOrderListQuery/" + username + ""; paramDic.Add("page", "1"); paramDic.Add("pageSize", "100"); paramDic.Add("createDateStart", StrDate.Replace("/", "-")); paramDic.Add("createDateEnd", EndDate.Replace("/", "-")); paramDic.Add("orderStatus", "WAIT_SELLER_SEND_GOODS"); paramDic.Add("access_token", token); aop_signature = sign(sign_str, paramDic, api_key); url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findOrderListQuery/" + username + "?page=1&pageSize=100&createDateStart=" + StrDate.Replace("/", "-") + "&createDateEnd=" + EndDate.Replace("/", "-") + "&orderStatus=WAIT_SELLER_SEND_GOODS&access_token=" + token + "&_aop_signature=" + aop_signature + ""; } else { return retstr; } byte[] datas = Encoding.UTF8.GetBytes(""); byte[] results = client.UploadData(url, "POST", datas); string PostResults = Encoding.UTF8.GetString(results); //var jObject = JObject.Parse(PostResults); //string retdata = jObject["orderList"].ToString() ;// jObject["data"]["orderList"][0]["parcelNumbers"][0].ToString(); string[] str = PostResults.Split(','); for (int i = 0; i < str.Length; i++) { if (str[i].Split(':')[0] == "\"orderId\"")//\"logisticsType\ { if (!arr.Contains(str[i].Split(':')[1].ToString().Replace("}]", "").Replace("}", ""))) { arr.Add(str[i].Split(':')[1].ToString().Replace("}]", "").Replace("}", "")); } } if (str[i].Split(':')[0] == "\"logisticsType\"")//\"logisticsType\ { arrs.Add(str[i].Split(':')[1].ToString().Replace("}]", "").Replace("}", "")); } if (str[i].Split(':')[0] == "\"productId\"")//\"productId\ { arrp.Add(str[i].Split(':')[1].ToString().Replace("}]", "").Replace("}", "")); } if (str[i].Split(':')[0] == "\"productName\"")//\"productName\ { arrpn.Add(str[i].Split(':')[1].ToString().Replace("}]", "").Replace("}", "")); } } if (arr.Count > 0) { for (int a = 0; a < arr.Count; a++) { api.CsRefNo = arr[a].ToString(); api.Contact = ""; DataSet dscr = C_c.SelectS("select * from ODR_OrderMain where CsRefNo='" + api.CsRefNo + "'"); DataTable dts = dscr.Tables[0];//判断是否存在 if (dts.Rows.Count > 0) { continue; } else { string strorderproduct = ""; Dictionary paramDic = new Dictionary(); string sign_str = "param2/1/aliexpress.open/api.findOrderTradeInfo/" + username + ""; paramDic.Add("orderId", arr[a].ToString()); paramDic.Add("access_token", token); aop_signature = sign(sign_str, paramDic, api_key); url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findOrderTradeInfo/" + username + "?orderId=" + arr[a] + "&access_token=" + token + "&_aop_signature=" + aop_signature + ""; datas = Encoding.UTF8.GetBytes(""); results = client.UploadData(url, "POST", datas); PostResults = Encoding.UTF8.GetString(results); string[] strs = PostResults.Split(','); if (strs.Length > 1) { for (int p = 0; p < strs.Length; p++) { if (strs[p].Split(':')[0] == "\"skuCode\"") { api.MaterialRefNo = strs[p].Split(':')[1]; if (api.MaterialRefNo == "") { if (arrpn.Count <= a) { } else { api.MaterialRefNo = arrpn[a].ToString(); } } } if (strs[p].Split(':')[0] == "\"productName\"") { if (arrpn.Count <= a) { api.EnName = strs[p].Split(':')[1].Substring(0, strs[p].Split(':')[1].Length - 2); } else { if (arrpn[a].ToString() != "") { api.EnName = arrpn[a].ToString().Substring(0, arrpn[a].ToString().Length - 2); } else { api.EnName = strs[p].Split(':')[1].Substring(0, strs[p].Split(':')[1].Length - 2); } } } if (strs[p].Split(':')[0] == "\"productPrice\"") { if (api.Price == "" || api.Price == "0.00" || api.Price == "0.000") { api.Price = strs[p].Split(':')[1]; } } if (strs[p].Split(':')[0] == "\"productCount\"") { if (strs[p].Split(':')[1] == "") { api.MaterialQuantity = "1"; } else { api.MaterialQuantity = strs[p].Split(':')[1]; } } } if (api.MaterialRefNo == null) { if (arrpn.Count <= a) { } else { api.MaterialRefNo = arrpn[a].ToString(); } } strorderproduct += "MaterialRefNo:" + api.MaterialRefNo.Replace("\"", "") + ",MaterialQuantity:" + api.MaterialQuantity.Replace("\"", "") + ",Price:" + api.Price.Replace("\"", "") + ",Weight:" + api.Weight + ",EnName:" + api.EnName.Replace("\"", "") + ",WarehouseID:" + api.WarehouseID + ",ProducingArea:,CnName:" + api.CnName + ",;"; } else { Dictionary paramDicp = new Dictionary(); string sign_strp = "param2/1/aliexpress.open/api.findAeProductById/" + username + ""; if (a >= arrp.Count) { continue; } paramDicp.Add("productId", arrp[a].ToString()); paramDicp.Add("access_token", token); aop_signature = sign(sign_strp, paramDicp, api_key); url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findAeProductById/" + username + "?productId=" + arrp[a] + "&access_token=" + token + "&_aop_signature=" + aop_signature + ""; datas = Encoding.UTF8.GetBytes(""); results = client.UploadData(url, "POST", datas); PostResults = Encoding.UTF8.GetString(results); string[] strp = PostResults.Split(','); if (strp.Length > 1) { for (int p = 0; p < strs.Length; p++) { if (strs[p].Split(':')[0] == "\"skuCode\"") { api.MaterialRefNo = strs[p].Split(':')[1]; if (api.MaterialRefNo == "") { if (arrpn.Count <= a) { } else { api.MaterialRefNo = arrpn[a].ToString(); } } } } if (api.MaterialRefNo != null && api.MaterialRefNo.Replace(@"\", "") == "") { strorderproduct += "MaterialRefNo:" + api.MaterialRefNo.Replace("\"", "") + ",MaterialQuantity:1,Price:" + api.Price.Replace("\"", "") + ",Weight:" + api.Weight + ",EnName:" + api.EnName.Replace("\"", "") + ",WarehouseID:" + api.WarehouseID + ",ProducingArea:,CnName:" + api.CnName + ",;"; } else { continue; } } } Dictionary paramDics = new Dictionary(); string sign_strs = "param2/1/aliexpress.open/api.findOrderReceiptInfo/" + username + ""; paramDics.Add("orderId", arr[a].ToString()); paramDics.Add("access_token", token); aop_signature = sign(sign_strs, paramDics, api_key); url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findOrderReceiptInfo/" + username + "?orderId=" + arr[a] + "&access_token=" + token + "&_aop_signature=" + aop_signature + ""; datas = Encoding.UTF8.GetBytes(""); results = client.UploadData(url, "POST", datas); PostResults = Encoding.UTF8.GetString(results); if (PostResults.Length > 2) { Dictionary dic = utits.GetList(PostResults); foreach (KeyValuePair arrkv in dic) { if (arrkv.Key == "country") { api.Country = arrkv.Value.ToString(); if (api.Country.ToString() == "UK") { api.Country = "GB"; } } if (arrkv.Key == "zip") { api.Zipcode = arrkv.Value.ToString(); } if (arrkv.Key == "city") { api.City = arrkv.Value.ToString(); } if (arrkv.Key == "province") { api.State = arrkv.Value.ToString(); } if (arrkv.Key == "contactPerson") { api.ConsigneeName = arrkv.Value.ToString(); } if (arrkv.Key == "detailAddress") { api.Address1 = arrkv.Value.ToString(); } if (arrkv.Key == "address2") { api.Address2 = arrkv.Value.ToString(); } if (arrkv.Key == "phoneNumber") { if (api.Contact != "") { api.Contact += "/" + arrkv.Value.ToString(); } else { api.Contact += arrkv.Value.ToString(); } } if (arrkv.Key == "mobileNo") { if (api.Contact != "") { api.Contact += "/" + arrkv.Value.ToString(); } else { api.Contact += arrkv.Value.ToString(); } } } } string APIChannel = ""; Dictionary paramDic_s = new Dictionary(); string sign_str_s = "param2/1/aliexpress.open/api.findOrderById/" + username + ""; paramDic_s.Add("orderId", arr[a].ToString()); paramDic_s.Add("access_token", token); aop_signature = sign(sign_str_s, paramDic_s, api_key); url = "http://gw.api.alibaba.com:80/openapi/param2/1/aliexpress.open/api.findOrderById/" + username + "?orderId=" + arr[a] + "&access_token=" + token + "&_aop_signature=" + aop_signature + ""; datas = Encoding.UTF8.GetBytes(""); results = client.UploadData(url, "POST", datas); PostResults = Encoding.UTF8.GetString(results); string[] express = PostResults.Split(','); if (express.Length > 1) { for (int e = 0; e < express.Length; e++) { if (express[e].Split(':')[0] == "\"logisticInfoList\"") { if (express[e].Split(':')[1] == "[]") { if (arrs.Count <= a) { } else { APIChannel = arrs[a].ToString().Replace("\"", ""); break; } } } if (express[e].Split(':')[0] == "\"logisticsTypeCode\"") { APIChannel = express[e].Split(':')[1].ToString(); } } } api.Base_ChannelInfoID = Get_APIChannel(userid, APIChannel, Base_APIAccountID); string strorderinfo = "Style:" + api.Style + ";GFF_CustomerID:" + userid + ";GFF_ReceiveSendAddressID:;ConsigneeName:" + api.ConsigneeName + ";Country:" + api.Country + ";Base_ChannelInfoID:" + api.Base_ChannelInfoID + ";State:" + api.State + ";City:" + api.City + ";OrderStatus:1;Address1:" + api.Address1 + ";Address2:" + api.Address2 + ";CsRefNo:" + api.CsRefNo + ";Zipcode:" + api.Zipcode + ";Contact:" + api.Contact + ";CusRemark:Aliexpress;TrackingNo:;AliexpressID:" + AccountName + "_" + arr[a] + ";"; APIWebService pfcapi = new APIWebService(); ret = pfcapi.InsertUpdateOrder(strorderinfo, strorderproduct, "", C_c.SelectS(" select apisecrete from dbo.Base_APIData where cuscode='" + userid + "'").Tables[0].Rows[0]["apisecrete"] + ""); if (ret != "") { string[] retmessage = ret.Split('-'); if (retmessage.Length > 1) { cm.Exec_BatchSql("insert into SYS_Code (code_name,code_value,code_cmmt) values('Aliexpress','订单ID:" + arr[a] + "获取订单成功','" + DateTime.Now.ToString() + "')"); //inid += s[i].id + ";"; //count++; okcount++; } else { cm.Exec_BatchSql("insert into SYS_Code (code_name,code_value,code_cmmt) values('Aliexpress','订单ID:" + arr[a] + "获取订单失败 错误提示:" + ret + "','" + DateTime.Now.ToString() + "')"); nocount++; } } } } } } else { } if (nocount > 0 && okcount > 0) { retstr = "成功获取订单:" + okcount + " 失败订单:" + nocount + ""; } else { if (nocount > 0) { retstr = "失败订单:" + nocount; } else { retstr = "成功获取订单:" + okcount; } } } catch (Exception ex) { if (ex.Message.ToString() == "远程服务器返回错误: (401) 未经授权。") { retstr = "授权已过期,请重新获取Access_Token并保存在系统中。"; } else { retstr = ex.Message.ToString(); } } return retstr; }