Query.Class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/9/7
  6. * Time: 11:33 AM
  7. */
  8. namespace Util\KuaiDi100;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Factory;
  12. class Query
  13. {
  14. // 基础域名
  15. private $baseUrl = '';
  16. // 公司编号
  17. private $customer = '';
  18. // 授权KEY
  19. private $key = '';
  20. // 实时查询物流信息缓存key
  21. private $pollQueryKey = 'kuaidi100PollQuery';
  22. public function __construct($customer='', $key = '')
  23. {
  24. $this->customer = $customer;
  25. $this->key = $key;
  26. }
  27. /**
  28. * 实时查询
  29. * 官方文档地址: https://api.kuaidi100.com/manager/page/document/synquery
  30. */
  31. public function pollQuery($params)
  32. {
  33. $postData = [
  34. 'customer' => $this->customer,
  35. 'param' => [
  36. 'com' => strtolower($params['com']), // 查询的快递公司的编码,一律用小写字母
  37. 'num' => $params['num'], // 查询的快递单号
  38. 'phone' => $params['phone'], // 收件人或寄件人的手机号或固话
  39. ],
  40. ];
  41. foreach ($postData['param'] as $key => $value){
  42. if(empty($value)){
  43. return ResultWrapper::fail($key.'参数为空', ErrorCode::$paramError);
  44. }
  45. }
  46. $postData['param'] = json_encode($postData['param']);
  47. $postData['sign'] = strtoupper(md5($postData['param'].$this->key.$postData['customer'])); // 签名
  48. $pollQueryData = Factory::cache('default')->get($this->pollQueryKey.':'.$params['num']);
  49. if (!empty($pollQueryData)) {
  50. return ResultWrapper::success($pollQueryData);
  51. }
  52. $response = request('https://poll.kuaidi100.com/poll/query.do', $postData, 30, true);
  53. if ($response['httpcode'] != 200) {
  54. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  55. }
  56. $responseData = json_decode($response['content'], true);
  57. if($responseData['message'] != 'ok'){
  58. return ResultWrapper::fail($responseData['message'], $responseData['returnCode']);
  59. }
  60. Factory::cache('default')->set($this->pollQueryKey . ':' . $params['num'], $response['content'], 3600);
  61. return ResultWrapper::success($response['content']);
  62. }
  63. // 快递单当前状态
  64. public function getState($state)
  65. {
  66. $satateData = [
  67. 0 => '在途',
  68. 1 => '揽收',
  69. 2 => '疑难',
  70. 3 => '签收',
  71. 4 => '退签',
  72. 5 => '派件',
  73. 6 => '退回',
  74. 7 => '转投',
  75. 10 => '待清关',
  76. 11 => '清关中',
  77. 12 => '已清关',
  78. 13 => '清关异常',
  79. 14 => '拒签'
  80. ];
  81. return isset($satateData[$state]) ? $satateData[$state] : '';
  82. }
  83. }