| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Services;
- use App\OrderPackage;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Http;
- class LogisticZopService implements LogisticRouteInterface
- {
- public function query($logistic_number)
- {
- $url = config('api_logistic.ZTO.url');
- $xAppKey = config('api_logistic.ZTO.x-appKey');
- $appSecret = config('api_logistic.ZTO.appSecret');
- $body = json_encode([
- 'billCode' => $logistic_number,
- ], JSON_UNESCAPED_UNICODE);
- $data_digest = base64_encode(md5($body . $appSecret, TRUE));
- $headers = [
- 'Content-Type' => 'application/json; charset=UTF-8',
- 'x-companyid' => $xAppKey,
- 'x-datadigest' => $data_digest,
- ];
- $response = Http::withHeaders($headers)->withBody($body, 'application/json')->post($url);
- return json_decode($response->body());
- }
- public function format($nativeResponse)
- {
- if (is_null($nativeResponse)) {
- return [];
- }
- $nativeRoutes = $nativeResponse->result;
- // $nativeMessages = $nativeResponse->messages;
- // $nativeStatus = $nativeResponse->status;
- // $nativeStatusCode = $nativeResponse->statusCode;
- $this->logistic_number = $nativeRoutes[0]->billCode;
- $order_package = OrderPackage::query()->where('logistic_number', $this->logistic_number)->first();
- $lastRoute = $nativeRoutes[count($nativeRoutes) - 1];
- $result = [
- 'logistic_number' => $this->logistic_number,
- 'exception_type' => $order_package->exception_type,
- 'exception'=>$order_package->exception,
- 'status' => $order_package->status,
- 'transfer_status' => $order_package->transfer_status,
- 'received_at' =>$order_package->received_at,
- ];
- list($status, $received_at) = $this->getNormalStatusAndReceivedAt($lastRoute);
- $result['status'] = $status;
- $result['received_at'] = $received_at;
- $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
- $result['routes_length'] = count($result['transfer_status']);
- $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
- $lastRouteDate = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
- $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastRouteDate);
- $result['exception_type'] = $exceptionData['exception_type'];
- $result['exception'] = $exceptionData['exception'];
- return $result;
- }
- /**
- * 正常的状态与签收时间
- */
- private function getNormalStatusAndReceivedAt($lastRoute): array
- {
- $status = null;
- $received_at = null;
- switch ($lastRoute->scanType) {
- case '收件':
- $status = '已揽收';
- break;
- case '到件':
- case '发件':
- $status = '在途';
- break;
- case 'ARRIVAL':
- case '派件':
- $status = '派送中';
- break;
- case 'SIGNED':
- case '签收':
- $status = '已收件';
- $received_at = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
- break;
- default:
- $status = '无';
- break;
- }
- return array($status, $received_at);
- }
- /**
- * @param $nativeRoutes
- * @return array
- */
- private function getTransferStatus($nativeRoutes): array
- {
- $transfer_status = [];
- foreach ($nativeRoutes as $item) {
- $data = [];
- $data['accept_time'] = Carbon::parse($item->scanDate / 1000)->addHours(8)->toDateTimeString();
- $scanSite = $item->scanSite;
- $data['accept_address'] = $scanSite->prov . '-' . $scanSite->name;
- $data['remark'] = $item->desc;
- $transfer_status[] = $data;
- }
- return $transfer_status;
- }
- }
|