订餐系统之获取淘宝外卖订单

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

  淘宝外卖提供的接口我知道的有三个,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;
        }
复制代码
posted @ 2017-09-09 23:52  左正  阅读(906)  评论(0编辑  收藏  举报