关于菜鸟的圆通电子面单打印

Posted 年华-夜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于菜鸟的圆通电子面单打印相关的知识,希望对你有一定的参考价值。

1.

 
2.

 https://dayin.cainiao.com/miandan/subscription/branchRechargeInfo.htm?spm=a2d3a.8498240.0.0.558119b3hxhtix 订购关系查询

3.

 
4.
 
 
 
前面的地址是需要仔细看的,我做的电子面单打印是依靠菜鸟的打印组件预览出来再手动打印,并不是全部用菜鸟的打印组件打印,下面是正式的介绍情况了.
 
第一步,申请需要申请的账号,这些你就看文档,我也帮不了你什么,我也是别人先申请我在用的.
第二部.单独创建一个控制器,这是控制器的内容,我并不是靠ajax传递数据到控制器接收的,而是用form表单提交,这里说清楚,接收的数据按照你要床底的情况自己组装
 
 1    //圆通信息
 2                     $a=time()+1;//随机订单号
 3                     $b=time();//随机包裹号      这两个参数不许是变量,不然每次返回的快递单号都是同一个,也就是合单,你也可以在群里问客服
 4 //下面是需要传递到接口的数据
 5                     $eorderi =\'{"cpCode":"YTO","dmsSorting":"false","needEncrypt":"false","resourceCode":"无",
 6         "sender":{
 7             "address":{
 8                 "city":"宿迁市",
 9                 "detail":"淮海东路*********",
10                 "district":"泗阳县",
11                 "province":"江苏省",
12                 "town":""
13             },
14             "mobile":"18***742",
15             "name":"曲*",
16             "phone":"1865***42",
17         },//这些是发货地址,和你在订购关系里申请的内容要一样
18         "storeCode":"无",
19         "tradeOrderInfoDtos":[
20             {
21                 "logisticsServices":"",
22                 "objectId":"1",
23                 "orderInfo":{
24                     "orderChannelsType":"OTHERS",
25                     "tradeOrderList":[
26                         "\'.$a.\'"//订单号
27                     ]
28                 },
29                 "packageInfo":{
30                     "id":"\'.$b.\'",//包裹号
31                     "items":[
32                         {
33                             "count":"\'.$total_number.\'",
34                             "name":"衣服"
35                         }
36                     ],
37                     "volume":"1",
38                     "weight":"1"
39                 },
40                 "recipient":{
41                     "address":{
42                         "city":"\'.$ship_order_data[\'city\'].\'",
43                         "detail":"\'.$ship_order_data[\'address\'].\'",
44                         "district":"\'.$ship_order_data[\'exparea\'].\'",
45                         "province":"\'.$ship_order_data[\'province\'].\'",
46                         "town":""
47                     },
48                     "mobile":"\'.$ship_order_data[\'phone\'].\'",
49                     "name":"\'.$ship_order_data[\'username\'].\'",
50                     "phone":"\'.$ship_order_data[\'phone\'].\'"
51                 },//这些是发货地址
52                 "templateUrl":" http://cloudprint.cainiao.com/template/standard/101/572",//我用的是圆通,所以这是模板的地址,自己在API里面找
53                 "userId":"12"//随便填写
54             }
55         ]
56     }\';
57
58    $jsonResult = $this->submitEOrderi($eorderi);
59
60    $str=json_decode($jsonResult);


      
      $this->assign(\'list\',$str->waybillCloudPrintResponseList[0]->printData);//只需要打印里面的printData里的数据


      return $this->fetch(\'ce\');//新写一个空白的前端.数据在那边接收

第三部.前面的都是准备工作,下面的才是正事的.

(1).

  
 1 //圆通电子面单接口
 2 
 3     /**
 4      * Json方式 调用电子面单接口
 5      */
 6     public function submitEOrderi($requestData)
 7     {
 8         $url=\'http://link.cainiao.com/gateway/link.do\';//正式地址  必须
 9 
10 
11         $appSecret =\'z026yj********83S3B6cv86\';必须
12 //
13         $datas = array(
14             \'msg_type\' =>\'TMS_WAYBILL_GET\',
15             \'logistic_provider_id\' =>\'UFI1c3dkR2tNRWpX*********************1zWWZxS2V5Y1FrWi90K21jelhOSTMwQ3pKaA==\',必须
16
17             \'data_digest\'=>$this->encrypti($requestData,$appSecret),//获取签名
18             \'logistics_interface\' =>$requestData,
19         );
20 
21         $result = $this->sendPosti($url,$datas);提交数据
22 
23         return $result;
24     }
25     /**
26      * 电商Sign签名生成
27      * @param data 内容
28      * @param appkey Appkey
29      * @return DataSign签名
30      */
31     public function encrypti($data,$appSecret)
32     {
33         return base64_encode(md5($data.$appSecret,true));
34     }
35     /**
36      *  post提交数据
37      * @param  string $url 请求Url
38      * @param  array $datas 提交的数据
39      * @return url响应返回的html
40      */
41 
42     function sendPosti($url, $datas) {
43         $temps = array();
44         foreach ($datas as $key => $value) {
45             $temps[] = sprintf(\'%s=%s\', $key, $value);
46         }
47         $post_data = implode(\'&\', $temps);
48         $url_info = parse_url($url);
49         if(empty($url_info[\'port\']))
50         {
51             $url_info[\'port\']=80;
52         }
53         $httpheader = "POST " . $url_info[\'path\'] . " HTTP/1.0\\r\\n";
54         $httpheader.= "Host:" . $url_info[\'host\'] . "\\r\\n";
55         $httpheader.= "Content-Type:application/x-www-form-urlencoded\\r\\n";
56         $httpheader.= "Content-Length:" . strlen($post_data) . "\\r\\n";
57         $httpheader.= "Connection:close\\r\\n\\r\\n";
58         $httpheader.= $post_data;
59         $fd = fsockopen($url_info[\'host\'], $url_info[\'port\']);
60         fwrite($fd, $httpheader);
61         $gets = "";
62         $headerFlag = true;
63         while (!feof($fd)) {
64             if (($header = @fgets($fd)) && ($header == "\\r\\n" || $header == "\\n")) {
65                 break;
66             }
67         }
68         while (!feof($fd)) {
69             $gets.= fread($fd, 128);
70         }
71         fclose($fd);
72 
73         return $gets;
74     }

 

第三部.前端页面的代码

  1 <include file="public@header"/>
  2 <head>
  3     <title>Print Test</title>
  4 
  5     <style type="text/css">
  6 
  7         /*div#ctlDiv {height:750px;width:50%;float:left;border-style:solid;border-color:red;border-width:0px;}*/
  8         div#pdfDiv {height:750px;width:49%;border-style:solid;border-color:red;border-width:0px;}
  9     </style>
 10     <script type="text/javascript">
 11 
 12         var socket;
 13 
 14         window.onload = function load()
 15         {
 16 
 17             document.getElementById("pdfDiv").style.visibility="hidden";
 18         }
 19 
 20         function setStatusLabel(msg)
 21         {
 22             var eStatus = document.getElementById("connectionStatus");
 23             eStatus.innerHTML = msg;
 24         }
 25 
 26         function doConnect(callback)
 27         {
 28             var serviceUrl = document.getElementById("serviceUrl").value;
 29 
 30             socket = new WebSocket(serviceUrl);
 31 
 32             // 监听消息
 33             socket.onmessage = function(event)
 34             {
 35                 console.log(\'Client received a message\',event);
 36                 document.getElementById("pdfDiv").style.visibility="hidden";
 37                 // parse json
 38                 var obj = eval(\'(\'+event.data+\')\');
 39 
 40                 if (obj && (obj.previewURL || obj.previewImage))
 41                 {
 42                     var url;
 43                     if (obj && obj.previewURL) {
 44                         url = obj.previewURL;
 45                     } else if (obj && obj.previewImage) {
 46                         url = obj.previewImage;
 47                     }
 48                     var pdf = document.getElementById(\'pdf\');
 49                     if (pdf) {
 50                         pdf.setAttribute(\'src\', url);
 51                         document.getElementById("pdfDiv").style.visibility="visible";
 52                     }
 53                 }
 54                 var resultElement = document.getElementById("resultMessage");
 55                 resultElement.value = resultElement.value + event.data + "\\n";
 56                 resultElement.scrollTop = resultElement.scrollHeight;
 57             };
 58 
 59             socket.onopen = function(event)
 60             {
 61                 if (callback != null) {
 62                     callback();
 63                 }
 64                 setStatusLabel("已连接");
 65             }
 66 
 67             socket.onerror = function(error)
 68             {
 69                 console.log("Failed to connect CN print at " + serviceUrl, error);
 70                 setStatusLabel("连接错误");
 71             }
 72 
 73             // 监听Socket的关闭
 74             socket.onclose = function(event)
 75             {
 76                 console.log(\'Client notified socket has closed\',event);
 77                 setStatusLabel("连接关闭");
 78             };
 79         }
 80 
 81         function sendCommand()
 82         {
 83             if (typeof socket == "undefined" || socket.readyState == WebSocket.CLOSED) {
 84                 doConnect(sendCommand);
 85                 return;
 86             }
 87 
 88             if (socket.readyState != WebSocket.OPEN) {
 89                 alert("无效连接: " + socket.readyState);
 90                 return;
 91             }
 92 
 93             var msg = document.getElementById(\'printmessage\').value;
 94             var repeatCount = document.getElementById(\'repeatCount\').value;
 95             var isPreview = document.getElementById(\'isPreview\').checked;
 96             var time = new Date().getTime();
 97 
 98             for (var index = 0; index < repeatCount; index++) {
 99                 var obj = JSON.parse(msg);
100 
101                 if (obj["task"])
102                 {
103                     // taskID
104                     obj["task"]["taskID"] = time.toString() + "_TASK_" 免费圆通电子面单接口对接

圆通电子面单接口对接及调试

常用电子面单接口API及对接攻略(快递鸟菜鸟顺丰)

圆通电子面单demo

电子面单批量打印接口对接(含打印控件)

电子面单api接口_JAVA调用示例