ALiYunService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace services\extend\logistics;
  3. use Yii;
  4. use linslin\yii2\curl\Curl;
  5. use common\forms\LogisticsForm;
  6. use yii\helpers\Json;
  7. use yii\web\NotFoundHttpException;
  8. /**
  9. * 阿里云物流查询
  10. *
  11. * Class ALiYunService
  12. * @package services\extend\logistics
  13. * @doc https://market.aliyun.com/products/57126001/cmapi021863.html
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class ALiYunService
  17. {
  18. const URL = 'https://wuliu.market.alicloudapi.com';
  19. const SUCCESS_STATUS = 0;
  20. const STATUS_ERROR = -1;
  21. const STATUS_COURIER_RECEIPT = 0;
  22. const STATUS_ON_THE_WAY = 1;
  23. const STATUS_SENDING_A_PIECE = 2;
  24. const STATUS_SIGNED = 3;
  25. const STATUS_DELIVERY_FAILED = 4;
  26. const STATUS_TROUBLESOME = 5;
  27. const STATUS_RETURN_RECEIPT = 6;
  28. /**
  29. * @var string[]
  30. */
  31. public $statusMap = [
  32. self::STATUS_ERROR => '异常',
  33. self::STATUS_COURIER_RECEIPT => '快递收件(揽件)',
  34. self::STATUS_ON_THE_WAY => '在途中',
  35. self::STATUS_SENDING_A_PIECE => '正在派件',
  36. self::STATUS_SIGNED => '已签收',
  37. self::STATUS_DELIVERY_FAILED => '派送失败',
  38. self::STATUS_TROUBLESOME => '疑难件',
  39. self::STATUS_RETURN_RECEIPT => '退件签收',
  40. ];
  41. /**
  42. * @param string $no 例如: SF:123456:2563
  43. * @param string $company
  44. * @param null $customerName 占位
  45. * @return LogisticsForm
  46. * @throws NotFoundHttpException
  47. */
  48. public function query($no, $company = null, $customerName = null)
  49. {
  50. $curl = new Curl();
  51. $request = $curl->setHeaders([
  52. 'Authorization' => 'APPCODE ' . Yii::$app->services->config->backendConfig('logistics_aliyun_app_code')
  53. ])->setGetParams([
  54. 'no' => $no,
  55. 'type' => $company,
  56. ])->get(self::URL . '/kdi');
  57. $data = Json::decode($request);
  58. // 错误码
  59. if ($data['status'] != 0) {
  60. throw new NotFoundHttpException($data['msg']);
  61. }
  62. $form = new LogisticsForm();
  63. $form->no = $no;
  64. $form->company = $company;
  65. $form->code = $data['status'];
  66. $form->message = $data['msg'];
  67. $form->original = $data['result']['list'];
  68. if (!empty($form->original)) {
  69. foreach ($form->original as $item) {
  70. $form->list[] = [
  71. 'datetime' => $item['time'],
  72. 'remark' => $item['status'],
  73. 'zone' => '',
  74. ];
  75. }
  76. }
  77. return $form;
  78. }
  79. /**
  80. * 获取物流公司列表
  81. *
  82. * @return mixed|null
  83. * @throws \Exception
  84. */
  85. public function companies()
  86. {
  87. $curl = new Curl();
  88. $request = $curl->setHeaders([
  89. 'Authorization' => 'APPCODE ' . Yii::$app->services->config->backendConfig('logistics_aliyun_app_code')
  90. ])->get(self::URL . '/getExpressList');
  91. $data = Json::decode($request);
  92. // 错误码
  93. if ($data['status'] != 200) {
  94. throw new NotFoundHttpException($data['msg']);
  95. }
  96. return $data['result'];
  97. }
  98. }
粤ICP备19079148号