BirdController.php 4.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/7
 * Time: 9:25
 */

namespace app\portal\controller;


use app\portal\model\IndentModel;
use cmf\controller\HomeBaseController;
use think\Db;

class BirdController extends HomeBaseController
{
    public function createOrder($indent_id = null)
    {
        if(empty($indent_id)){
            $this->error('缺少必要参数','','','');
        }
        $where['id'] = ['eq',$indent_id];
        $indentModel = new IndentModel();
        $indent = $indentModel->findData($where);
        if(empty($indent)){
            $this->error('未查询到该订单','','','');
        }
        if($indent['state'] != 2){
            $this->error('该订单不是待发货状态','','','');
        }
        $bird = config('bird');
        //电商ID
        defined('EBusinessID') or define('EBusinessID', $bird['EBusinessID']);
        //电商加密私钥,快递鸟提供,注意保管,不要泄漏
        defined('AppKey') or define('AppKey', $bird['AppKey']);
        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://testapi.kdniao.com:8081/api/oorderservice
        defined('ReqURL') or define('ReqURL', 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json');

        //构造在线下单提交信息
        $eorder = [];
        $eorder["ShipperCode"] = "SF";
        $eorder["OrderCode"] = "PM2016050789471";
        $eorder["PayType"] = 1;
        $eorder["ExpType"] = 1;
        $eorder['IsNotice'] = 0;
        $sender = config('sender');
//        $sender["Name"] = "李先生";
//        $sender["Mobile"] = "18888888888";
//        $sender["ProvinceName"] = "李先生";
//        $sender["CityName"] = "深圳市";
//        $sender["ExpAreaName"] = "福田区";
//        $sender["Address"] = "赛格广场5401AB";

        $receiver = [];
        $receiver["Name"] = "李先生";
        $receiver["Mobile"] = "18888888888";
        $receiver["ProvinceName"] = "李先生";
        $receiver["CityName"] = "深圳市";
        $receiver["ExpAreaName"] = "福田区";
        $receiver["Address"] = "赛格广场5401AB";

        $commodityOne = [];
        $commodityOne["GoodsName"] = "书";
        $commodity = [];
        $commodity[] = $commodityOne;

        $eorder["Sender"] = $sender;
        $eorder["Receiver"] = $receiver;
        $eorder["Commodity"] = $commodity;


        //调用在线下单
        $jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
        $jsonResult = $this->submitOOrder($jsonParam);

        //解析在线下单返回结果
        $result = json_decode($jsonResult, true);
        if ($result["ResultCode"] == "100") {
            return $result['Order']['LogisticCode'];//返回快递单号
        } else {
            return $result['Reason'];
        }
    }


    /**
     * Json方式 提交在线下单
     * @param $requestData
     * @return url响应返回的html
     */
    function submitOOrder($requestData)
    {
        $datas = array(
            'EBusinessID' => EBusinessID,
            'RequestType' => '1001',
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, AppKey);
        $result = $this->sendPost(ReqURL, $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);
        $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)));
    }
}