LogisticSFService.php 7.1 KB

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