LogisticSFService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. try {
  124. switch ($lastRoute['opcode']) {
  125. case 123:
  126. case 130:
  127. case 3036:
  128. case 31:
  129. case 30:
  130. case 36:
  131. $data['status'] = '在途';
  132. break;
  133. case 70:
  134. case 33:
  135. $data['status'] = '派送异常';
  136. $data['exception'] = '是';
  137. break;
  138. case 204:
  139. case 44:
  140. $data['status'] = '派送中';
  141. break;
  142. case 50:
  143. $data['status'] = '已揽收';
  144. break;
  145. case 607:
  146. case 8000:
  147. case 80:
  148. $data['status'] = '已收件';
  149. $data['received_at'] = $lastRoute[$this->protected_switch['received_at']];
  150. break;
  151. case 648:
  152. case 99:
  153. $data['status'] = '返回中';
  154. break;
  155. default:
  156. throw new WarningException("未知的丰桥状态码: {$lastRoute['opcode']}->{json_encode($lastRoute)}");
  157. }
  158. } catch (WarningException $e) {
  159. $data['status'] = '其他异常';
  160. }finally {
  161. return $data;
  162. }
  163. }
  164. /**
  165. * @param string $head
  166. * @param string $numbers
  167. * @param string $checkWord
  168. * @param string $url
  169. * @return Response
  170. * @throws Exception
  171. */
  172. public function sendHttpToSF(string $head, string $numbers, string $checkWord, string $url): Response
  173. {
  174. $xml = $this->buildXmlStr($head, $numbers);
  175. $checkingJson = $xml . $checkWord;
  176. $verifyCode = base64_encode(md5($checkingJson, true));
  177. try {
  178. $response = Http::withHeaders(['Content-Type' => 'text/xml'])->get($url, ['xml' => $xml, 'verifyCode' => $verifyCode]);
  179. } catch (Exception $e) {
  180. throw new WarningException("HTTP请求顺丰接口异常->{$e->getMessage()}");
  181. }
  182. return $response;
  183. }
  184. /**
  185. * 将多个顺丰的单号转换为数组
  186. * @param array $responseBody
  187. * @param array $result
  188. * @return array
  189. * @throws Exception
  190. */
  191. public function transformSFMoreToArr(array $responseBody, array $result): array
  192. {
  193. foreach ($responseBody as $routeResponse) {
  194. $result[] = $this->transformSFOneToArr(get_object_vars($routeResponse), []);
  195. }
  196. return $result;
  197. }
  198. }