JuHeService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace services\extend\logistics;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\web\NotFoundHttpException;
  6. use common\forms\LogisticsForm;
  7. use linslin\yii2\curl\Curl;
  8. /**
  9. * 聚合物流查询
  10. *
  11. * Class JuHeService
  12. * @package services\extend\logistics
  13. * @author jianyan74 <751393839@qq.com>
  14. * @deprecated 接口已停用
  15. */
  16. class JuHeService
  17. {
  18. const URL = 'https://v.juhe.cn/exp';
  19. const STATUS_PENDING = 'PENDING';
  20. const STATUS_NO_RECORD = 'NO_RECORD';
  21. const STATUS_IN_TRANSIT = 'IN_TRANSIT';
  22. const STATUS_DELIVERING = 'DELIVERING';
  23. const STATUS_SIGNED = 'SIGNED';
  24. const STATUS_REJECTED = 'REJECTED';
  25. const STATUS_PROBLEM = 'PROBLEM';
  26. const STATUS_INVALID = 'INVALID';
  27. const STATUS_TIMEOUT = 'TIMEOUT';
  28. const STATUS_FAILED = 'FAILED';
  29. const STATUS_SEND_BACK = 'SEND_BACK';
  30. const STATUS_TAKING = 'TAKING';
  31. /**
  32. * @var string[]
  33. */
  34. public $statusMap = [
  35. self::STATUS_PENDING => '待查询',
  36. self::STATUS_NO_RECORD => '无记录',
  37. self::STATUS_IN_TRANSIT => '运输中',
  38. self::STATUS_DELIVERING => '派送中',
  39. self::STATUS_SIGNED => '已签收',
  40. self::STATUS_REJECTED => '拒签',
  41. self::STATUS_PROBLEM => '疑难件',
  42. self::STATUS_INVALID => '无效件',
  43. self::STATUS_TIMEOUT => '超时件',
  44. self::STATUS_FAILED => '派送失败',
  45. self::STATUS_SEND_BACK => '退回',
  46. self::STATUS_TAKING => '揽件',
  47. ];
  48. /**
  49. * @param string $no 例如: SF:123456
  50. * @param string $company 物流公司必填
  51. * @param number $senderPhone 发送者手机号码
  52. * @return LogisticsForm
  53. * @throws NotFoundHttpException
  54. */
  55. public function query($no, $company, $senderPhone)
  56. {
  57. $curl = new Curl();
  58. $request = $curl->setGetParams([
  59. 'key' => Yii::$app->services->config->backendConfig('logistics_juhe_app_code'),
  60. 'com' => $company, // 需要查询的快递公司编号
  61. 'no' => $no, // 需要查询的快递单号
  62. 'senderPhone' => '',
  63. 'receiverPhone' => ''
  64. ])->get(self::URL . '/index');
  65. $data = Json::decode($request);
  66. // 错误码
  67. if ($data['error_code'] != 0) {
  68. throw new NotFoundHttpException($data['reason']);
  69. }
  70. $form = new LogisticsForm();
  71. $form->no = $no;
  72. $form->company = $company;
  73. $form->code = $data['status'];
  74. $form->message = $data['msg'];
  75. $form->original = $data['result']['list'];
  76. if (!empty($form->original)) {
  77. foreach ($form->original as $item) {
  78. $form->list[] = [
  79. 'datetime' => $item['time'],
  80. 'remark' => $item['status'],
  81. 'zone' => '',
  82. ];
  83. }
  84. }
  85. return $form;
  86. }
  87. /**
  88. * 获取物流公司列表
  89. *
  90. * @return mixed|null
  91. * @throws \Exception
  92. */
  93. public function companies()
  94. {
  95. $curl = new Curl();
  96. $request = $curl->setGetParams([
  97. 'key' => Yii::$app->services->config->backendConfig('logistics_juhe_app_code')
  98. ])->get(self::URL . '/com');
  99. $data = Json::decode($request);
  100. // 错误码
  101. if ($data['error_code'] != 0) {
  102. throw new NotFoundHttpException($data['reason']);
  103. }
  104. return $data['result'];
  105. }
  106. }
粤ICP备19079148号