博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
订餐系统之获取淘宝外卖订单
阅读量:5160 次
发布时间:2019-06-13

本文共 15908 字,大约阅读时间需要 53 分钟。

当时的情况是这样的:我们一个客户用了我们的订餐系统,也在上开了店,但是订餐系统中的订单都要通过调度系统,根据配送员的位置,把订单发送给合适的配送员,所以要把淘宝外卖的订单加到订餐系统中(据说淘宝外卖也是因为味捷外卖才开发这个接口的)。

  淘宝外卖提供的接口我知道的有三个,1.获取未确认的订单;2.确认订单;3.拒绝订单。所以订餐系统要做的就是从淘宝外卖获取未确认的订单、确认订单并加入订餐系统。

  开放平台地址:

  

  关于授权:

  

  也上一张我们调度系统的图吧,这样大家可能更直观些,主要就是根据商家的位置,用户的位置,及配送员手上的订单量等情况来分配订单,不过目前都是调度人员人式调度的。

   

  

  下面简单的说明下接口调用方法,及示例代码。【注:之前他的接口文档中有一个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
parameters = new SortedList
(); //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
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
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
()[0]; //1.加到系统, ETogoOrderInfo ordermodel = new ETogoOrderInfo(); IList
foodlist = new List
(); 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
parameters = new SortedList
(); //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
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
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; }

转载于:https://www.cnblogs.com/soundcode/p/7499835.html

你可能感兴趣的文章
苹果开发中常用英语单词
查看>>
[USACO 1.4.3]等差数列
查看>>
Shader Overview
查看>>
Reveal 配置与使用
查看>>
Java中反射的学习与理解(一)
查看>>
C语言初学 俩数相除问题
查看>>
B/S和C/S架构的区别
查看>>
[Java] Java record
查看>>
jQuery - 控制元素显示、隐藏、切换、滑动的方法
查看>>
postgresql学习文档
查看>>
Struts2返回JSON数据的具体应用范例
查看>>
js深度克隆对象、数组
查看>>
socket阻塞与非阻塞,同步与异步
查看>>
团队工作第二天
查看>>
System类
查看>>
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>