訂餐系統之獲取淘寶外賣訂單
當時的情況是這樣的:我們一個客戶用了我們的訂餐系統,也在淘寶外賣上開了店,但是訂餐系統中的訂單都要通過調度系統,根據配送員的位置,把訂單發送給合適的配送員,所以要把淘寶外賣的訂單加到訂餐系統中(據說淘寶外賣也是因為味捷外賣才開發這個接口的)。
淘寶外賣提供的接口我知道的有三個,1.獲取未確認的訂單;2.確認訂單;3.拒絕訂單。所以訂餐系統要做的就是從淘寶外賣獲取未確認的訂單、確認訂單並加入訂餐系統。
開放平臺地址:
http://open.taobao.com/doc/api_cat_detail.htm?scope_id=317&category_id=102
關於授權:
http://open.taobao.com/doc/category_list.htm?id=199
也上一張我們調度系統的圖吧,這樣大家可能更直觀些,主要就是根據商家的位置,用戶的位置,及配送員手上的訂單量等情況來分配訂單,不過目前都是調度人員人式調度的。
下面簡單的說明下接口調用方法,及示例代碼。【註:之前他的接口文檔中有一個bug,咨詢了他們技術支持才處理好,具體位置不太記得了,也不知道他們修改了沒。】
獲取訪問權限這部分代碼比較簡單,就是到指定地址獲取 top_session,代碼如下:
string appkey = WebUtility.GetConfigKey("taobao_appkey"); string url = "http://container.open.taobao.com/container?appkey=" + appkey + "&encode=utf-8"; Response.Redirect(url);
string appkey = WebUtility.GetConfigKey("taobao_appkey"); string url = "http://container.open.taobao.com/container?appkey=" + appkey + "&encode=utf-8"; Response.Redirect(url);
獲取未確認的訂單
這一步要用到上一步中獲取的 top_session,基本也就是構造鏈接,然後用官方提供的 TopSdl.dll,轉成IList<TakeoutThirdOrder>,這樣就直接Repeater 綁定了,都是程序員,代碼一看就明白了。程序中有一個 shopid,這是商戶號,開店時會提供。
string top_session = Request["top_session"];
if (top_session == null || top_session == "")
{
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:未授權,不能獲取訂單,請先授權!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();");
return;
}
string shopid = WebUtility.FixgetCookie("taobao_shopid");
if (shopid == null || shopid == "")
{
Response.Redirect("getsectionkey.aspx");
}
string appkey = WebUtility.GetConfigKey("taobao_appkey");
string appsecret = WebUtility.GetConfigKey("taobao_appsecret");
//參數集合
SortedList<string, string> parameters = new SortedList<string, string>();
//2.1 應用級輸入參數
parameters.Add("is_all", "true");
parameters.Add("store_id", shopid);
parameters.Add("max_size", "20");
//2.2 系統級參數
parameters.Add("method", "taobao.trade.waimai.get");
parameters.Add("session", top_session);
parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
parameters.Add("format", "json");
parameters.Add("app_key", appkey);
parameters.Add("v", "2.0");
parameters.Add("partner_id", "top-apitools");
//2.3 生成sign,格式: app_secret參數1值1參數2值2,生成md5後轉為大寫
StringBuilder sb = new StringBuilder();
sb.Append(appsecret);
foreach (KeyValuePair<string, string> item in parameters)
{
sb.Append(item.Key + item.Value);
}
//sb.Append(appsecret);
parameters.Add("sign", WebUtility.GetMd5(sb.ToString()).ToUpper());
//3.生成url
string url = "http://gw.api.taobao.com/router/rest?";//線上環境: http://gw.api.taobao.com/router/rest 測試環境: http://gw.sandbox.taobao.com/router/rest
StringBuilder query = new StringBuilder();
foreach (KeyValuePair<string, string> item in parameters)
{
query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&");
}
url += query.ToString().TrimEnd(new char[] { ‘&‘ });
ITopClient client = new DefaultTopClient(url, appkey, appsecret, "json");
TradeWaimaiGetRequest req = new TradeWaimaiGetRequest();
req.StoreId = Convert.ToInt32(shopid);
req.IsAll = true;
req.MaxSize = 20L;
TradeWaimaiGetResponse response = client.Execute(req);
if (response.Result != null && response.Result.ResultList != null)
{
ttOrderlist = response.Result.ResultList;
}
BindData();
string top_session = Request["top_session"]; if (top_session == null || top_session == "") { AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:未授權,不能獲取訂單,請先授權!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();"); return; } string shopid = WebUtility.FixgetCookie("taobao_shopid"); if (shopid == null || shopid == "") { Response.Redirect("getsectionkey.aspx"); } string appkey = WebUtility.GetConfigKey("taobao_appkey"); string appsecret = WebUtility.GetConfigKey("taobao_appsecret"); //參數集合 SortedList<string, string> parameters = new SortedList<string, string>(); //2.1 應用級輸入參數 parameters.Add("is_all", "true"); parameters.Add("store_id", shopid); parameters.Add("max_size", "20"); //2.2 系統級參數 parameters.Add("method", "taobao.trade.waimai.get"); parameters.Add("session", top_session); parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); parameters.Add("format", "json"); parameters.Add("app_key", appkey); parameters.Add("v", "2.0"); parameters.Add("partner_id", "top-apitools"); //2.3 生成sign,格式: app_secret參數1值1參數2值2,生成md5後轉為大寫 StringBuilder sb = new StringBuilder(); sb.Append(appsecret); foreach (KeyValuePair<string, string> item in parameters) { sb.Append(item.Key + item.Value); } //sb.Append(appsecret); parameters.Add("sign", WebUtility.GetMd5(sb.ToString()).ToUpper()); //3.生成url string url = "http://gw.api.taobao.com/router/rest?";//線上環境: http://gw.api.taobao.com/router/rest 測試環境: http://gw.sandbox.taobao.com/router/rest StringBuilder query = new StringBuilder(); foreach (KeyValuePair<string, string> item in parameters) { query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&"); } url += query.ToString().TrimEnd(new char[] { ‘&‘ }); ITopClient client = new DefaultTopClient(url, appkey, appsecret, "json"); TradeWaimaiGetRequest req = new TradeWaimaiGetRequest(); req.StoreId = Convert.ToInt32(shopid); req.IsAll = true; req.MaxSize = 20L; TradeWaimaiGetResponse response = client.Execute(req); if (response.Result != null && response.Result.ResultList != null) { ttOrderlist = response.Result.ResultList; } BindData();確認訂單
這個也其他是參考官方的文檔,沒有特別的地方。上代碼:
protected void rtpOrderlist_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string type = e.CommandName;
string oid = e.CommandArgument.ToString();
switch (type)
{
case "pass":
//確認後,加到系統(這部分訂單要再加一個步定位(用戶,商家)修改這個這個字段:latlng)
//先判斷此訂單是否已經加到系統
string sql = " orderid=‘" + oid + "‘ ";
if (bll.GetCount(sql) > 0)
{
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:此訂單已經加入系統,請不要重復操作!‘,‘250‘,‘150‘,‘true‘,‘2000‘,‘true‘,‘text‘);init();");
return;
}
TakeoutThirdOrder model = ttOrderlist.Where(p => p.Id.ToString() == oid).ToList<TakeoutThirdOrder>()[0];
//1.加到系統,
ETogoOrderInfo ordermodel = new ETogoOrderInfo();
IList<FoodInOrderInfo> foodlist = new List<FoodInOrderInfo>();
decimal allfoodmoney = 0;
foreach (OrderGoods item in model.GoodsList)
{
FoodInOrderInfo fmodel = new FoodInOrderInfo();
fmodel.FoodID = 0;
fmodel.FoodName = item.Name;
fmodel.FoodPrice = Convert.ToDecimal(item.RealPrice);
fmodel.Currentprice = fmodel.FoodPrice;
fmodel.package = 0;
fmodel.Num = Convert.ToInt32(item.Count);
allfoodmoney += fmodel.FoodPrice * fmodel.Num;
foodlist.Add(fmodel);
}
ordermodel.OrderID = model.Id.ToString();
ordermodel.UserName = model.UserAddress.Name;
ordermodel.Tel = model.UserAddress.Mobile;
ordermodel.Address = model.UserAddress.Address;
ordermodel.tabaoshopid = Convert.ToInt64(model.StoreId);
ordermodel.orderTime = Convert.ToDateTime(model.CreateTime);
ordermodel.TotalPrice = allfoodmoney;
ordermodel.Remark = model.Note;
ordermodel.sendmoney = Convert.ToInt32(Convert.ToDecimal(model.DeliveryPay));
ordermodel.Inve2 = "";
ordermodel.OrderTotal = allfoodmoney;
ordermodel.PayMode = 1;
ordermodel.paytime = Convert.ToDateTime(model.CreateTime);
ordermodel.paymoney = Convert.ToDecimal(model.TotalPay);
ordermodel.SentTime = model.StartDeliverytime;
ordermodel.shopaddress = model.Address;
ordermodel.TogoName = model.StoreName;
int rs = bll.AddTBOrder(ordermodel, foodlist);
//2.修改狀態
if (rs > 0)
{
string shopid = WebUtility.FixgetCookie("taobao_shopid");
if (shopid == null || shopid == "")
{
Response.Redirect("getsectionkey.aspx");
}
string appkey = WebUtility.GetConfigKey("taobao_appkey");
string appsecret = WebUtility.GetConfigKey("taobao_appsecret");
string top_session = Request["top_session"];
//參數集合
SortedList<string, string> parameters = new SortedList<string, string>();
//2.1 應用級輸入參數
parameters.Add("order_id", oid);
//2.2 系統級參數
parameters.Add("method", "taobao.trade.waimai.confirm");
parameters.Add("session", top_session);
parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
parameters.Add("format", "json");
parameters.Add("app_key", appkey);
parameters.Add("v", "2.0");
parameters.Add("partner_id", "top-apitools");
//2.3 生成sign,格式: app_secret參數1值1參數2值2,生成md5後轉為大寫
StringBuilder sb = new StringBuilder();
sb.Append(appsecret);
foreach (KeyValuePair<string, string> item in parameters)
{
sb.Append(item.Key + item.Value);
}
//sb.Append(appsecret);
parameters.Add("sign", WebUtility.GetMd5(sb.ToString()).ToUpper());
//3.生成url
string url = "http://gw.api.taobao.com/router/rest?";
StringBuilder query = new StringBuilder();
foreach (KeyValuePair<string, string> item in parameters)
{
query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&");
}
url += query.ToString().TrimEnd(new char[] { ‘&‘ });
ITopClient client = new DefaultTopClient(url, appkey, appsecret, "json");
TradeWaimaiConfirmRequest req = new TradeWaimaiConfirmRequest();
req.OrderId = Convert.ToInt64(oid);
TradeWaimaiConfirmResponse response = client.Execute(req, top_session);
ttOrderlist.Remove(model);
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:操作成功!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();");
BindData();
}
else
{
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:操作失敗!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();");
}
break;
case "refuse":
if (bll.DelETogoOrder(Convert.ToInt32(e.CommandArgument)) > 0)//只是隱藏了,並沒刪除
{
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:刪除成功!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();");
BindData();
}
else
{
AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:刪除失敗!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();");
}
break;
}
轉:
http://www.cnblogs.com/jijunjian/p/3405111.html
protected void rtpOrderlist_ItemCommand(object source, RepeaterCommandEventArgs e) { string type = e.CommandName; string oid = e.CommandArgument.ToString(); switch (type) { case "pass": //確認後,加到系統(這部分訂單要再加一個步定位(用戶,商家)修改這個這個字段:latlng) //先判斷此訂單是否已經加到系統 string sql = " orderid=‘" + oid + "‘ "; if (bll.GetCount(sql) > 0) { AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:此訂單已經加入系統,請不要重復操作!‘,‘250‘,‘150‘,‘true‘,‘2000‘,‘true‘,‘text‘);init();"); return; } TakeoutThirdOrder model = ttOrderlist.Where(p => p.Id.ToString() == oid).ToList<TakeoutThirdOrder>()[0]; //1.加到系統, ETogoOrderInfo ordermodel = new ETogoOrderInfo(); IList<FoodInOrderInfo> foodlist = new List<FoodInOrderInfo>(); decimal allfoodmoney = 0; foreach (OrderGoods item in model.GoodsList) { FoodInOrderInfo fmodel = new FoodInOrderInfo(); fmodel.FoodID = 0; fmodel.FoodName = item.Name; fmodel.FoodPrice = Convert.ToDecimal(item.RealPrice); fmodel.Currentprice = fmodel.FoodPrice; fmodel.package = 0; fmodel.Num = Convert.ToInt32(item.Count); allfoodmoney += fmodel.FoodPrice * fmodel.Num; foodlist.Add(fmodel); } ordermodel.OrderID = model.Id.ToString(); ordermodel.UserName = model.UserAddress.Name; ordermodel.Tel = model.UserAddress.Mobile; ordermodel.Address = model.UserAddress.Address; ordermodel.tabaoshopid = Convert.ToInt64(model.StoreId); ordermodel.orderTime = Convert.ToDateTime(model.CreateTime); ordermodel.TotalPrice = allfoodmoney; ordermodel.Remark = model.Note; ordermodel.sendmoney = Convert.ToInt32(Convert.ToDecimal(model.DeliveryPay)); ordermodel.Inve2 = ""; ordermodel.OrderTotal = allfoodmoney; ordermodel.PayMode = 1; ordermodel.paytime = Convert.ToDateTime(model.CreateTime); ordermodel.paymoney = Convert.ToDecimal(model.TotalPay); ordermodel.SentTime = model.StartDeliverytime; ordermodel.shopaddress = model.Address; ordermodel.TogoName = model.StoreName; int rs = bll.AddTBOrder(ordermodel, foodlist); //2.修改狀態 if (rs > 0) { string shopid = WebUtility.FixgetCookie("taobao_shopid"); if (shopid == null || shopid == "") { Response.Redirect("getsectionkey.aspx"); } string appkey = WebUtility.GetConfigKey("taobao_appkey"); string appsecret = WebUtility.GetConfigKey("taobao_appsecret"); string top_session = Request["top_session"]; //參數集合 SortedList<string, string> parameters = new SortedList<string, string>(); //2.1 應用級輸入參數 parameters.Add("order_id", oid); //2.2 系統級參數 parameters.Add("method", "taobao.trade.waimai.confirm"); parameters.Add("session", top_session); parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); parameters.Add("format", "json"); parameters.Add("app_key", appkey); parameters.Add("v", "2.0"); parameters.Add("partner_id", "top-apitools"); //2.3 生成sign,格式: app_secret參數1值1參數2值2,生成md5後轉為大寫 StringBuilder sb = new StringBuilder(); sb.Append(appsecret); foreach (KeyValuePair<string, string> item in parameters) { sb.Append(item.Key + item.Value); } //sb.Append(appsecret); parameters.Add("sign", WebUtility.GetMd5(sb.ToString()).ToUpper()); //3.生成url string url = "http://gw.api.taobao.com/router/rest?"; StringBuilder query = new StringBuilder(); foreach (KeyValuePair<string, string> item in parameters) { query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&"); } url += query.ToString().TrimEnd(new char[] { ‘&‘ }); ITopClient client = new DefaultTopClient(url, appkey, appsecret, "json"); TradeWaimaiConfirmRequest req = new TradeWaimaiConfirmRequest(); req.OrderId = Convert.ToInt64(oid); TradeWaimaiConfirmResponse response = client.Execute(req, top_session); ttOrderlist.Remove(model); AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:操作成功!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();"); BindData(); } else { AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:操作失敗!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();"); } break; case "refuse": if (bll.DelETogoOrder(Convert.ToInt32(e.CommandArgument)) > 0)//只是隱藏了,並沒刪除 { AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:刪除成功!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();"); BindData(); } else { AlertScript.RegScript(this.Page, this.UpdatePanel1, "tipsWindown(‘提示信息‘,‘text:刪除失敗!‘,‘250‘,‘150‘,‘true‘,‘1000‘,‘true‘,‘text‘);init();"); } break; }
訂餐系統之獲取淘寶外賣訂單