| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- /**
- * 快递100接口管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services
- */
- class Kd100Service extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- protected $apiUrl = 'https://poll.kuaidi100.com/poll/query.do';
- protected $apiKey = '';
- protected $apiCode = '';
- public function __construct()
- {
- $this->apiKey = ConfigService::make()->getConfigByCode('kd_customer_key');
- $this->apiCode = ConfigService::make()->getConfigByCode('kd_customer_code');
- }
- /**
- * 静态入口
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 查询
- * @param $no 快递单号
- * @param string $phone // 手机号
- * @param string $code 快递公司编号
- * @return bool
- */
- public function query($no, $phone, $code,$to='')
- {
- if(empty($this->apiKey) || empty($this->apiCode)){
- $this->error = '接口参数未配置';
- return false;
- }
- $cacheKey = "caches:kd100:{$no}_{$code}";
- if(RedisService::get($cacheKey.'_lock')){
- $this->error = '2011';
- return false;
- }
- $param = [
- 'com' => $code, // 快递公司编码
- 'num' => $no, // 快递单号
- 'phone' => $phone, // 手机号
- 'from' => '', // 出发地城市
- 'to' => $to, // 目的地城市
- 'resultv2' => $to?8:1, // 开启行政区域解析
- 'show' => '0', // 返回格式:0:json格式(默认),1:xml,2:html,3:text
- 'order' => 'desc', // 返回结果排序:desc降序(默认),asc 升序
- 'needCourierInfo'=> true // 是否返回快递员电话
- ];
- //请求参数
- $data = [];
- $data['customer'] = $this->apiCode;
- $data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
- $sign = md5($data['param'].$this->apiKey.$data['customer']);
- $data['sign'] = strtoupper($sign);
- $result = httpRequest($this->apiUrl, $data,'post','',5);
- RedisService::set($cacheKey,['url'=>$this->apiUrl,'data'=>$data,'result'=>$result], 7200);
- return $result;
- }
- }
|