|
|
@@ -8,68 +8,76 @@ use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
class LogisticQiaoSFService
|
|
|
{
|
|
|
- public function get($logisticNums)
|
|
|
+ /**
|
|
|
+ * 顺丰字段与数据库字段的映射关心
|
|
|
+ * @var string[]
|
|
|
+ */
|
|
|
+ protected $protected_switch = [
|
|
|
+ 'logistic_number' => 'mailno',
|
|
|
+ 'transfer_status' => 'remark',
|
|
|
+ 'received_at' => 'accept_time',
|
|
|
+ ];
|
|
|
+ /**
|
|
|
+ * 获取顺丰快递揽收数据
|
|
|
+ * @param $logisticNums [logisticNums]快递单号数组
|
|
|
+ * @return array 快递揽收信息数组
|
|
|
+ */
|
|
|
+ public function get($logisticNums): array
|
|
|
{
|
|
|
$url = config('api_logistic.SF.url');
|
|
|
$head = config('api_logistic.SF.head');
|
|
|
$checkWord = config('api_logistic.SF.checkWord');
|
|
|
- $maxSize = config('api_logistic.SF.maxSize');
|
|
|
+ $maxSize = config('api_logistic.SF.maxSize',10);
|
|
|
|
|
|
// 将$logisticNums以10个位单位进行分割,返回二维数组
|
|
|
- $logisticNums_size_10 = array_chunk($logisticNums, $maxSize, true);
|
|
|
+ $logisticNums_size_10 = array_chunk($logisticNums, $maxSize);
|
|
|
|
|
|
// 遍历二维数组批量查询顺丰接口
|
|
|
//将查询到的结果整合到一起,更新order_packages.received_at
|
|
|
$result = [];
|
|
|
foreach ($logisticNums_size_10 as $numbers) {
|
|
|
- $numbers = implode(',', $logisticNums);
|
|
|
- $result_10 = $this->getResultFromSF($head, $numbers, $checkWord, $url);
|
|
|
- $result = array_merge($result_10);
|
|
|
+ $numbersStr = implode(',', $numbers);
|
|
|
+ $result_10 = $this->getResultFromSF($head, $numbersStr, $checkWord, $url);
|
|
|
+ $result = array_merge($result, $result_10);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param string $head
|
|
|
- * @param string $number
|
|
|
- * @param string $checkWord
|
|
|
- * @param string $url
|
|
|
+ * @param string $head 客户号
|
|
|
+ * @param string $numbers 快递单号字符串,多个以','分隔
|
|
|
+ * @param string $checkWord 客户秘钥
|
|
|
+ * @param string $url 顺丰接口地址
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getResultFromSF(string $head, string $number, string $checkWord, string $url): array
|
|
|
+ public function getResultFromSF(string $head, string $numbers, string $checkWord, string $url): array
|
|
|
{
|
|
|
- $xml = $this->bulidXmlStr($head, $number);
|
|
|
+ $xml = $this->buildXmlStr($head, $numbers);
|
|
|
$checkingJson = $xml . $checkWord;
|
|
|
$verifyCode = base64_encode(md5($checkingJson, true));
|
|
|
$response = Http::withHeaders(['Content-Type' => 'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
|
|
|
+
|
|
|
$routeResponses = get_object_vars(simplexml_load_string($response)->Body)['RouteResponse'];
|
|
|
$result = [];
|
|
|
- foreach ($routeResponses as $routeResponse) {
|
|
|
- $routeResponse = get_object_vars($routeResponse);
|
|
|
- $data['mailno'] = $routeResponse['@attributes']['mailno'];
|
|
|
- $data['Route'] = [];
|
|
|
- $routes = $routeResponse['Route'];
|
|
|
- foreach ($routes as $route) {
|
|
|
- $route = get_object_vars($route)['@attributes'];
|
|
|
- if ($route['opcode'] == 50) {
|
|
|
- $data['Route']['transfer_status'] = $route['remark'];
|
|
|
- $data['Route']['status'] = '已揽收';
|
|
|
- $data['Route']['received_at'] = $route['accept_time'];
|
|
|
- }
|
|
|
+ if (is_array($routeResponses)) {
|
|
|
+ foreach ($routeResponses as $routeResponse) {
|
|
|
+ $result[] = $this->buildData(get_object_vars($routeResponse), []);
|
|
|
}
|
|
|
- $result[] = $data;
|
|
|
+ } else {
|
|
|
+ $result[] = $this->buildData(get_object_vars($routeResponses), []);
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 构建顺丰xml请求体
|
|
|
* @param string $head
|
|
|
* @param string $number
|
|
|
* @return string
|
|
|
*/
|
|
|
- public function bulidXmlStr(string $head, string $number): string
|
|
|
+ public function buildXmlStr(string $head, string $number): string
|
|
|
{
|
|
|
- $xml = <<<xml
|
|
|
+ return <<<xml
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
<Request service='RouteService' lang='zh-CN'>
|
|
|
<Head>$head</Head>
|
|
|
@@ -80,6 +88,26 @@ class LogisticQiaoSFService
|
|
|
</Body>
|
|
|
</Request>
|
|
|
xml;
|
|
|
- return $xml;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将顺丰的数据转换为数组
|
|
|
+ * @param array $routeResponse
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function buildData(array $routeResponse, array $data): array
|
|
|
+ {
|
|
|
+ $data['logistic_number'] = $routeResponse['@attributes'][$this->protected_switch['logistic_number']];
|
|
|
+ foreach ($routeResponse['Route'] as $route) {
|
|
|
+ $route = get_object_vars($route)['@attributes'];
|
|
|
+ if ($route['opcode'] == 50) {
|
|
|
+ $data['status'] = '已揽收';
|
|
|
+ $data['transfer_status'] = $route[$this->protected_switch['transfer_status']];
|
|
|
+ $data['received_at'] = $route[$this->protected_switch['received_at']];
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
}
|
|
|
}
|