丰桥自助打印顺丰面单

本文详细介绍了如何使用丰桥三方API在服务器上部署面单打印服务,包括服务器环境配置、Java服务启动、SDK封装以及调用示例。通过这个服务,可以实现自动打印顺丰面单,并提供了面单图片生成、查询订单状态等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:基础准备工作

对接的丰桥三方官网地址:

http://qiao.sf-express.com/pages/developDoc/index.html?level2=535247

服务器需要有java环境:需要1.6或以上版本(目前用的java版本是1.8),具体安装步骤可百度搜索一下,安装完后查看下版本。

查看服务器时区,使用命令timedatectl | grep "Time zone" 查看,查看是否是 echo "Asia/Shanghai" > /etc/timezone如果不是需要时区设置成这样,不然请求面单服务生成面单图片的时候,上面打印时间会与实际生成的时间相差八小时。

服务器还需要有宋体黑体库(simsun.ttf和simhei.ttf),因为生成的面单图片上面字母生成需要服务器有这字体库。使用命令fc-list查看字体库列表有没有这两种字体库。

从顺丰丰桥官网下载面单自助打印SDK,下面这个目录文件是生成面单图片的服务。

上传到服务器上,启动这个服务命令是java -Dfile.encoding=utf-8 -jar csim_waybill_print_service_V1.1.*.jar port

启动端口默认是4040,如果不特别设置的话。

启动端口需要登录服务器云管理平台,在安全组中增加开放端口。

端口设置后,命令执行后,浏览器打开

http://服务所在服务器ip:4040/servertest.html

显示OK代表面单图片服务开启成功。

此处ip建议换成域名,这样安全些。

备注:这个面单服务建议做成长链接编写一个脚本让这个服务器不能停,避免服务停止,服务停止了就无法生成面单图片。

顺丰面单图片打印机型号:汉印(N41BT)

面单图片所用打印纸:热敏打印纸

电脑所需安装驱动下载地址:https://cn.hprt.com/XiaZai.html(搜索N41BT这个打印机对应的驱动)

驱动具体安装步骤丰桥上应该有视频教程,如果没有可以联系丰桥那边技术客服咨询进行安装。

以上基础准备工作完成,接下来进行第二大步,项目里封装面单打印sdk。

第二步:tp5框架里封装面单打印类

(1)对接顺丰接口类

common目录下新增logistics目录,common\logistics目录下新增LogisticsSdk类文件

namespace app\common\logistics;
class LogisticsSdk {
    //配置信息
    public  $SURFACE_SINGLE = array(
        'partnerID'=>'',//例如ghdfnf
        'checkword'=>'',//例如NZvqbVTXENbFkvhIXliWQRbshjdhjHJSDhjhj
        'serviceCode'=>'EXP_RECE_CREATE_ORDER',
        'call_url_box'=>'https://sfapi.sf-express.com/std/service',
        'monthlyCard'=>'',//例如 0200179234
        'cancelOrder'=>'EXP_RECE_UPDATE_ORDER',
        'queryOrder'=>'EXP_RECE_SEARCH_ORDER_RESP',
        'queryRoutes'=>'EXP_RECE_SEARCH_ROUTES'
    );


    public function __construct()
    {
        $this->partnerID    =$this->SURFACE_SINGLE['partnerID'];//顾客编码;
        $this->checkword    =$this->SURFACE_SINGLE['checkword'];//校验码
        $this->serviceCode  =$this->SURFACE_SINGLE['serviceCode'];//生成面单图片服务接口
        $this->call_url_box =$this->SURFACE_SINGLE['call_url_box'];//环境的地址
        $this->monthlyCard  =$this->SURFACE_SINGLE['monthlyCard'];//月结卡号
        $this->queryOrder   =$this->SURFACE_SINGLE['queryOrder'];//查询订单服务接口
        $this->cancelOrder  =$this->SURFACE_SINGLE['cancelOrder'];//取消订单服务接口
        $this->queryRoutes  =$this->SURFACE_SINGLE['queryRoutes'];//路由查询接口
    }


    /*顺丰订单取消*/
    public function cancelOrder($xiao_order_id){
        //发送数据包
        $sendData=[
            'dealType'=>2,
            'language'=>'zh-CN',
            'orderId'=>$xiao_order_id,
            'totalWeight'=>0,
            'waybillNoInfoList'=>[]
        ];
        //发送参数
        $post_data = array(
            'serviceCode' => $this->cancelOrder,
        );
        $res=$this->sign_send($sendData,$post_data);
        $resu=json_decode($res['apiResultData'],true);
        if($resu['success']==1){
            return json_encode(['code'=>1,'msg'=>'取消成功','data'=>[]]);
        }else{
            return json_encode(['code'=>0,'msg'=>$resu['errorMsg'],'data'=>[]]);
        }
    }


