Express.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // 使用阿里云查询顺丰时 物流单号需要加上手机尾号
  49. if ($config['default'] == 'aliyun' && $code === 'SF') {
  50. $lastPhoneNumber = mb_substr($address['phone'], -4);
  51. $expressNo = "{$expressNo}:$lastPhoneNumber";
  52. }
  53. // 请求API查询物流轨迹
  54. $result = ExpressFacade::store($config['default'])
  55. ->setOptions($config['providerConfig'][$config['default']])
  56. ->query($code, $expressNo);
  57. // 记录缓存, 有效期60分钟
  58. Cache::set($cacheIndex, $result, 60 * 60);
  59. return $result;
  60. }
  61. /**
  62. * 获取物流查询API配置项
  63. * @return mixed
  64. * @throws BaseException
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. private function getTracesConfig(int $storeId)
  70. {
  71. // 实例化快递100类
  72. $config = SettingModel::getItem(SettingEnum::DELIVERY, $storeId);
  73. if (empty($config['traces']['enable'])) {
  74. throwError('很抱歉,物流查询功能未开启');
  75. }
  76. return $config['traces'];
  77. }
  78. }