KdBrod.php 5.6 KB
<?php

namespace app\api\controller;

use think\Db;
use app\common\controller\Api;

//调用查询物流轨迹
//---------------------------------------------

/**
 * 快递接口
 */
class KdBrod extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiTitle    (快递接口-查询)
     * @ApiSummary  (查询)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Kd_Brod/kuaidi)
     * @ApiParams   (name="shop", type="string", required=true, description="快递名称")
     * @ApiParams   (name="shop_order", type="string", required=true, description="快递单号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "LogisticCode": "75370537792337",
    "ShipperCode": "中通快递",
    "Traces": [
    {
    "AcceptStation": "【广州鸿顺】(020-22046523) 的 温建阳(19927413363) 已揽收",
    "AcceptTime": "2020-07-15 16:03:05"
    },
    {
    "AcceptStation": "快件离开 【广州鸿顺】 已发往 【天津中转部】",
    "AcceptTime": "2020-07-16 04:51:28"
    },
    {
    "AcceptStation": "快件已经到达 【广州中心】",
    "AcceptTime": "2020-07-16 08:44:19"
    },
    {
    "AcceptStation": "快件离开 【广州中心】 已发往 【天津中转部】",
    "AcceptTime": "2020-07-16 08:54:04"
    },
    {
    "AcceptStation": "快件已经到达 【天津中转部】",
    "AcceptTime": "2020-07-17 22:09:53"
    },
    {
    "AcceptStation": "快件离开 【天津中转部】 已发往 【天津海泰分部】",
    "AcceptTime": "2020-07-17 22:10:02"
    },
    {
    "AcceptStation": "快件已经到达 【天津海泰分部】",
    "AcceptTime": "2020-07-18 06:38:50"
    },
    {
    "AcceptStation": "【天津海泰分部】 的吴可可(18622722466) 正在第1次派件, 请保持电话畅通,并耐心等待(95720为中通快递员外呼专属号码,请放心接听)",
    "AcceptTime": "2020-07-18 07:34:56"
    },
    {
    "AcceptStation": "您的快递已暂存至【菜鸟的天津格调松间北里配建3】。如有问题请电联:(18622722466), 投诉电话:(9519666), 感谢您使用中通快递, 期待再次为您服务!",
    "AcceptTime": "2020-07-18 10:03:05"
    }
    ],
    "State": "2",
    "EBusinessID": "1649160",
    "Success": true
    }
    })
     */
    public function kuaidi()
    {
        $param = $this->request->param();
        $code = Db::name('kuaidicode')->where(['kuaidi' => $param['shop']])->value('code');
        $logisticResult = $this->getOrderTracesByJson($code, $param['shop_order']);
        $kuaidi = json_decode(htmlspecialchars_decode($logisticResult), true);
        $shopname = Db::name('kuaidicode')->where(['code' => $kuaidi['ShipperCode']])->value('kuaidi');
        $return['LogisticCode'] = $kuaidi['LogisticCode'];
        $return['ShipperCode'] = $shopname;
        $return['Traces'] = $kuaidi['Traces'];
//        $return['State'] = $kuaidi['State'];
//        $return['EBusinessID'] = $kuaidi['EBusinessID'];
//        $return['Success'] = $kuaidi['Success'];
        $this->success('成功', $return);
    }
//---------------------------------------------

    /**
     * Json方式 查询订单物流轨迹
     */
    function getOrderTracesByJson($kgs, $number)
    {
        $requestData = "{'OrderCode':'','ShipperCode':'$kgs','LogisticCode':'$number'}";

        $datas = array(
            'EBusinessID' => '1649160',
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, 'fdf75944-8e72-4c72-b40f-534b0336b552');
        $result = $this->sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);

        //根据公司业务处理返回的信息......

        return $result;
    }

    /**
     *  post提交数据
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据
     * @return url响应返回的html
     */
    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;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    function encrypt($data, $appkey)
    {
        return urlencode(base64_encode(md5($data . $appkey)));
    }

}

?>