|
|
@@ -0,0 +1,124 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Traits\ServiceAppAop;
|
|
|
+use App\LogisticAliJiSuApi;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
|
+
|
|
|
+class LogisticAliJiSuApiService
|
|
|
+{
|
|
|
+ use ServiceAppAop;
|
|
|
+
|
|
|
+ public function query($logistic_number)
|
|
|
+ {
|
|
|
+ $app_code = config('api_logistic.AliJiSu.prod.app-code');
|
|
|
+ $type = config('api_logistic.AliJiSu.prod.type');
|
|
|
+ $host = config('api_logistic.AliJiSu.prod.search.host');
|
|
|
+ $path = config('api_logistic.AliJiSu.prod.search.path');
|
|
|
+ $method = config('api_logistic.AliJiSu.prod.method');
|
|
|
+ $headers = array();
|
|
|
+ array_push($headers, "Authorization:APPCODE " . $app_code);
|
|
|
+ array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
|
|
|
+ $query='number='.$logistic_number.'&type='.$type;
|
|
|
+ $bodys = "null";
|
|
|
+ $url = $host . $path . "?" . $query;
|
|
|
+// $response = Http::withHeaders($headers)->get($url);
|
|
|
+// return json_decode($response->body());
|
|
|
+
|
|
|
+ $curl = curl_init();
|
|
|
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
+ curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
+ curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
|
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
+// curl_setopt($curl, CURLOPT_HEADER, true);
|
|
|
+ if (1 == strpos("$".$host, "https://"))
|
|
|
+ {
|
|
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+ }
|
|
|
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
|
|
|
+ return json_decode(curl_exec($curl));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function format($response): array
|
|
|
+ {
|
|
|
+ $result = [];
|
|
|
+ if (!isset($response)) {return [];}
|
|
|
+ else {
|
|
|
+ try {
|
|
|
+ if ($response->result->number??false)$result['logistic_number'] = $response->result->number;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ LogService::log(LogisticYTOService::class, "AliJiSu快递信息异常", $response);
|
|
|
+ }
|
|
|
+ $list=$response->result->list;
|
|
|
+ if (!empty($list) && is_array($list)) {
|
|
|
+ $lastNativeRoute = $list[0];
|
|
|
+ $result['status'] = $this->getStatus($response);
|
|
|
+ if ($result['status'] == '已收件') $result['received_at'] = $lastNativeRoute->time;
|
|
|
+ $result['transfer_status'] = $this->getTransferStatus($list);
|
|
|
+ $result['routes_length'] = array_key_exists('transfer_status', $result) ? count($result['transfer_status']) : 0;
|
|
|
+ $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
|
|
|
+ $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastNativeRoute ? $lastNativeRoute->time : null);
|
|
|
+ $result['exception_type'] = $exceptionData['exception_type'];
|
|
|
+ $result['exception'] = $exceptionData['exception'];
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $result['status'] = null;
|
|
|
+ $result['transfer_status'] = [];
|
|
|
+ }
|
|
|
+ if (!array_key_exists('status', $result)) {$result['status'] = null;$result['transfer_status'] = [];}
|
|
|
+ //如果没有发现额外的异常,且查询到物流轨迹,将异常置为无
|
|
|
+ if (!array_key_exists('exception', $result)
|
|
|
+ && !array_key_exists('exception_type', $result)
|
|
|
+ && array_key_exists('transfer_status', $result)
|
|
|
+ ) {$result['exception_type'] = '无';$result['exception'] = '否';}
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param $nativeData
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function getStatus($nativeData): string
|
|
|
+ {
|
|
|
+ $status = null;
|
|
|
+ switch ($nativeData->result->deliverystatus) {
|
|
|
+ case '1':
|
|
|
+ $status = '在途';
|
|
|
+ break;
|
|
|
+ case '2':
|
|
|
+ $status = '派送中';
|
|
|
+ break;
|
|
|
+ case '3':
|
|
|
+ $status = '已收件';
|
|
|
+ break;
|
|
|
+ case '4':
|
|
|
+ $status = '派送异常';
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $status = '无';
|
|
|
+ }
|
|
|
+ return $status;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param $nativeRoutes
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function getTransferStatus($nativeRoutes): array
|
|
|
+ {
|
|
|
+ $transferStatus = [];
|
|
|
+ foreach ($nativeRoutes as $nativeRoute) {
|
|
|
+ $item = [];
|
|
|
+ $item['accept_time'] = $nativeRoute->time;
|
|
|
+ $item['accept_address'] = $nativeRoute->status;
|
|
|
+ $item['remark'] = "";
|
|
|
+ $transferStatus[] = $item;
|
|
|
+ }
|
|
|
+ return $transferStatus;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|