Kd100Service.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. /**
  13. * 快递100接口管理-服务类
  14. * @author laravel开发员
  15. * @since 2020/11/11
  16. * @package App\Services
  17. */
  18. class Kd100Service extends BaseService
  19. {
  20. // 静态对象
  21. protected static $instance = null;
  22. protected $apiUrl = 'https://poll.kuaidi100.com/poll/query.do';
  23. protected $apiKey = '';
  24. protected $apiCode = '';
  25. public function __construct()
  26. {
  27. $this->apiKey = ConfigService::make()->getConfigByCode('kd_customer_key');
  28. $this->apiCode = ConfigService::make()->getConfigByCode('kd_customer_code');
  29. }
  30. /**
  31. * 静态入口
  32. */
  33. public static function make(){
  34. if(!self::$instance){
  35. self::$instance = new static();
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * 查询
  41. * @param $no 快递单号
  42. * @param string $phone // 手机号
  43. * @param string $code 快递公司编号
  44. * @return bool
  45. */
  46. public function query($no, $phone, $code,$to='')
  47. {
  48. if(empty($this->apiKey) || empty($this->apiCode)){
  49. $this->error = '接口参数未配置';
  50. return false;
  51. }
  52. $cacheKey = "caches:kd100:{$no}_{$code}";
  53. if(RedisService::get($cacheKey.'_lock')){
  54. $this->error = '2011';
  55. return false;
  56. }
  57. $param = [
  58. 'com' => $code, // 快递公司编码
  59. 'num' => $no, // 快递单号
  60. 'phone' => $phone, // 手机号
  61. 'from' => '', // 出发地城市
  62. 'to' => $to, // 目的地城市
  63. 'resultv2' => $to?8:1, // 开启行政区域解析
  64. 'show' => '0', // 返回格式:0:json格式(默认),1:xml,2:html,3:text
  65. 'order' => 'desc', // 返回结果排序:desc降序(默认),asc 升序
  66. 'needCourierInfo'=> true // 是否返回快递员电话
  67. ];
  68. //请求参数
  69. $data = [];
  70. $data['customer'] = $this->apiCode;
  71. $data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
  72. $sign = md5($data['param'].$this->apiKey.$data['customer']);
  73. $data['sign'] = strtoupper($sign);
  74. $result = httpRequest($this->apiUrl, $data,'post','',5);
  75. return $result;
  76. }
  77. }