Kuaidi100.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\express;
  13. use think\facade\Cache;
  14. use cores\traits\ErrorTrait;
  15. use app\common\library\helper;
  16. /**
  17. * 快递100API模块
  18. * Class Kuaidi100
  19. * @package app\common\library\express
  20. */
  21. class Kuaidi100
  22. {
  23. use ErrorTrait;
  24. // 微信支付配置
  25. /* @var array $config */
  26. private $config;
  27. /**
  28. * 构造方法
  29. * WxPay constructor.
  30. * @param $config
  31. */
  32. public function __construct($config)
  33. {
  34. $this->config = $config;
  35. }
  36. /**
  37. * 执行查询
  38. * @param string $code
  39. * @param string $expressNo
  40. * @return bool
  41. */
  42. public function query(string $code, string $expressNo): bool
  43. {
  44. // 缓存索引
  45. $cacheIndex = "express_{$code}_$expressNo";
  46. if ($cacheData = Cache::get($cacheIndex)) {
  47. return $cacheData;
  48. }
  49. // 参数设置
  50. $postData = [
  51. 'customer' => $this->config['customer'],
  52. 'param' => helper::jsonEncode([
  53. 'resultv2' => '1',
  54. 'com' => $code,
  55. 'num' => $expressNo
  56. ])
  57. ];
  58. $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
  59. // 请求快递100 api
  60. $url = 'http://poll.kuaidi100.com/poll/query.do';
  61. $result = curl_post($url, http_build_query($postData));
  62. $express = helper::jsonDecode($result);
  63. // 记录错误信息
  64. if (isset($express['returnCode']) || !isset($express['data'])) {
  65. $this->error = $express['message'] ?? '查询失败';
  66. return false;
  67. }
  68. // 记录缓存, 时效5分钟
  69. Cache::set($cacheIndex, $express['data'], 300);
  70. return $express['data'];
  71. }
  72. }