Express.php 4.2 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\admin\model\Order;
use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
 * 物流接口
 */
class Express extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $uid = '';//token存贮user_id
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (调用查询物流轨迹)
     * @ApiSummary  (调用查询物流轨迹)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/express/getExpress)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1575532542",
        "data": {
        "LogisticCode": "75317246876602",//快递单号
        "ShipperCode": "ZTO",//快递编码
        "Traces": [
            {
                "AcceptStation": "【东莞市】  【东莞厚街】(0769-86947628) 的 张小姐(18027104980) 已揽收",//轨迹
                "AcceptTime": "2019-12-03 18:43:03"//时间
            },
            {
                "AcceptStation": "【东莞市】  快件离开 【东莞厚街】 已发往 【天津中转部】",
                "AcceptTime": "2019-12-04 00:18:01"
            },
            {
                "AcceptStation": "【天津市】  快件离开 【天津中转部】 已发往 【天津南开六部】",
                "AcceptTime": "2019-12-05 14:35:03"
            }
        ],
        "State": "2",//2:在途中,3:签收,4:问题件
        "EBusinessID": "1442433",
        "Success": true,
        "LogisticCompany": "中通快递"//快递公司
        }
    })
     *
     */
    public function getExpress(){
        $order_id = $this->request->get('order_id');

        $rule = config('verify.order_detail');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['order_id'=>$order_id])) {
            $this->error($validate->getError());
        }

        $where = ['id'=>$order_id,'uid'=>$this->uid];
        $order = Common::findSoftWhereData('order',$where,'id,ShipperCode,LogisticCode,LogisticCompany');
        if($order){
            $logisticResult = $this->getOrderTracesByJson($order['LogisticCode'],$order['ShipperCode']);//快递编码,单号
            $data = json_decode($logisticResult,true);
            if($data['Success'] == true){//返回信息成功
                $data['LogisticCompany'] = $order['LogisticCompany'];
                $this->success('成功',$data);
            }
            $this->error('未找到物流踪迹');
        }
        $this->error('未找到物流踪迹');
    }

    /**
     * 物流
     * @ApiInternal
     */
    public function getOrderTracesByJson($kgs,$number){
        $express = config('verify.express');
        $EBusinessID = $express['EBusinessID'];
        $appKey = $express['appKey'];
        $url = $express['url'];
        $requestData= "{'OrderCode':'','ShipperCode':'$kgs','LogisticCode':'$number'}";
        $data = array(
            'EBusinessID' => $EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $data['DataSign'] = $this->encrypt($requestData.$appKey);
        $result = $this->auth->http_post($url, $data);
        return $result;
    }

    /**
     * 签名
     * @ApiInternal
     */
    public function encrypt($str) {
        return urlencode(base64_encode(md5($str)));
    }
}