Kd100Service.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace services\extend\logistics;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\web\NotFoundHttpException;
  6. use common\components\Service;
  7. use common\forms\LogisticsForm;
  8. use linslin\yii2\curl\Curl;
  9. /**
  10. * Class Kd100Service
  11. * @package services\extend\logistics
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class Kd100Service extends Service
  15. {
  16. const SUCCESS_STATUS = 200;
  17. const STATUS_ON_THE_WAY = 0;
  18. const STATUS_PACKAGE = 1;
  19. const STATUS_DIFFICULT = 2;
  20. const STATUS_SIGNING = 3;
  21. const STATUS_REFUND = 4;
  22. const STATUS_PIECE = 5;
  23. const STATUS_RETURN = 6;
  24. const RETURN_TO_BE_CLEARED = 10;
  25. const STATUS_CLEARANCE = 11;
  26. const STATUS_CLEARED = 12;
  27. const STATUS_CUSTOMS_CLEARANCE_ABNORMALITY = 13;
  28. const STATUS_RECIPIENT_REFUSAL = 14;
  29. /**
  30. * @var string[]
  31. */
  32. public $statusMap = [
  33. self::STATUS_PACKAGE => '揽件',
  34. self::STATUS_DIFFICULT => '疑难',
  35. self::STATUS_SIGNING => '签收',
  36. self::STATUS_REFUND => '退签',
  37. self::STATUS_PIECE => '派件',
  38. self::STATUS_RETURN => '退回',
  39. self::RETURN_TO_BE_CLEARED => '待清关',
  40. self::STATUS_CLEARANCE => '清关中',
  41. self::STATUS_CLEARED => '已清关',
  42. self::STATUS_CUSTOMS_CLEARANCE_ABNORMALITY => '清关异常',
  43. self::STATUS_RECIPIENT_REFUSAL => '收件人拒签',
  44. ];
  45. /**
  46. * @param $no
  47. * @param $company
  48. * @param $phone
  49. * @return LogisticsForm
  50. * @throws NotFoundHttpException
  51. */
  52. public function query($no, $company = null, $phone = null)
  53. {
  54. $key = Yii::$app->services->config->backendConfig('logistics_kd100_app_key');
  55. $customer = Yii::$app->services->config->backendConfig('logistics_kd100_app_id');
  56. $customer = strtolower($customer);
  57. // 无物流公司,自动匹配物流
  58. if (empty($company)) {
  59. $companies = $this->autoCompanies($key, $no);
  60. $company = current($companies)['comCode'];
  61. }
  62. $param = [
  63. 'customer' => $customer,
  64. 'com' => $company,
  65. 'num' => $no,
  66. 'phone' => $phone, // 收、寄件人的电话号码(手机和固定电话均可,只能填写一个,顺丰速运和丰网速运必填,其他快递公司选填。如座机号码有分机号,分机号无需传入。)
  67. ];
  68. $param = Json::encode($param);
  69. $curl = new Curl();
  70. $request = $curl->setHeaders([
  71. 'Content-Type' => 'application/x-www-form-urlencoded'
  72. ])->setPostParams([
  73. 'param' => $param,
  74. 'customer' => $customer,
  75. 'sign' => $this->generateSign($param, $key, $customer),
  76. ])->post('http://poll.kuaidi100.com/poll/query.do');
  77. $data = Json::decode($request);
  78. // 错误码
  79. if (isset($data['returnCode']) && 200 != $data['returnCode']) {
  80. throw new NotFoundHttpException($data['message']);
  81. }
  82. $form = new LogisticsForm();
  83. $form->no = $no;
  84. $form->company = $company;
  85. $form->code = $data['status'];
  86. $form->message = $data['message'];
  87. $form->original = $data['data'];
  88. if (!empty($form->original)) {
  89. foreach ($form->original as $item) {
  90. $form->list[] = [
  91. 'datetime' => $item['time'],
  92. 'remark' => $item['context'],
  93. 'zone' => '',
  94. ];
  95. }
  96. }
  97. return $form;
  98. }
  99. /**
  100. * 获取物流公司列表
  101. *
  102. * @return mixed|null
  103. * @throws \Exception
  104. */
  105. public function companies()
  106. {
  107. return [];
  108. }
  109. /**
  110. * 自动匹配单号
  111. *
  112. * @param $no
  113. * @return mixed|null
  114. * @throws NotFoundHttpException
  115. */
  116. public function autoCompanies($key, $no)
  117. {
  118. $curl = new Curl();
  119. $request = $curl->setHeaders([
  120. 'Content-Type' => 'application/x-www-form-urlencoded'
  121. ])->setGetParams([
  122. 'key' => $key,
  123. 'num' => $no,
  124. ])->get('http://www.kuaidi100.com/autonumber/auto');
  125. $data = Json::decode($request);
  126. // 错误码
  127. if (isset($data['returnCode']) && self::SUCCESS_STATUS != $data['returnCode']) {
  128. throw new NotFoundHttpException($data['message']);
  129. }
  130. if (empty($data)) {
  131. throw new NotFoundHttpException('未查询到该订单信息!');
  132. }
  133. return $data;
  134. }
  135. /**
  136. * @param $param
  137. * @param $key
  138. * @param $customer
  139. *
  140. * @return string
  141. */
  142. protected function generateSign($param, $key, $customer)
  143. {
  144. return strtoupper(md5($param . $key . $customer));
  145. }
  146. }
粤ICP备19079148号