LogisticSFService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\WarningException;
  4. use App\OrderPackage;
  5. use Exception;
  6. use Illuminate\Http\Client\Response;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Http;
  9. class LogisticSFService
  10. {
  11. /**
  12. * 顺丰字段与数据库字段的映射关心
  13. * @var string[]
  14. */
  15. protected $protected_switch = [
  16. 'logistic_number' => 'mailno',
  17. 'transfer_status' => 'remark',
  18. 'received_at' => 'accept_time',
  19. ];
  20. /**
  21. * 获取顺丰快递揽收数据
  22. * @param array $logisticNums [logisticNums]快递单号数组
  23. * @return array 快递揽收信息数组
  24. * @throws Exception 未知的丰桥opCode
  25. */
  26. public function get(array $logisticNums): array
  27. {
  28. // 将$logisticNums以10个位单位进行分割,返回二维数组
  29. $logisticNums_size_10 = array_chunk($logisticNums, config('api_logistic.SF.max_size', 10));
  30. // 遍历二维数组批量查询顺丰接口
  31. //将查询到的结果整合到一起,更新order_packages.received_at
  32. $result = [];
  33. foreach ($logisticNums_size_10 as $numbers) {
  34. $numbersStr = implode(',', $numbers);
  35. $result_10 = $this->getResultFromSF(config('api_logistic.SF.head'), $numbersStr, config('api_logistic.SF.check_word'), config('api_logistic.SF.url'));
  36. $result = array_merge($result, $result_10);
  37. }
  38. return $result;
  39. }
  40. /**
  41. * @param string $head 客户号
  42. * @param string $numbers 快递单号字符串,多个以','分隔
  43. * @param string $checkWord 客户秘钥
  44. * @param string $url 顺丰接口地址
  45. * @return array
  46. * @throws Exception 未知的丰桥opCode
  47. */
  48. public function getResultFromSF(string $head, string $numbers, string $checkWord, string $url): array
  49. {
  50. try {
  51. $responseBody = get_object_vars(simplexml_load_string($this->sendHttpToSF($head, $numbers, $checkWord, $url))->Body)['RouteResponse'];
  52. } catch (Exception $e) {//index RouteResponse 异常处理
  53. return [];
  54. }
  55. $result = [];
  56. if (is_array($responseBody)) {//SF返回多个单号的查询结果
  57. $result = $this->transformSFMoreToArr($responseBody, $result);
  58. } else {
  59. $result[] = $this->transformSFOneToArr(get_object_vars($responseBody), []);
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 构建顺丰xml请求体
  65. * @param string $head
  66. * @param string $number
  67. * @return string
  68. */
  69. private function buildXmlStr(string $head, string $number): string
  70. {
  71. return <<<xml
  72. <?xml version="1.0" encoding="utf-8" ?>
  73. <Request service='RouteService' lang='zh-CN'>
  74. <Head>$head</Head>
  75. <Body>
  76. <RouteRequest
  77. tracking_number="{$number}" tracking_type='1' method_type='1'
  78. />
  79. </Body>
  80. </Request>
  81. xml;
  82. }
  83. /**
  84. * 将单个单号的顺丰数据转换为数组
  85. * @param array $routeResponse
  86. * @param array $data
  87. * @return array
  88. * @throws Exception
  89. */
  90. public function transformSFOneToArr(array $routeResponse, array $data): array
  91. {
  92. $data['logistic_number'] = $routeResponse['@attributes'][$this->protected_switch['logistic_number']];
  93. try {
  94. $lastRoute = get_object_vars($routeResponse['Route'][count($routeResponse['Route']) - 1])['@attributes'];//获取最新的路由信息
  95. $data = $this->switchOpCodeToStatus($lastRoute, $data);
  96. $data['transfer_status'] = $this->transformRoutes($routeResponse['Route']);
  97. } catch (Exception $e) {
  98. throw new WarningException("单号没有查询到快递路由信息','LogisticSFService->transformSFOneToArr->{$data['logistic_number']}");
  99. } finally {
  100. $data['routes_length'] = array_key_exists('transfer_status', $data) ? count($data['transfer_status']) : 0;
  101. return $data;
  102. }
  103. }
  104. /**
  105. * 转换快递路由信息
  106. * @param $routs 快递路由
  107. * @return array
  108. */
  109. public function transformRoutes($routs): array
  110. {
  111. $result = [];
  112. if (!is_array($routs)) {
  113. $routs = [$routs];
  114. }
  115. foreach ($routs as $route) {
  116. $route = get_object_vars($route)['@attributes'];
  117. $data['accept_time'] = $route['accept_time']??'';
  118. $data['accept_address'] = $route['accept_address']??'';
  119. $data['remark'] = $route['remark']??'';
  120. $result[] = $data;
  121. }
  122. return $result;
  123. }
  124. /**
  125. * 将最新路由信息转换为数组
  126. * @param array $lastRoute
  127. * @param array $data
  128. * @return array
  129. * @throws Exception
  130. */
  131. public function switchOpCodeToStatus(array $lastRoute, array $data): array
  132. {
  133. try {
  134. switch ($lastRoute['opcode']) {
  135. case 123:
  136. case 130:
  137. case 3036:
  138. case 31:
  139. case 30:
  140. case 36:
  141. case 106://航空板箱到达
  142. $data['status'] = '在途';
  143. break;
  144. case 33:
  145. $data['status'] = '派送异常';
  146. break;
  147. case 204:
  148. case 44:
  149. $data['status'] = '派送中';
  150. break;
  151. case 50:
  152. $data['status'] = '已揽收';
  153. break;
  154. case 607:
  155. case 8000:
  156. case 80:
  157. $data['status'] = '已签收';
  158. $data['received_at'] = $lastRoute[$this->protected_switch['received_at']];
  159. break;
  160. case 648:
  161. case 99:
  162. $data['status'] = '返回中';
  163. break;
  164. case 70:
  165. $data['status'] = '其他';
  166. break;
  167. default:
  168. $data['status'] = '其他';
  169. throw new WarningException("未知的丰桥状态码: {$lastRoute['opcode']}->{json_encode($lastRoute)}");
  170. }
  171. } catch (WarningException $e) {
  172. $data['status'] = '其他异常';
  173. } finally {
  174. return $data;
  175. }
  176. }
  177. /**
  178. * @param string $head
  179. * @param string $numbers
  180. * @param string $checkWord
  181. * @param string $url
  182. * @return Response
  183. * @throws Exception
  184. */
  185. public function sendHttpToSF(string $head, string $numbers, string $checkWord, string $url): Response
  186. {
  187. $xml = $this->buildXmlStr($head, $numbers);
  188. $checkingJson = $xml . $checkWord;
  189. $verifyCode = base64_encode(md5($checkingJson, true));
  190. try {
  191. $response = Http::withHeaders(['Content-Type' => 'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
  192. } catch (Exception $e) {
  193. throw new WarningException("HTTP请求顺丰接口异常->{$e->getMessage()}");
  194. }
  195. return $response;
  196. }
  197. /**
  198. * 将多个顺丰的单号转换为数组
  199. * @param array $responseBody
  200. * @param array $result
  201. * @return array
  202. * @throws Exception
  203. */
  204. public function transformSFMoreToArr(array $responseBody, array $result): array
  205. {
  206. foreach ($responseBody as $routeResponse) {
  207. $result[] = $this->transformSFOneToArr(get_object_vars($routeResponse), []);
  208. }
  209. return $result;
  210. }
  211. }