审查视图

app/portal/controller/BirdController.php 10.5 KB
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/7
 * Time: 9:25
 */

namespace app\portal\controller;

王晓刚 authored
12
use app\portal\model\AddressModel;
13 14 15 16 17 18
use app\portal\model\IndentModel;
use cmf\controller\HomeBaseController;
use think\Db;

class BirdController extends HomeBaseController
{
王晓刚 authored
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    protected $EBusinessID;
    protected $AppKey;
    protected $indent_id;
    function _initialize() {
        $bird = config('bird');
        $this->EBusinessID = $bird['EBusinessID'];
        $this->AppKey = $bird['AppKey'];
        //电商ID
        defined('EBusinessID') or define('EBusinessID', $this->EBusinessID);
        //电商加密私钥,快递鸟提供,注意保管,不要泄漏
        defined('AppKey') or define('AppKey', $this->AppKey);
    }

    /**
     * 预约取件
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
39 40 41
    public function createOrder($indent_id = null)
    {
        if(empty($indent_id)){
王晓刚 authored
42
            return "缺少必要参数";
43
        }
王晓刚 authored
44 45
        $this->indent_id = $indent_id;
        $where1['id'] = ['eq',$this->indent_id];
46
        $indentModel = new IndentModel();
王晓刚 authored
47
        $indent = $indentModel->findData($where1);
48
        if(empty($indent)){
王晓刚 authored
49
            return '未查询到该订单';
50 51
        }
        if($indent['state'] != 2){
王晓刚 authored
52
            return '该订单不是待发货状态';
53
        }
王晓刚 authored
54 55 56 57 58 59
        //收货地址
        $where2['id'] = ['eq',$indent['indent_address']];
        $addressModel = new AddressModel();
        $address = $addressModel->findData($where2);
        $region = explode(' ',$address['region']);
60
        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://testapi.kdniao.com:8081/api/oorderservice
王晓刚 authored
61
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/OOrderService');
62 63 64 65

        //构造在线下单提交信息
        $eorder = [];
        $eorder["ShipperCode"] = "SF";
王晓刚 authored
66
        $eorder["OrderCode"] = $indent['order_number'];
67 68 69 70 71 72 73 74 75 76 77 78
        $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 = [];
王晓刚 authored
79 80 81 82 83 84
        $receiver["Name"] = $address['name'];
        $receiver["Mobile"] = $address['phone'];
        $receiver["ProvinceName"] = $region[0];
        $receiver["CityName"] = $region[1];
        $receiver["ExpAreaName"] = $region[2];
        $receiver["Address"] = $address['detailed'];
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        $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") {
王晓刚 authored
103
            return ['code'=>20000,'msg'=>'SUCCESS','data'=>$result['Order']['LogisticCode']];//返回快递单号
104
        } else {
王晓刚 authored
105
            return ['code'=>40000,'msg'=>$result['Reason']];
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        }
    }

    /**
     * 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;
    }
王晓刚 authored
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    /**
     * 查询订单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrder($indent_id = null){
        if(empty($indent_id)){
            $this->error('缺少必要参数','','','');
        }
        $this->indent_id = $indent_id;
        $where['id'] = ['eq',$this->indent_id];
        $indentModel = new IndentModel();
        $indent = $indentModel->findData($where);
        if(empty($indent)){
            $this->error('未查询到该订单','','','');
        }
        if($indent['state'] != 5){
            $this->error('该订单不是已发货状态','','','');
        }

        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');

        $jsonResult = $this->getOrderTracesByJson($indent['order_number'],$indent['logistic_code']);
        $result = json_decode($jsonResult,true);
        if($result['Success'] == true){
王晓刚 authored
158
            return ['code'=>20000,'msg'=>'SUCCESS','data'=>['state'=>$result['State'],'traces'=>$result['Traces']]];//订单轨迹
王晓刚 authored
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        }else{
            return ['code'=>40000,'msg'=>$result['Reason']];
        }
    }

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

        $datas = array(
            'EBusinessID' => EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, AppKey);
        $result=$this->sendPost(ReqURL, $datas);

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

        return $result;
    }

    /**
     * 订阅订单轨迹
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function takeOrder($indent_id = null){
        if(empty($indent_id)){
            $this->error('缺少必要参数','','','');
        }
        $this->indent_id = $indent_id;
        $where['id'] = ['eq',$this->indent_id];
        $indentModel = new IndentModel();
        $indent = $indentModel->findData($where);
        if(empty($indent)){
            $this->error('未查询到该订单','','','');
        }
        if($indent['state'] != 5){
            $this->error('该订单不是已发货状态','','','');
        }
        //收货地址
        $where2['id'] = ['eq',$indent['indent_address']];
        $addressModel = new AddressModel();
        $address = $addressModel->findData($where2);
        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://testapi.kdniao.com:8081/api/oorderservice
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/dist');

        $jsonResult = $this->orderTracesSubByJson($indent['order_number'],$indent['logistic_code'],$address);
        $result = json_decode($jsonResult,true);
        if(empty($result['Reason'])){
            return dump($result);//订单轨迹
        }else{
            return $result['Reason'];
        }
    }

    /**
     * Json方式  物流信息订阅
     */
    public function orderTracesSubByJson($OrderCode,$LogisticCode,$address){
        $sender = config('sender');
        $region = explode(' ',$address['region']);
        $requestData="{'OrderCode': '$OrderCode',".
            "'ShipperCode':'SF',".
            "'LogisticCode':'$LogisticCode',".
            "'PayType':1,".
            "'ExpType':1,".
            "'IsNotice':0,".
            "'Sender':".
            "{".
            "'Company':'LV','Name':'$sender[Name]','Mobile':'$sender[Mobile]','ProvinceName':'$sender[ProvinceName]','CityName':'$sender[CityName]','ExpAreaName':'$sender[ExpAreaName]','Address':'$sender[Address]'},".
            "'Receiver':".
            "{".
            "'Company':'GCCUI','Name':'$address[name]','Mobile':'$address[phone]','ProvinceName':'$region[0]','CityName':'$region[1]','ExpAreaName':'$region[2]','Address':'$address[detailed]'},".
            "'Commodity':".
            "[{".
            "'GoodsName':'书'}],".
            "'Remark':'小心轻放'}";

        $datas = array(
            'EBusinessID' => EBusinessID,
            'RequestType' => '1008',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, AppKey);
        $result = $this->sendPost(ReqURL, $datas);

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

        return $result;
    }


260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

    /**
     *  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);
王晓刚 authored
275 276 277 278
        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        $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)));
    }
}