欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

菜鸟电子面单获取教程

程序员文章站 2022-04-02 23:41:12
...

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace FormTest
{
    public partial class Form1 : Form
    {
//以下四个值是在菜鸟开通电子面单服务后得到
        string serverUrl = "xxxx";
        string appKey = "xxxx";
        string appSecret = "xxxx";
        string sessionKey = "xxxx";

        List<string> tradeOrderList;

        public Form1()
        {
            InitializeComponent();
        }

//测试
        private void button1_Click(object sender, EventArgs e)
        {
            //第一步 获取到用户ID,
            //对应接口: taobao.user.seller.get (查询卖家用户信息)
            long UserID = UserSellerGet(); 
            //第二步 获取到发件人信息(取快递单必须用这个地址设置),
            //对应接口: cainiao.waybill.ii.search (查询面单服务订购及面单使用情况)
            CainiaoWaybillIiSearchResponse SenderInfo = WaybillIiSearch();
            //第三部 获取快递单号
            //对应接口: cainiao.waybill.ii.get (电子面单云打印接口)
            GetWaybill(SenderInfo, UserID);
        }

        //获取UserID
        public long UserSellerGet()
        {
            ITopClient client = new DefaultTopClient(serverUrl, appKey, appSecret);
            UserSellerGetRequest req = new UserSellerGetRequest();
            req.Fields = "user_id,nick,sex";
            UserSellerGetResponse rsp = client.Execute(req, sessionKey);
            long UserID = rsp.User.UserId;
            Console.WriteLine(rsp.Body);
            return UserID;
        }

        //查询商家电子面单开通信息, 主要是为了去地址信息
        public CainiaoWaybillIiSearchResponse WaybillIiSearch()
        {
            ITopClient client = new DefaultTopClient(serverUrl, appKey, appSecret);
            CainiaoWaybillIiSearchRequest req = new CainiaoWaybillIiSearchRequest();
            //req.CpCode = "YUNDA";
            CainiaoWaybillIiSearchResponse rsp = client.Execute(req, sessionKey);
            Console.WriteLine(rsp.Body);
            return rsp;
        }

        //获取面单号
        public void GetWaybill(CainiaoWaybillIiSearchResponse SenderInfo, long UserID)
        {
            ITopClient client = new DefaultTopClient(serverUrl, appKey, appSecret);
            CainiaoWaybillIiGetRequest req = new CainiaoWaybillIiGetRequest();
            CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequestDomain obj1 = new CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequestDomain();
            obj1.CpCode = "YUNDA";
            //obj1.ProductCode = "";
            CainiaoWaybillIiGetRequest.UserInfoDtoDomain obj2 = new CainiaoWaybillIiGetRequest.UserInfoDtoDomain();
            CainiaoWaybillIiGetRequest.AddressDtoDomain obj3 = new CainiaoWaybillIiGetRequest.AddressDtoDomain();
            obj3.City = "xx从SenderInfo中获取xx";
            obj3.Detail = "xx从SenderInfo中获取xx";
            obj3.District = "xx从SenderInfo中获取xx";
            obj3.Province = "xx从SenderInfo中获取xx;
            obj3.Town = "";
            obj2.Address = obj3;
            obj2.Mobile = "xxxxxx";
            obj2.Name = "xxxx";
            obj2.Phone = "";
            obj1.Sender = obj2;
            List<CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain> list5 = new List<CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain>();
            CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain obj6 = new CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain();
            list5.Add(obj6);
            obj6.LogisticsServices = "";
            obj6.ObjectId = UserID + "";   // 商家的UserID
            CainiaoWaybillIiGetRequest.OrderInfoDtoDomain obj7 = new CainiaoWaybillIiGetRequest.OrderInfoDtoDomain();
            obj7.OrderChannelsType = "TB";

            tradeOrderList = new List<string>();
            tradeOrderList.Add("PU1710121-001");
            obj7.TradeOrderList = tradeOrderList;
            obj6.OrderInfo = obj7;
            CainiaoWaybillIiGetRequest.PackageInfoDtoDomain obj8 = new CainiaoWaybillIiGetRequest.PackageInfoDtoDomain();
            obj8.Id = "1";
            List<CainiaoWaybillIiGetRequest.ItemDomain> list10 = new List<CainiaoWaybillIiGetRequest.ItemDomain>();
            CainiaoWaybillIiGetRequest.ItemDomain obj11 = new CainiaoWaybillIiGetRequest.ItemDomain();
            list10.Add(obj11);
            obj11.Count = 1L;
            obj11.Name = "衣服";
            obj8.Items = list10;
            obj8.Volume = 1L;
            obj8.Weight = 1L;
            obj6.PackageInfo = obj8;
            CainiaoWaybillIiGetRequest.UserInfoDtoDomain obj12 = new CainiaoWaybillIiGetRequest.UserInfoDtoDomain();
            CainiaoWaybillIiGetRequest.AddressDtoDomain obj13 = new CainiaoWaybillIiGetRequest.AddressDtoDomain();
            obj13.City = "xxxxxx市";
            obj13.Detail = "测试详细地址";
            obj13.District = "xx区";
            obj13.Province = "xxxxxxxxx";
            obj13.Town = "";
            obj12.Address = obj13;
            obj12.Mobile = "xxxxxxxxx";
            obj12.Name = "xxxxxxxxxxxx";
            obj12.Phone = "xxxxxxxxx";
            obj6.Recipient = obj12;
            obj6.TemplateUrl = "http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=1001";
            obj6.UserId = 12L;
            obj1.TradeOrderInfoDtos = list5;
            obj1.StoreCode = "";
            obj1.ResourceCode = "DISTRIBUTOR_978324";
            obj1.DmsSorting = false;
            req.ParamWaybillCloudPrintApplyNewRequest_ = obj1;
            CainiaoWaybillIiGetResponse rsp = client.Execute(req, sessionKey);
            Console.WriteLine(rsp.Body);

            string WaybillCode = rsp.Modules[0].WaybillCode;
        }

    }
}