| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * 物流100
- */
- namespace app\api\services;
- use think\facade\Db;
- /**
- * Class ExpressServices
- * @package app\services
- * @method $this data($data)
- * @method $this orderId(int $orderId)
- * @method $this code(string $code)
- * @method $this number(string $number)
- */
- class ExpressServices
- {
- /**
- * 数据体
- * @var
- */
- protected $data;
- /**
- * 订单ID
- * @var
- */
- protected $orderId;
- /**
- * 物流编码
- * @var
- */
- protected $code;
- /**
- * 物流单号
- * @var
- */
- protected $number;
- private $expire = 7200;
- private $key = 'XmHZSjUU7854';
- private $customer = '7B02894E9F8FF33CC1071AF87DC56B6D';
- private $url = 'http://poll.kuaidi100.com/poll/query.do';
- protected static $instance = null;
- public static function instance ()
- {
- if (is_null(self::$instance)) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- public function find ()
- {
- try {
- $delivery = Db::name('shop_order_done_delivery')->where(['order_id' => $this->orderId])->value('details');
- if ($delivery) {
- return json_decode($delivery, true);
- }
- if (($data = \services\CacheServices::get(md5($this->orderId . $this->number))) === false || empty($data)) {
- $param = array(
- 'com' => $this->code, //快递公司编码
- 'num' => $this->number, //快递单号
- 'phone' => '', //手机号
- 'from' => '', //出发地城市
- 'to' => '', //目的地城市
- 'resultv2' => '1' //开启行政区域解析
- );
- //请求参数
- $post_data = array();
- $post_data["customer"] = $this->customer;
- $post_data["param"] = json_encode($param);
- $sign = md5($post_data["param"] . $this->key . $post_data["customer"]);
- $post_data["sign"] = strtoupper($sign);
- $params = "";
- foreach ($post_data as $k => $v) {
- $params .= "$k=" . urlencode($v) . "&"; //默认UTF-8编码格式
- }
- $post_data = substr($params, 0, -1);
- //发送post请求
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_URL, $this->url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($ch);
- $data = json_decode($result, true)['data'];
- if (empty($data)) {
- return [];
- }
- if ($data[0]['status'] == '签收') {
- $delivery = Db::name('shop_order_done_delivery')->where(['order_id' => $this->orderId])->value('id');
- if (empty($delivery)) {
- Db::name('shop_order_done_delivery')->insert(['order_id' => $this->orderId, 'code' => $this->code, 'number' => $this->number, 'details' => json_encode($data, JSON_UNESCAPED_UNICODE)]);
- } else {
- 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)]);
- }
- }
- \services\CacheServices::set(md5($this->orderId . $this->number), $data, $this->expire);
- }
- return $data;
- } catch (\Exception $e) {
- return false;
- }
- }
- /**
- * 魔术方法
- * @param $name
- * @param $arguments
- * @return $this
- */
- public function __call ($name, $arguments)
- {
- // TODO: Implement __call() method.
- if ($name == 'data') {
- $this->{$name} = $arguments[0];
- } else {
- $this->{$name} = $arguments[0];
- }
- return $this;
- }
- }
|