审查视图

app/portal/controller/BirdController.php 13.0 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
     */
1  
anyv authored
39
    public function createOrder($indent_id = null)
40 41
    {
        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']);
王晓刚 authored
60
        //请求url,接口正式地址:http://api.kdniao.com/api/OOrderService    测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
61
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/EOrderService');
王晓刚 authored
62
63 64 65

        //构造在线下单提交信息
        $eorder = [];
66
        $eorder["ShipperCode"] = "$indent[logistic_name]";
王晓刚 authored
67
        $eorder["OrderCode"] = $indent['order_number'];
68 69 70 71 72 73 74 75 76 77 78 79
        $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
80 81 82 83 84 85
        $receiver["Name"] = $address['name'];
        $receiver["Mobile"] = $address['phone'];
        $receiver["ProvinceName"] = $region[0];
        $receiver["CityName"] = $region[1];
        $receiver["ExpAreaName"] = $region[2];
        $receiver["Address"] = $address['detailed'];
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);
103
        cache('test',$result);
104
        if ($result["ResultCode"] == "100") {
王晓刚 authored
105 106 107
            if(!empty($result['Order']['LogisticCode'])){
                $indentModel->updateData(['id'=>$indent_id],['logistic_code'=>$result['Order']['LogisticCode']]);
            }
1  
anyv authored
108
            return dump(['code'=>20000,'msg'=>'SUCCESS','data'=>$result]);//返回快递单号
109
        } else {
1  
anyv authored
110
            return dump(['code'=>40000,'msg'=>$result['Reason']]);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }
    }

    /**
     * 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
135 136 137 138 139 140 141
    /**
     * 查询订单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
142
    public function getOrder($indent_id = null){
王晓刚 authored
143 144 145 146 147 148 149 150 151 152
        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('未查询到该订单','','','');
        }
153 154 155
        if(!empty($indent['salesman_uid'])){
            $indent['order_number']= null;
        }
王晓刚 authored
156 157 158
//        if($indent['state'] != 5){
//            $this->error('该订单不是已发货状态','','','');
//        }
王晓刚 authored
159
王晓刚 authored
160
        //请求url,接口正式地址:http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx    测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json
王晓刚 authored
161
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');
王晓刚 authored
162
王晓刚 authored
163
        $jsonResult = $this->getOrderTracesByJson($indent['order_number'],$indent['logistic_code'],$indent['logistic_name']);
王晓刚 authored
164 165
        $result = json_decode($jsonResult,true);
        if($result['Success'] == true){
王晓刚 authored
166
            return ['code'=>20000,'msg'=>'SUCCESS','data'=>['state'=>$result['State'],'traces'=>$result['Traces']]];//订单轨迹
王晓刚 authored
167 168 169 170 171 172 173 174
        }else{
            return ['code'=>40000,'msg'=>$result['Reason']];
        }
    }

    /**
     * Json方式 查询订单物流轨迹
     */
王晓刚 authored
175
    function getOrderTracesByJson($OrderCode,$LogisticCode,$LogisticName){
176
        $requestData= "{'OrderCode':'$OrderCode','ShipperCode':'$LogisticName','LogisticCode':$LogisticCode}";
王晓刚 authored
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

        $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)){
王晓刚 authored
208
            return ['code'=>40000,'msg'=>'未查询到该订单'];
王晓刚 authored
209
王晓刚 authored
210
        }
王晓刚 authored
211 212 213
//        if($indent['state'] != 2){
//            $this->error('该订单不是已发货状态','','','');
//        }
王晓刚 authored
214 215 216 217
        //收货地址
        $where2['id'] = ['eq',$indent['indent_address']];
        $addressModel = new AddressModel();
        $address = $addressModel->findData($where2);
王晓刚 authored
218
        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInv
王晓刚 authored
219
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/dist');
王晓刚 authored
220
王晓刚 authored
221
        $jsonResult = $this->orderTracesSubByJson($indent['order_number'],$indent['logistic_code'],$address,$indent['logistic_name'],$indent['logistic_name']);
王晓刚 authored
222
        $result = json_decode($jsonResult,true);
王晓刚 authored
223
        cache('b',$result);
王晓刚 authored
224
        if(empty($result['Reason'])){
王晓刚 authored
225
            return ['code'=>20000,'msg'=>'SUCCESS'];//订单轨迹
王晓刚 authored
226
        }else{
王晓刚 authored
227
            return ['code'=>40000,'msg'=>$result['Reason']];
王晓刚 authored
228 229 230 231 232 233
        }
    }

    /**
     * Json方式  物流信息订阅
     */
王晓刚 authored
234
    public function orderTracesSubByJson($OrderCode,$LogisticCode,$address,$LogisticName){
王晓刚 authored
235 236 237
        $sender = config('sender');
        $region = explode(' ',$address['region']);
        $requestData="{'OrderCode': '$OrderCode',".
王晓刚 authored
238
            "'ShipperCode':'$LogisticName',".
王晓刚 authored
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
            "'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;
    }
268 269 270 271 272 273 274 275 276 277 278 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 313 314
    public function monitoring($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'] != 2){
////            $this->error('该订单不是已发货状态','','','');
////        }
//        //收货地址
//        $where2['id'] = ['eq',$indent['indent_address']];
//        $addressModel = new AddressModel();
//        $address = $addressModel->findData($where2);
        //请求url,接口正式地址:http://api.kdniao.com/api/eorderservice    测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInv
        defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');

        $jsonResult = $this->getOrder2();
        $result = json_decode($jsonResult,true);
        dump($result);
        if(empty($result['Reason'])){
            return ['code'=>20000,'msg'=>'SUCCESS'];//订单轨迹
        }else{
            return ['code'=>40000,'msg'=>$result['Reason']];
        }
    }
    public function getOrder2(){
        $requestData= "{'OrderCode':'','ShipperCode':'YD','LogisticCode':'3718481494155'}";

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

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

        return $result;
    }
王晓刚 authored
315 316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    /**
     *  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
332 333 334 335
        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        $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)));
    }
}