审查视图

application/api/controller/KdBrod.php 5.6 KB
wangzhi authored
1 2
<?php
wangzhi authored
3 4
namespace app\api\controller;
wangzhi authored
5 6 7
use think\Db;
use app\common\controller\Api;
wangzhi authored
8 9
//调用查询物流轨迹
//---------------------------------------------
wangzhi authored
10 11 12 13

/**
 * 快递接口
 */
wangzhi authored
14
class KdBrod extends Api
wangzhi authored
15 16 17 18 19 20 21 22 23 24
{

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

    public function _initialize()
    {
        parent::_initialize();
    }
wangzhi authored
25 26 27 28
    /**
     * @ApiTitle    (快递接口-查询)
     * @ApiSummary  (查询)
     * @ApiMethod   (POST)
wangzhi authored
29
     * @ApiRoute    (/api/Kd_Brod/kuaidi)
wangzhi authored
30 31 32 33 34 35 36
     * @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':'返回成功'
wangzhi authored
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    "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
    }
wangzhi authored
82 83
    })
     */
wangzhi authored
84
    public function kuaidi()
wangzhi authored
85 86
    {
        $param = $this->request->param();
wangzhi authored
87 88
        $code = Db::name('kuaidicode')->where(['kuaidi' => $param['shop']])->value('code');
        $logisticResult = $this->getOrderTracesByJson($code, $param['shop_order']);
wangzhi authored
89
        $kuaidi = json_decode(htmlspecialchars_decode($logisticResult), true);
wangzhi authored
90 91
        $shopname = Db::name('kuaidicode')->where(['code' => $kuaidi['ShipperCode']])->value('kuaidi');
        $return['LogisticCode'] = $kuaidi['LogisticCode'];
wangzhi authored
92
        $return['ShipperCode'] = $shopname;
wangzhi authored
93
        $return['Traces'] = $kuaidi['Traces'];
王智 authored
94 95 96
//        $return['State'] = $kuaidi['State'];
//        $return['EBusinessID'] = $kuaidi['EBusinessID'];
//        $return['Success'] = $kuaidi['Success'];
王智 authored
97
        $this->success('成功', $return);
wangzhi authored
98 99 100 101 102 103 104 105 106 107 108
    }
//---------------------------------------------

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

        $datas = array(
wangzhi authored
109
            'EBusinessID' => '1649160',
wangzhi authored
110 111 112 113
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
wangzhi authored
114
        $datas['DataSign'] = $this->encrypt($requestData, 'fdf75944-8e72-4c72-b40f-534b0336b552');
wangzhi authored
115
        $result = $this->sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);
wangzhi authored
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

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

        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";
wangzhi authored
141
        $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
wangzhi authored
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        $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)));
    }

}

?>