ExtensionWuLiuEntity.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\AliyunWuliu\Entity;
  4. use SixShop\AliyunWuliu\Facade\WuLiuClient;
  5. use SixShop\Core\Entity\BaseEntity;
  6. use think\facade\Cache;
  7. use think\Model;
  8. use function SixShop\Core\throw_logic_exception;
  9. class ExtensionWuLiuEntity extends BaseEntity
  10. {
  11. public function getWuliuInfo(int $userID, string $no, string $type = ''): array
  12. {
  13. $entity = $this->where('number', $no)->when($type, function (Model $query, $type) {
  14. $query->where('type', $type);
  15. })->findOrEmpty();
  16. if (!$entity->isEmpty() && ($entity->is_sign == 1 || $entity->update_time > time() - 3600)) {
  17. // 已签收或一小时内更新过,直接返回
  18. return $entity->toArray();
  19. }
  20. // 一个人一天最多查询100次
  21. $name = 'wuliu_' . date('Ymd') . '_' . $userID;
  22. $times = Cache::remember($name, function () use ($name) {
  23. return Cache::get($name, 0) + 1;
  24. }, 86400);
  25. if ($times > 100) {
  26. throw_logic_exception('查询太过频繁,请明天再试');
  27. }
  28. $result = WuLiuClient::kdi($no, $type);
  29. $entity->number = $result->number;
  30. $entity->type = $result->type;
  31. $entity->list = $result->list;
  32. $entity->delivery_status = $result->deliverystatus;
  33. $entity->is_sign = $result->issign;
  34. $entity->exp_name = $result->expName;
  35. $entity->exp_site = $result->expSite;
  36. $entity->exp_phone = $result->expPhone;
  37. $entity->courier = $result->courier;
  38. $entity->courier_phone = $result->courierPhone;
  39. $entity->take_time = $result->takeTime;
  40. $entity->save();
  41. return $entity->toArray();
  42. }
  43. }