菜鳥雲打印接入Demo


菜鳥雲打印接入Demo

0. 接入流程圖

1. 連接打印客戶端

首先要打開打印客戶端,然后使用下面的方法,連接客戶端(WebSocket協議):

地址 :  

function doConnect()
{
    socket = new WebSocket('ws://127.0.0.1:13528');

    // 打開Socket
    socket.onopen = function(event)
    {
        // 監聽消息
        socket.onmessage = function(event)
        {
            console.log('Client received a message',event);
        };

        // 監聽Socket的關閉
        socket.onclose = function(event)
        {
            console.log('Client notified socket has closed',event);
        };

    };
}

2. 打印機配置

2.1 獲取打印機列表

function doGetPrinters() {
    var request  = {
        requestID : '12345678901234567890',
        version : '1.0',
        cmd : 'getPrinters';
    };

    socket.send(JSON.stringify(request));
}

2.2 獲取打印機配置(彈出打印機配置界面)

function doPrinterConfig() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "printerConfig"
    };

    socket.send(JSON.stringify(request));
}

2.3 設置打印機

function doSetPrinterConfig() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "setPrinterConfig",
        printer : {
            name : "打印機名稱",
            needTopLogo : true,
            needBottomLogo: false
        }
    };

    socket.send(JSON.stringify(request));
}

3 打印

  • 商家可以選擇直接打印電子面單模板, 或是打印帶自定義區域的用戶模板

3.1 標准電子面單模板

商家無自定義區,只有標准電子面單模板

3.1.1 TOP調用請求相關數據

3.1.1.1 獲取所有的菜鳥標准電子面單模板: cainiao.cloudprint.stdtemplates.get

返回的數據是StandardTemplateResult數組:

    cp_code : cp編碼
    standard_templates : StandardTemplateDo[]
        standard_template_id : 標准模板id
        standard_template_name : 標准模板名稱
        standard_template_url : 標准模板url

一個CP可以有多個電子面單標准模板, standard_template_url是電子面單標准模板的內容地址.

3.1.1.2 電子面單雲打印接口: cainiao.waybill.ii.get

返回的數據是WaybillCloudPrintResponse:

    object_id : 請求id
    signature : 簽名
    template_url : 模板URL
    waybill_code : 面單號
    data : 模板內容

3.1.2 電子面單模板打印數據

電子面單雲打印接口返回的數據,構建模板打印數據:
{
    "templateURL":"http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=801",
    "signature": "ALIBABACAINIAOWANGLUO",
    "waybillNo":"9890000076011"
    "data":{
        "key1":"value1",
        "key2":"value2"
    }
}

3.2 用戶模板

用戶模板包含標准電子模板和用戶自定義區

3.2.1 TOP調用請求相關數據

3.2.1.1 獲取用戶使用的菜鳥電子面單模板信息 : cainiao.cloudprint.mystdtemplates.get

返回的數據是UserTemplateResult數組:

    cp_code : cp編碼
    user_std_templates(UserTemplateDo[]) :用戶使用的模板數據
        keys(KeyResult[]) :     keys
            key_name : key的名稱
        user_std_template_url : 用戶使用模板的url
        user_std_template_id : 用戶使用模板的id
        user_std_template_name : 用戶使用模板名稱
數據返回的是用戶使用到的菜鳥標准電子面單模板
3.2.1.1 獲取商家的自定義區模板信息 : cainiao.cloudprint.customares.get

輸入參數是 : seller_id, template_id(用戶使用的標准電子模板id,3.2.1.1返回的user_std_template_id)

返回的數據是CustomAreaResult數組:

    custom_area_id : 自定義區id
    custom_area_url : 自定義區url
    keys(KeyResult[]) : keys
        key_name : key名稱
3.2.1.2 電子面單雲打印接口: cainiao.waybill.ii.get(同3.1.1.2)

3.2.2 用戶模板打印數據格式

3.2.2.1 標准電子面單模板打印數據格式(同3.1.2)
{
    "templateURL":"http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=801",
    "signature": "ALIBABACAINIAOWANGLUO",
    "waybillNo":"電子面單號"
    "data":{
        "key1":"value1",
        "key2":"value2"
    }
}
3.2.2.2 自定義區打印數據格式
{
    "templateURL":"http://cloudprint.cainiao.com/cloudprint/customArea/queryCustomAreaList4Top.json?custom_area_id=2201&user_id=2066393830",
    "data":{
        "key1":"value1",
        "key2":"value2"
    }
}

其中的templateURL來源於3.2.1.1, data需要根據key名稱自行填充.

3.3 打印

面單號 : 

電子面單URL : 

自定義區URL : 

function doPrint()
{
    request  = {
        cmd : "print",
        requetID : "12345678901234567890",
        version : "1.0",
        task : {
            taskID : "1",
            preview : false,
            printer : "km200",
            documents : [
                {
                    documentID : "9890000076011",
                    contents : [
                        //電子面單部分
                        {
                            templateURL : "http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=801",
                            signature : "ALIBABACAINIAOWANGLUO",
                            "data": {
                              "recipient": {
                                "address": {
                                  "city": "北京市",
                                  "detail": "花家地社區衛生服務站三層樓我也不知道是哪兒了",
                                  "district": "朝陽區",
                                  "province": "北京",
                                  "town": "望京街道"
                                },
                                "mobile": "1326443654",
                                "name": "張三",
                                "phone": "057123222"
                              },
                              "routingInfo": {
                                "consolidation": {
                                  "name": "杭州",
                                  "code": "hangzhou"
                                },
                                "origin": {
                                  "code": "POSTB"
                                },
                                "sortation": {
                                  "name": "杭州"
                                },
                                "routeCode": "380D-56-04"
                              },
                              "sender": {
                                "address": {
                                  "city": "北京市",
                                  "detail": "花家地社區衛生服務站二層樓我也不知道是哪兒了",
                                  "district": "朝陽區",
                                  "province": "北京",
                                  "town": "望京街道"
                                },
                                "mobile": "1326443654",
                                "name": "張三",
                                "phone": "057123222"
                              },
                              "shippingOption": {
                                "code": "COD",
                                "services": {
                                  "SVC-COD": {
                                    "value": "200"
                                  }
                                },
                                "title": "代收貨款"
                              },
                              "waybillCode": "9890000160004"
                            }
                        },
                        //自定義區部分
                        {
                            templateURL : "http://cloudprint.cainiao.com/cloudprint/customArea/queryCustomAreaList4Top.json?custom_area_id=642230",
                            data : {
                                "item_name": "我是你要的商品芭比娃娃。。。",
                            }
                        }
                    ]
                }
            ]
        }
    };

    socket.send(JSON.stringify(request));
}

 

 

4 查詢打印任務

4.1 根據taskID查詢打印任務

function doGetTaskStatus() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "getTaskStatus",
        taskID : [
            "1","2"
        ]
    };
    socket.send(JSON.stringify(request));
}

4.2 根據面單號查詢打印任務

waybillNO :  

function doGetDocumentStatus() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "getDocumentStatus",
        documentIDs : [
            "9890000076011"
        ]
    };
    socket.send(JSON.stringify(request));
}

5 參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM