万国优联物流对接api查询物流轨迹<?php
namespace App\Services\Sale\Shipping;
use DB;
use App\Models\Common\Config;
abstract class LogisticsAbstract
{
/** 接口请求地址
* @var
*/
protected static $ApiUrl;//请求接口地址
protected static $TimeOut = 30;//请求超时
/** 获取该物流的配置参数
* @return mixed
*/
public function getConfig($shipping_name)
{
$config = Config::where('shipping_name', $shipping_name)->pluck('value', 'name');
if (empty($config)) {
die('config empty!');
}
return $config;
}
/** 保存异常信息
* @param $tracking_no
* @param $exception
*/
public function saveException($tracking_no, $exception, $enable = 0)
{
if (empty($tracking_no) || empty($exception)) return;
$saleShippingTrackingModel = new \App\Models\Sale\Shipping\SaleShippingTracking();
$update = [
'remark' => $exception,
'enable' => $enable,
];
$saleShippingTrackingModel->where('last_tracking_number', $tracking_no)->update($update);
}
/** 将结果写入轨迹信息表中
* @param $carrier_id 物流商id
* @param $row 格式化好了的一条轨迹信息
* @param $show_log 是否显示日志信息
*/
public function saveToSaleShippingTracking($carrier_id, $row, $show_log = true)
{
if (empty($row)) return;
$saleShippingTrackingModel = new \App\Models\Sale\Shipping\SaleShippingTracking();
$update['content'] = json_encode($row['tracking']);
$update['remark'] = '';
$update['complete_status'] = 0;
if ($row['completed']) $update['complete_status'] = 1;
$update_result = $saleShippingTrackingModel->where('carrier_id', $carrier_id)->where('last_tracking_number', $row['tracking_no'])->update($update);
if ($update_result && $show_log) {
echo 'carrier_id '.$carrier_id.' '.$row['tracking_no'] . ' complete_status=>' . $update['complete_status'] . PHP_EOL;
}
}
/** 获取物流轨迹 ,对返回结果不做任何处理
* @return mixed
* $tracking_no 物流单号
*/
abstract function getLogisticsTrack($tracking_no);
/** 对获取到的物流轨迹结果做处理,筛选出必要的信息
* @param $result
* $tracking_no 物流单号
*/
abstract function getFormatLogisticsTrack($tracking_no): array;
/** 轨迹信息是否已经彻底获取完
* @param $final_tracking 最后一个轨迹信息
* @return bool
*/
abstract function boolComplete($final_tracking): bool;
}<?php
namespace App\Services\Sale\Shipping;
class WanGuoLogistics extends LogisticsAbstract
{
//说明文档:http://wgyl.itdida.com/itdida-api/swagger-ui.html
//批量查询:支持
private static $Username;
private static $Password;
private static $Authorize;
public function __construct()
{
$config = $this->getConfig('_wanGuo');
self::$Username = $config['_wanGuo_userName'];
self::$Password = $config['_wanGuo_password'];
self::$ApiUrl = 'http://wgyl.itdida.com/itdida-api/';
if (!self::$Authorize) $this->init();
}
private function init()
{
$client = new \GuzzleHttp\Client([
'base_uri' => self::$ApiUrl
]);
$requestUrl = 'login';
$result = $client->request('POST', $requestUrl, [
'query' => ['username' => self::$Username, 'password' => self::$Password]
]);
$resultObj = json_decode($result->getBody()->getContents());
if (property_exists($resultObj, 'success') && property_exists($resultObj, 'data') && $resultObj->success) {
self::$Authorize = $resultObj->data;
}
}
/** 获取物流轨迹 ,对返回结果不做任何处理
* @return mixed
* $tracking_no 物流单号
*/
public function getLogisticsTrack($tracking_no)
{
$client = new \GuzzleHttp\Client([
'base_uri' => self::$ApiUrl,
'timeout' => self::$TimeOut,
]);
$requestUrl = 'queryTracks';
$result = $client->request('GET', $requestUrl, [
'query' => ['no' => $tracking_no],
'headers' => [
'Accept' => 'text/plain',
'Authorization' => self::$Authorize
]
]);
return $result->getBody()->getContents();
}
/** 对获取到的物流轨迹结果做处理,筛选出必要的信息
* @param $result
* $tracking_no 物流单号
*/
public function getFormatLogisticsTrack($tracking_no): array
{
$result = $this->getLogisticsTrack($tracking_no);
$result = json_decode($result, true);
$filterResult = [];
if (isset($result['success']) && $result['success']) {
$tempResult = array_reverse($result['data'][0]['trackList']);
foreach ($tempResult as $key => $val) {
$filterResult['tracking_no'] = $tracking_no;
$filterResult['tracking'][] = ['time' => $val['time'], 'place' => $val['localtion'], 'content' => $val['desc']];
$filterResult['completed'] = $this->boolComplete($val, $result['data'][0]);
}
}
return $filterResult;
}
/** 轨迹信息是否已经彻底获取完
* @param $final_tracking 最后一个轨迹信息
* @return bool
*/
public function boolComplete($final_tracking, $tracking = array()): bool
{
if (isset($tracking['detail']['status']) && $tracking['detail']['status'] == 100) {
return true;
}
return false;
}
/** 批量查询物流单号的轨迹
* @param $tracking_no_arr
* @return array
*/
public function getMultiFormatLogisticsTrack($tracking_no_arr): array
{
$result = $this->getLogisticsTrack(implode(',', $tracking_no_arr));
$result = json_decode($result, true);
$filterResult = [];
if (isset($result['data']) && $result['data']) {
foreach ($result['data'] as $one) {
$tempResult = array_reverse($one['trackList']);
foreach ($tempResult as $key => $val) {
$filterResult[$one['no']]['tracking_no'] = $one['no'];
$filterResult[$one['no']]['tracking'][] = ['time' => $val['time'], 'place' => $val['localtion'], 'content' => $val['desc']];
$filterResult[$one['no']]['completed'] = $this->boolComplete($val, $one);
}
}
}
return $filterResult;
}
}