Express.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\service;
  13. use think\facade\Cache;
  14. use app\common\enum\Setting as SettingEnum;
  15. use app\common\model\store\Setting as SettingModel;
  16. use app\common\library\express\Facade as ExpressFacade;
  17. use cores\exception\BaseException;
  18. /**
  19. * 服务类:物流管理
  20. * Class Express
  21. * @package app\common\service
  22. */
  23. class Express extends BaseService
  24. {
  25. /**
  26. * 物流轨迹查询
  27. * @param mixed $express 物流公司记录
  28. * @param string $expressNo 物流单号
  29. * @param $address
  30. * @param int $storeId 商城ID
  31. * @return array
  32. * @throws BaseException
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function traces($express, string $expressNo, $address, int $storeId): array
  38. {
  39. // 获取物流查询API配置项
  40. $config = $this->getTracesConfig($storeId);
  41. // 物流公司编码
  42. $code = $config['default'] === 'kd100' ? $express['kuaidi100_code'] : $express['kdniao_code'];
  43. // 获取缓存的数据
  44. $cacheIndex = "expressTraces_{$code}_$expressNo";
  45. if (Cache::has($cacheIndex)) {
  46. return Cache::get($cacheIndex);
  47. }
  48. // 请求API查询物流轨迹
  49. $result = ExpressFacade::store($config['default'])
  50. ->setOptions($config['providerConfig'][$config['default']])
  51. ->query($code, $expressNo, ['phone' => $address['phone']]);
  52. // 记录缓存, 有效期60分钟
  53. Cache::set($cacheIndex, $result, 60 * 60);
  54. return $result;
  55. }
  56. /**
  57. * 获取物流查询API配置项
  58. * @return mixed
  59. * @throws BaseException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. private function getTracesConfig(int $storeId)
  65. {
  66. // 实例化快递100类
  67. $config = SettingModel::getItem(SettingEnum::DELIVERY, $storeId);
  68. if (empty($config['traces']['enable'])) {
  69. throwError('很抱歉,物流查询功能未开启');
  70. }
  71. return $config['traces'];
  72. }
  73. }