打印快递鸟电子面单

968 阅读1分钟
<div id="hh">
       打印内容展示
</div>
<button class="btn btn-default btn-xs" type="button" id ="aaa">打印</button>


$(document).ready(function(){     
      $("#aaa").click(function(){ 
         $.ajax({
            type:"post",
            url:"{:url('Mall/orderface')}",
            data:'number='+$number+'&id='+$id,
            dataType:'json',                
            success:function(data){                   
            if (data.ret == 0){                        
                 alert(data.msg);                    
            }else{                        
              $count = data.data.length;                                            
              for (var i=0;i<$count;i++){                                                    
                 $('#hh').html(data.data);                            
                 $('#hh').show();                           
                 $("#hh").printArea();                            
                 $('#hh').hide();                        
              }                    
            }                
            }            
         });
       }); 
});

public function orderface(){
    $id = $_POST['id'];
    $number = $_POST['number'];
    $config_model = new Config();
    $config = $config_model->findOne(['uid'=>UID]);
    $mall_order_model = new MallOrder();
    $order = $mall_order_model->findOne(['uid' => 2, 'id' => $id]);
    //构造电子面单提交信息
    //收件人信息 
    $receiver = [
        "Name"=>$order['linkman'],//收件人
        "Mobile"=>$order['mobile_phone'],//电话与手机,必填一个
        "ProvinceName"=>$order['province'],//收件省
        "CityName"=>$order['city'],//收件市
        "ExpAreaName"=>$order['area'],//收件区/县
        "Address"=>$order['address'],//收件人详细地址
    ];
    //发件人信息
    $sender = [
        "Name"=>$config['sender_name'],//发件人
        "Mobile"=>$config['sender_phone'],//电话与手机,必填一个
        "ProvinceName"=>$config['province'],//收件省
        "CityName"=>$config['city'],//发件市
        "ExpAreaName"=>$config['area'],//发件区/县
        "Address"=>$config['sender_address'],//发件人详细地址
    ];
    $commodity = [];
    if ($id) {
        $order_id = $id;
        $mall_order_product_model = new MallOrderProduct();
        $order_product = $mall_order_product_model->selectList(['order_id' => array('IN', $order_id)]);
        foreach ($order_product as $key => $value) {
            $sku_name = $this->skuToName($value['product_sku'], $value['product_id']);
            $arr = [
                "GoodsName"=>$value['product_name'].$sku_name,
                "Goodsquantity"=>$value['number'],
                "GoodsPrice"=>$value['product_price']
            ];
            array_push($commodity,$arr);
        }
    }
    $kdiniao_express_model = new KdiniaoExpress();
    $code = $kdiniao_express_model->where(['id'=>$order['express_id']])->find();
    $eorder = [
        "ShipperCode"=>$code['encipher'],//快递公司编码
        "OrderCode"=>$order['order_number'],//订单编号(自定义,不可重复)
        "PayType"=>1,//邮费支付方式:1-现付,2-到付,3-月结,4-第三方支付(仅SF支持)
        "ExpType"=>1,//快递类型:1-标准快件
        "Sender"=>$sender,
        "Receiver"=>$receiver,
        "Commodity"=>$commodity,
        "Quantity"=>(int)$number,//包裹数(最多支持30件)
        "Remark"=>"",//备注
        "IsReturnPrintTemplate"=>1//返回电子面单模板:0-不需要;1-需要
    ];
    $jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
    $jc_api_model = new JcApi();
    $result = $jc_api_model->getExterFaceByKdniao($jsonParam);
    //解析电子面单返回结果
    $result = json_decode($result, true);
    if ($result['Success'] == false){
        return ajaxFalse($result['Reason']);
    }
    $PrintTemplate = [
        $result['PrintTemplate']
    ];
    if (array_key_exists('SubPrintTemplates',$result)){
        foreach($result['SubPrintTemplates'] as $kay => $val){
            array_push($PrintTemplate,$val);
        }
    }
    return ajaxSuccess($PrintTemplate);
}
function getExterFaceByKdniao($content)
{
        $jc_third_party_config_model = new JcThirdPartyConfig();
        $deliver_kdniao_config = $jc_third_party_config_model->getKdniaoDeliveryConfig();
        // 测试$deliver_kdniao_config = ['kdniao_user_id'=>'test1597343','kdniao_api_key'=>'07ca9479-e5ff-4e72-81cf-32511eb30000'];
        $datas = array(
            'EBusinessID' => $deliver_kdniao_config['kdniao_user_id'],//客户id
            'RequestType' => '1007',
            'RequestData' => urlencode($content) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($content, $deliver_kdniao_config['kdniao_api_key']);
//        $url = "http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json";//测试
        $url = "https://api.kdniao.com/api/EOrderService";//正式
        $result = $this->sendPost($url, $datas);
        return $result;
    }
//生成签名
function encrypt($data, $appkey) 
{
    return urlencode(base64_encode(md5($data.$appkey)));
}/** *  post提交数据 * @param */
function sendPost($url, $datas) {
    $temps = array();
    foreach ($datas as $key => $value) {
        $temps[] = sprintf('%s=%s', $key, $value);
    }
    $post_data = implode('&', $temps);
    $url_info = parse_url($url);
    if(empty($url_info['port']))
    {
        $url_info['port']=80;
    }
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader.= "Host:" . $url_info['host'] . "\r\n";
    $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
    $httpheader.= "Connection:close\r\n\r\n";
    $httpheader.= $post_data;
    $fd = fsockopen($url_info['host'], $url_info['port']);
    fwrite($fd, $httpheader);
    $gets = "";
    $headerFlag = true;
    while (!feof($fd)) {
        if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
            break;
        }
    }
    while (!feof($fd)) {
        $gets.= fread($fd, 128);
    }
    fclose($fd);
    return $gets;
}

下载jquery.printarea.js :   plugins.jquery.com/PrintArea/

下载快递鸟官方Demo:  www.kdniao.com/api-eorder