LogisticSFService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $responseBody = get_object_vars(simplexml_load_string($this->sendHttpToSF($head, $numbers, $checkWord, $url))->Body)['RouteResponse'];
  49. $result = [];
  50. if (is_array($responseBody)) {//SF返回多个单号的查询结果
  51. $result = $this->transformSFMoreToArr($responseBody, $result);
  52. } else {
  53. $result[] = $this->transformSFOneToArr(get_object_vars($responseBody), []);
  54. }
  55. return $result;
  56. }
  57. /**
  58. * 构建顺丰xml请求体
  59. * @param string $head
  60. * @param string $number
  61. * @return string
  62. */
  63. private function buildXmlStr(string $head, string $number): string
  64. {
  65. return <<<xml
  66. <?xml version="1.0" encoding="utf-8" ?>
  67. <Request service='RouteService' lang='zh-CN'>
  68. <Head>$head</Head>
  69. <Body>
  70. <RouteRequest
  71. tracking_number="{$number}" tracking_type='1' method_type='1'
  72. />
  73. </Body>
  74. </Request>
  75. xml;
  76. }
  77. /**
  78. * 将单个单号的顺丰数据转换为数组
  79. * @param array $routeResponse
  80. * @param array $data
  81. * @return array
  82. * @throws Exception
  83. */
  84. public function transformSFOneToArr(array $routeResponse, array $data): array
  85. {
  86. $data['logistic_number'] = $routeResponse['@attributes'][$this->protected_switch['logistic_number']];
  87. try {
  88. $lastRoute = get_object_vars($routeResponse['Route'][count($routeResponse['Route']) - 1])['@attributes'];//获取最新的路由信息
  89. $data = $this->switchOpCodeToStatus($lastRoute, $data);
  90. $data['transfer_status'] = $this->transformRoutes($routeResponse['Route']);
  91. } catch (Exception $e) {
  92. throw new WarningException("单号没有查询到快递路由信息','LogisticSFService->transformSFOneToArr->{$data['logistic_number']}");
  93. } finally {
  94. return $data;
  95. }
  96. }
  97. /**
  98. * 转换快递路由信息
  99. * @param array $routs 快递路由
  100. * @return array
  101. */
  102. public function transformRoutes(array $routs): array
  103. {
  104. $result = [];
  105. foreach ($routs as $route) {
  106. $route = get_object_vars($route)['@attributes'];
  107. $data['accept_time'] = $route['accept_time'];
  108. $data['accept_address'] = $route['accept_address'];
  109. $data['remark'] = $route['remark'];
  110. $result[] = $data;
  111. }
  112. return $result;
  113. }
  114. /**
  115. * 将最新路由信息转换为数组
  116. * @param array $lastRoute
  117. * @param array $data
  118. * @return array
  119. * @throws Exception
  120. */
  121. public function switchOpCodeToStatus(array $lastRoute, array $data): array
  122. {
  123. switch ($lastRoute['opcode']) {
  124. case 123:
  125. case 130:
  126. case 3036:
  127. case 31:
  128. case 30:
  129. $data['status'] = '在途';
  130. break;
  131. case 70:
  132. case 33:
  133. $data['status'] = '派送异常';
  134. $data['exception'] = '是';
  135. break;
  136. case 204:
  137. case 44:
  138. $data['status'] = '派送中';
  139. break;
  140. case 50:
  141. $data['status'] = '已揽收';
  142. break;
  143. case 607:
  144. case 8000:
  145. case 80:
  146. $data['status'] = '已收件';
  147. $data['received_at'] = $lastRoute[$this->protected_switch['received_at']];
  148. break;
  149. case 648:
  150. case 99:
  151. $data['status'] = '返回中';
  152. break;
  153. default:
  154. throw new WarningException("未知的丰桥状态码: {$lastRoute['opcode']}->{json_encode($lastRoute)}");
  155. }
  156. return $data;
  157. }
  158. /**
  159. * @param string $head
  160. * @param string $numbers
  161. * @param string $checkWord
  162. * @param string $url
  163. * @return Response
  164. * @throws Exception
  165. */
  166. public function sendHttpToSF(string $head, string $numbers, string $checkWord, string $url): Response
  167. {
  168. $xml = $this->buildXmlStr($head, $numbers);
  169. $checkingJson = $xml . $checkWord;
  170. $verifyCode = base64_encode(md5($checkingJson, true));
  171. try {
  172. $response = Http::withHeaders(['Content-Type' => 'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
  173. } catch (Exception $e) {
  174. throw new WarningException("HTTP请求顺丰接口异常->{$e->getMessage()}");
  175. }
  176. return $response;
  177. }
  178. /**
  179. * 将多个顺丰的单号转换为数组
  180. * @param array $responseBody
  181. * @param array $result
  182. * @return array
  183. * @throws Exception
  184. */
  185. public function transformSFMoreToArr(array $responseBody, array $result): array
  186. {
  187. foreach ($responseBody as $routeResponse) {
  188. $result[] = $this->transformSFOneToArr(get_object_vars($routeResponse), []);
  189. }
  190. return $result;
  191. }
  192. }