<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/6/7 * Time: 9:25 */ namespace app\portal\controller; use app\portal\model\AddressModel; use app\portal\model\IndentModel; use cmf\controller\HomeBaseController; use think\Db; class BirdController extends HomeBaseController { 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 */ public function createOrder($indent_id = null) { if(empty($indent_id)){ return "缺少必要参数"; } $this->indent_id = $indent_id; $where1['id'] = ['eq',$this->indent_id]; $indentModel = new IndentModel(); $indent = $indentModel->findData($where1); if(empty($indent)){ return '未查询到该订单'; } if($indent['state'] != 2){ return '该订单不是待发货状态'; } //收货地址 $where2['id'] = ['eq',$indent['indent_address']]; $addressModel = new AddressModel(); $address = $addressModel->findData($where2); $region = explode(' ',$address['region']); //请求url,接口正式地址:http://api.kdniao.com/api/OOrderService 测试环境地址:http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/api/EOrderService'); //构造在线下单提交信息 $eorder = []; $eorder["ShipperCode"] = "$indent[logistic_name]"; $eorder["OrderCode"] = $indent['order_number']; $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"] = $address['name']; $receiver["Mobile"] = $address['phone']; $receiver["ProvinceName"] = $region[0]; $receiver["CityName"] = $region[1]; $receiver["ExpAreaName"] = $region[2]; $receiver["Address"] = $address['detailed']; $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); cache('test',$result); if ($result["ResultCode"] == "100") { if(!empty($result['Order']['LogisticCode'])){ $indentModel->updateData(['id'=>$indent_id],['logistic_code'=>$result['Order']['LogisticCode']]); } return dump(['code'=>20000,'msg'=>'SUCCESS','data'=>$result]);//返回快递单号 } else { return dump(['code'=>40000,'msg'=>$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; } /** * 查询订单 * @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(!empty($indent['salesman_uid'])){ $indent['order_number']= null; } // if($indent['state'] != 5){ // $this->error('该订单不是已发货状态','','',''); // } //请求url,接口正式地址:http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx 测试环境地址: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'],$indent['logistic_name']); $result = json_decode($jsonResult,true); if($result['Success'] == true){ return ['code'=>20000,'msg'=>'SUCCESS','data'=>['state'=>$result['State'],'traces'=>$result['Traces']]];//订单轨迹 }else{ return ['code'=>40000,'msg'=>$result['Reason']]; } } /** * Json方式 查询订单物流轨迹 */ function getOrderTracesByJson($OrderCode,$LogisticCode,$LogisticName){ $requestData= "{'OrderCode':'$OrderCode','ShipperCode':'$LogisticName','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)){ return ['code'=>40000,'msg'=>'未查询到该订单']; } // 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/api/dist'); $jsonResult = $this->orderTracesSubByJson($indent['order_number'],$indent['logistic_code'],$address,$indent['logistic_name'],$indent['logistic_name']); $result = json_decode($jsonResult,true); cache('b',$result); if(empty($result['Reason'])){ return ['code'=>20000,'msg'=>'SUCCESS'];//订单轨迹 }else{ return ['code'=>40000,'msg'=>$result['Reason']]; } } /** * Json方式 物流信息订阅 */ public function orderTracesSubByJson($OrderCode,$LogisticCode,$address,$LogisticName){ $sender = config('sender'); $region = explode(' ',$address['region']); $requestData="{'OrderCode': '$OrderCode',". "'ShipperCode':'$LogisticName',". "'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; } 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; } /** * 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"; $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))); } }