    /*顺丰路由查询*/
    public function queryRoutes($express_order){
            $sendData=[
                'trackingType'=>1,
                'trackingNumber'=>$express_order,
                'language'=>'0',
                'methodType'=>1
            ];
            $post_data = array(
                'serviceCode' => $this->queryRoutes,
            );
            $res=$this->sign_send($sendData,$post_data);
            $apiResultData=json_decode($res['apiResultData'],true);
            if(!empty($apiResultData['msgData'])){
                $routeResps=$apiResultData['msgData']['routeResps'];
                $routes=$routeResps[0]['routes'];
                if(!empty($routes)){
                    //更新快递状态
                    $opCode=array_column($routes,'opCode');
                    return json_encode(['code'=>1,'msg'=>'请求成功','data'=>$opCode]);
                }else{
                    return json_encode(['code'=>0,'msg'=>'没有查询到信息','data'=>[]]);
                }
            }else{
                return json_encode(['code'=>0,'msg'=>'请求失败','data'=>'']);
            }
    }


    //生成签名
    public function sign_send($sendData,$post_data){
        $msgData=json_encode($sendData);
        $timestamp = time();
        //通过MD5和BASE64生成数字签名
        $msgDigest = base64_encode(md5((urlencode($msgData .$timestamp. $this->checkword)), TRUE));
        $post_data['partnerID']=$this->partnerID;
        $post_data['requestID']=$this->create_uuid();
        $post_data['timestamp']=$timestamp;
        $post_data['msgData']  =$msgData;
        $post_data['msgDigest']=$msgDigest;
        $resultCont = $this->send_post($this->call_url_box, $post_data);
        $res=json_decode($resultCont,true);
        return $res;
    }

    //获取UUID
    public function create_uuid() {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        return $uuid ;
    }

    //发送请求
    public function send_post($url,$post_data) {
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded;charset=utf-8',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间(单位:s)
            )
        );
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)');
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }


    /*顺丰路由查询*/
    public function queryRoutesInfo($express_order){
        $sendData=[
            'trackingType'=>1,
            'trackingNumber'=>$express_order,
            'language'=>'0',
            'methodType'=>1
        ];
        $post_data = array(
            'serviceCode' => $this->queryRoutes,
        );
        $res=$this->sign_send($sendData,$post_data);

        $apiResultData=json_decode($res['apiResultData'],true);
        if($apiResultData['success']){
            if(!empty($apiResultData['msgData'])){
                $routeResps=$apiResultData['msgData']['routeResps'];
                $routes=$routeResps[0]['routes'];
                return json_encode(['code'=>1,'msg'=>'请求成功','data'=>$routes],320);
            }else{
                return json_encode(['code'=>0,'msg'=>'请求失败','data'=>'']);
            }
        }else{
            return json_encode(['code'=>0,'msg'=>'请求失败','data'=>'']);
        }

    }
}

(2) 调用顺丰面单服务类WaybillPrinterSdk文件

class WaybillPrinterSdk
{
    public  $host = 'http://test.logistics.xiaodongai.com';
    public  function createPhoto($data=[],$drug=[]){
        /**
         * *******2联150 丰密运单*************
         */
        /**
         * 调用打印机 不弹出窗口 适用于批量打印【二联单】
         */
//        $reqURL = "http://localhost:4040/sf/waybill/print?type=V2.0.FM_poster_100mm150mm&output=noAlertPrint";
        /**
         * 调用打印机 弹出窗口 可选择份数 适用于单张打印【二联单】
         */
//        $reqURL = "http://localhost:4040:4040/sf/waybill/print?type=V2.0.FM_poster_100mm150mm&output=print";
        /**
         * 直接输出图片的BASE64编码字符串 可以使用html标签直接转换成图片【二联单】
         */
        $reqURL = "http://面单服务所部署服务器ip:4040/sf/waybill/print?type=V2.0.FM_poster_100mm150mm&output=image";//ip建议换成域名,这样安全些
//        $reqURL = "http://localhost:4040/sf/waybill/print?type=V2.0.FM_poster_100mm150mm&output=image";

        /**
         * *******3联210 丰密运单*************
         */
        /**
         * 调用打印机 不弹出窗口 适用于批量打印【三联单】
         */
        $url10 = "http://localhost:4040/sf/waybill/print?type=V3.0.FM_poster_100mm210mm&output=noAlertPrint";
        /**
         * 调用打印机 弹出窗口 可选择份数 适用于单张打印【三联单】
         */
//        $reqURL = "http://localhost:4040/sf/waybill/print?type=V3.0.FM_poster_100mm210mm&output=print";

        /**
         * 直接输出图片的BASE64编码字符串 可以使用html标签直接转换成图片【三联单】
         */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值