1.
https://open.taobao.com/docs/doc.htm?spm=a21da.7629140.0.0.97iBG8&docType=1&articleId=107052 电子面单接入文档(LINK接入)
2.
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 "; 54 $httpheader.= "Host:" . $url_info['host'] . " "; 55 $httpheader.= "Content-Type:application/x-www-form-urlencoded "; 56 $httpheader.= "Content-Length:" . strlen($post_data) . " "; 57 $httpheader.= "Connection:close "; 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 == " " || $header == " ")) { 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;50%;float:left;border-style:solid;border-color:red;border-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 + " "; 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_" + index; 105 106 // preview 107 obj["task"]["preview"] = isPreview; 108 } 109 110 var newMsg = JSON.stringify(obj); 111 socket.send(newMsg); 112 } 113 } 114 115 function clearResult() 116 { 117 var resultElement = document.getElementById("resultMessage"); 118 resultElement.value = ""; 119 } 120 </script> 121 122 123 </head> 124 <body> 125 126 <div id="ctlDiv"> 127 128 <form name="formname" action="javascript:sendCommand()"> 129 <p hidden>重复发送请求:<input id="repeatCount" type="number" min="1" max="1000" step="1" value="1"> 130 <input hidden id="isPreview" type="checkbox" checked="true">预览模式 131 </p> 132 <p> <input hidden type="text" id="serviceUrl" name="serviceUrl" value="ws://localhost:13528" /> 133 <label hidden id="connectionStatus">未知</label> 134 <input hidden id="dianji" type="submit" value="发送请求"/> 135 </p> 136 <textarea hidden cols="80" rows="20" id="printmessage" name="printmessage">{ 137 "cmd": "print", 138 "requestID": "123458976", 139 "version": "1.0", 140 "task": { 141 "taskID": "7293666", 142 "preview": false, 143 "printer": "", 144 "notifyMode": "allInOne", 145 "previewType": "html", 146 "documents": [ 147 { 148 "documentID": "0123456789", 149 "contents": [ 150 {$list}//数据是在这里哦接收的,你不需要改什么,如果你觉得这不是你想要的,你也可以借鉴一下方法 151 ] 152 } 153 ] 154 } 155 } 156 </textarea> 157 <br/><br/> 158 <textarea hidden cols="80" rows="20" id="resultMessage" name="resultMessage" readonly="readonly"></textarea> 159 <br/> 160 <input hidden type="button" value="清除返回消息" onclick="clearResult()"/> 161 </form> 162 </div> 163 164 <div style="margin: 0 auto" id="pdfDiv" align="middle"> 165 <iframe id="pdf" src="" width="100%" height="100%"> 166 </iframe> 167 </div> 168 169 </body> 170 </html> 171 172 <script> 173 $('#dianji').click(); 174 175 176 </script>
最后放一张预览成功的图片(注:必须安装菜鸟打印组件菜鸟预览)
这个需要右键打印,或点击打印的图标才能打印,打印情况就说到这里了,有什么不懂的可以留言