Skip to content

Commit

Permalink
优化接口返回信息和修改说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
uuk020 committed Jan 17, 2019
1 parent 700eb1b commit 6ab54e9
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 63 deletions.
68 changes: 60 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $logistics->query('12313131231', ['kuaidi100']);
[
'kuaidi100' => [
'channel' => 'kuaidi100',
"status" => 'success',
'status' => 'success',
'result' => [
[
'status' => 200,
Expand All @@ -44,6 +44,14 @@ $logistics->query('12313131231', ['kuaidi100']);
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
],
[
'status' => 201,
'message' => '快递公司参数异常:单号不存在或者已经过期',
'error_code' => 0,
'data' => '',
'logistics_company' => '',
'logistics_bill_no' => ''
]
]
]
Expand All @@ -61,7 +69,7 @@ $logistics->query('12313131231', ['ickd']);
[
'ickd' => [
'channel' => 'ickd',
"status" => 'success',
'status' => 'success',
'result' => [
[
'status' => 200,
Expand Down Expand Up @@ -91,16 +99,16 @@ $logistics->query('12313131231', ['baidu']);
[
'baidu' => [
'channel' => 'baidu',
"status" => 'success',
'status' => 'success',
'result' => [
[
'status' => 200,
'message' => 'OK',
'error_code' => 0,
'data' => [
['time' => '1545444420', 'desc' => '仓库-已签收'],
['time' => '1545441977', 'desc' => '广东XX服务点'],
['time' => '1545438199', 'desc' => '广东XX转运中心']
['time' => '2019-01-09 12:11', 'desc' => '仓库-已签收'],
['time' => '2019-01-09 12:11', 'desc' => '广东XX服务点'],
['time' => '2019-01-09 12:11', 'desc' => '广东XX转运中心']
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
Expand All @@ -112,7 +120,7 @@ $logistics->query('12313131231', ['baidu']);

## 多接口获取物流信息
```php
$logistics->query('12313131231'); // 只要一个接口请求成功, 停止请求下一个接口
$logistics->query('12313131231');
$logistics->query('12313131231', ['kuaidi100', 'ickd', 'baidu']);
```
示例:
Expand All @@ -121,7 +129,7 @@ $logistics->query('12313131231', ['kuaidi100', 'ickd', 'baidu']);
[
'kuaidi100' => [
'channel' => 'kuaidi100',
"status" => 'success',
'status' => 'success',
'result' => [
[
'status' => 200,
Expand All @@ -134,6 +142,50 @@ $logistics->query('12313131231', ['kuaidi100', 'ickd', 'baidu']);
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
],
[
'status' => 201,
'message' => '快递公司参数异常:单号不存在或者已经过期',
'error_code' => 0,
'data' => '',
'logistics_company' => '',
'logistics_bill_no' => ''
]
]
],
'ickd' => [
'channel' => 'ickd',
'status' => 'success',
'result' => [
[
'status' => 200,
'message' => 'OK',
'error_code' => 0,
'data' => [
['time' => '2019-01-09 12:11', 'context' => '仓库-已签收'],
['time' => '2019-01-07 12:11', 'context' => '广东XX服务点'],
['time' => '2019-01-06 12:11', 'context' => '广东XX转运中心']
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
]
]
],
'baidu' => [
'channel' => 'baidu',
'status' => 'success',
'result' => [
[
'status' => 200,
'message' => 'OK',
'error_code' => 0,
'data' => [
['time' => '2019-01-09 12:11', 'desc' => '仓库-已签收'],
['time' => '2019-01-09 12:11', 'desc' => '广东XX服务点'],
['time' => '2019-01-09 12:11', 'desc' => '广东XX转运中心']
],
'logistics_company' => '申通快递',
'logistics_bill_no' => '12312211'
]
]
]
Expand Down
23 changes: 20 additions & 3 deletions src/Channel/BaiduChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function get(string $code): array
'token' => '',
'_' => $rand[1]
];
$this->format($this->request($this->url, $urlParams));
$response = $this->request($this->url, $urlParams);
$this->format($this->toArray($response));
$this->response['logistics_bill_no'] = $code;
return $this->response;
} catch (\Exception $exception) {
Expand All @@ -53,12 +54,12 @@ public function get(string $code): array
}

/**
* 格式返回响应信息
* 转为数组
*
* @param $response
* @return void
*/
protected function format($response)
protected function toArray($response)
{
$pattern = '/(jQuery\d+_\d+\()({.*})\)$/i';
if (preg_match($pattern, $response, $match)) {
Expand All @@ -81,6 +82,22 @@ protected function format($response)
}
}

/**
* 统一物流信息
*
* @return mixed|void
*/
protected function format()
{
if (!empty($this->response['data']) && is_array($this->response['data'])) {
$formatData = [];
foreach ($this->response['data'] as $datum) {
$formatData[] = ['time' => date('Y-m-d H:i:s', $datum['time']), 'description' => $datum['desc']];
}
$this->response['data'] = $formatData;
}
}

/**
* 生成请求随机字符串数组
*
Expand Down
12 changes: 10 additions & 2 deletions src/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class Channel
protected $url;

protected $response;

/**
* 调用查询接口
*
Expand All @@ -27,10 +28,17 @@ abstract class Channel
abstract public function get(string $code):array ;

/**
* 格式响应信息
* 转换为数组
*
* @param string|array $response
* @return void
*/
abstract protected function format($response);
abstract protected function toArray($response);

/**
* 格式物流信息
*
* @return mixed
*/
abstract protected function format();
}
23 changes: 20 additions & 3 deletions src/Channel/IckdChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ public function get(string $code): array
'callback' => '_jqjsp',
'_'.time()
];
$this->format($this->request($this->url.$code, $urlParams, 0, ['referer: https://biz.trace.ickd.cn']));
$response = $this->request($this->url.$code, $urlParams, 0, ['referer: https://biz.trace.ickd.cn']);
$this->format($this->toArray($response));
return $this->response;
} catch (\Exception $exception) {
throw new HttpException($exception->getMessage());
}
}

/**
* 格式化请求返回信息
* 转为数组
*
* @param array|string $response
*/
protected function format($response)
protected function toArray($response)
{
$pattern = '/(\_jqjsp\()({.*})\)/i';
if (preg_match($pattern, $response, $match)) {
Expand All @@ -88,4 +89,20 @@ protected function format($response)
];
}
}

/**
* 统一物流信息
*
* @return mixed|void
*/
protected function format()
{
if (!empty($this->response['data']) && is_array($this->response['data'])) {
$formatData = [];
foreach ($this->response['data'] as $datum) {
$formatData[] = ['time' => $datum['time'], 'description' => $datum['context']];
}
$this->response['data'] = $formatData;
}
}
}
26 changes: 23 additions & 3 deletions src/Channel/Kuaidi100Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function get(string $code): array
$urlParams[] = ['type' => $companyCode, 'postid' => $code];
$urls[] = $this->url;
}
$this->format($this->requestWithUrls($urls, $urlParams));
$response = $this->requestWithUrls($urls, $urlParams);
$this->format($this->toArray($response));
return $this->response;
} catch (\Exception $exception) {
throw new HttpException($exception->getMessage());
Expand All @@ -69,11 +70,11 @@ protected function getCompanyCode(string $code): array
}

/**
* 格式响应数据
* 转为数组
*
* @param array|string $response
*/
protected function format($response)
protected function toArray($response)
{
foreach ($response as $item) {
$data = \json_decode($item['result'], true);
Expand All @@ -87,4 +88,23 @@ protected function format($response)
];
}
}

/**
* 统一物流信息
*
* @return mixed|void
*/
protected function format()
{
$formatData = [];
foreach ($this->response as &$item) {
if (!empty($item['data']) && is_array($item['data'])) {
foreach ($item['data'] as $datum) {
$formatData[] = ['time' => $datum['time'], 'description' => $datum['context']];
}
$item['data'] = $formatData;
}
}
unset($item);
}
}
7 changes: 2 additions & 5 deletions src/Logistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function __construct()
public function query(string $code, $channels = ['kuaidi100']): array
{
$results = [];
$isSuccessful = false;
if (empty($code)) {
throw new InvalidArgumentException('code arguments cannot empty.');
}
Expand All @@ -47,18 +46,16 @@ public function query(string $code, $channels = ['kuaidi100']): array
'status' => self::SUCCESS,
'result' => $this->factory->createChannel($channel)->get($code),
];
$isSuccessful = true;
break;
} catch (\Exception $exception) {
$results[$channel] = [
'channel' => $channel,
'status' => self::FAILURE,
'exception' => $exception->getMessage(),
];

}
}
if (!$isSuccessful) {
$collectionOfException = array_column($results, 'exception');
if (count($collectionOfException) === 3) {
throw new NoQueryAvailableException('sorry! no channel class available');
}
return $results;
Expand Down
Loading

0 comments on commit 6ab54e9

Please sign in to comment.