ExpressServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * 物流100
  4. */
  5. namespace app\api\services;
  6. use think\facade\Db;
  7. /**
  8. * Class ExpressServices
  9. * @package app\services
  10. * @method $this data($data)
  11. * @method $this orderId(int $orderId)
  12. * @method $this code(string $code)
  13. * @method $this number(string $number)
  14. */
  15. class ExpressServices
  16. {
  17. /**
  18. * 数据体
  19. * @var
  20. */
  21. protected $data;
  22. /**
  23. * 订单ID
  24. * @var
  25. */
  26. protected $orderId;
  27. /**
  28. * 物流编码
  29. * @var
  30. */
  31. protected $code;
  32. /**
  33. * 物流单号
  34. * @var
  35. */
  36. protected $number;
  37. private $expire = 7200;
  38. private $key = 'XmHZSjUU7854';
  39. private $customer = '7B02894E9F8FF33CC1071AF87DC56B6D';
  40. private $url = 'http://poll.kuaidi100.com/poll/query.do';
  41. protected static $instance = null;
  42. public static function instance ()
  43. {
  44. if (is_null(self::$instance)) {
  45. self::$instance = new static();
  46. }
  47. return self::$instance;
  48. }
  49. public function find ()
  50. {
  51. try {
  52. $delivery = Db::name('shop_order_done_delivery')->where(['order_id' => $this->orderId])->value('details');
  53. if ($delivery) {
  54. return json_decode($delivery, true);
  55. }
  56. if (($data = \services\CacheServices::get(md5($this->orderId . $this->number))) === false || empty($data)) {
  57. $param = array(
  58. 'com' => $this->code, //快递公司编码
  59. 'num' => $this->number, //快递单号
  60. 'phone' => '', //手机号
  61. 'from' => '', //出发地城市
  62. 'to' => '', //目的地城市
  63. 'resultv2' => '1' //开启行政区域解析
  64. );
  65. //请求参数
  66. $post_data = array();
  67. $post_data["customer"] = $this->customer;
  68. $post_data["param"] = json_encode($param);
  69. $sign = md5($post_data["param"] . $this->key . $post_data["customer"]);
  70. $post_data["sign"] = strtoupper($sign);
  71. $params = "";
  72. foreach ($post_data as $k => $v) {
  73. $params .= "$k=" . urlencode($v) . "&"; //默认UTF-8编码格式
  74. }
  75. $post_data = substr($params, 0, -1);
  76. //发送post请求
  77. $ch = curl_init();
  78. curl_setopt($ch, CURLOPT_POST, 1);
  79. curl_setopt($ch, CURLOPT_HEADER, 0);
  80. curl_setopt($ch, CURLOPT_URL, $this->url);
  81. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  83. $result = curl_exec($ch);
  84. $data = json_decode($result, true)['data'];
  85. if (empty($data)) {
  86. return [];
  87. }
  88. if ($data[0]['status'] == '签收') {
  89. $delivery = Db::name('shop_order_done_delivery')->where(['order_id' => $this->orderId])->value('id');
  90. if (empty($delivery)) {
  91. Db::name('shop_order_done_delivery')->insert(['order_id' => $this->orderId, 'code' => $this->code, 'number' => $this->number, 'details' => json_encode($data, JSON_UNESCAPED_UNICODE)]);
  92. } else {
  93. Db::name('shop_order_done_delivery')->where(['id' => $delivery])->update(['order_id' => $this->orderId, 'code' => $this->code, 'number' => $this->number, 'details' => json_encode($data, JSON_UNESCAPED_UNICODE)]);
  94. }
  95. }
  96. \services\CacheServices::set(md5($this->orderId . $this->number), $data, $this->expire);
  97. }
  98. return $data;
  99. } catch (\Exception $e) {
  100. return false;
  101. }
  102. }
  103. /**
  104. * 魔术方法
  105. * @param $name
  106. * @param $arguments
  107. * @return $this
  108. */
  109. public function __call ($name, $arguments)
  110. {
  111. // TODO: Implement __call() method.
  112. if ($name == 'data') {
  113. $this->{$name} = $arguments[0];
  114. } else {
  115. $this->{$name} = $arguments[0];
  116. }
  117. return $this;
  118. }
  119. }