Express.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\model;
  13. use app\common\library\express\Usps;
  14. use cores\BaseModel;
  15. use app\common\library\express\Kuaidi100;
  16. use app\common\model\store\Setting as SettingModel;
  17. use app\common\enum\Setting as SettingEnum;
  18. use think\facade\Log;
  19. /**
  20. * 物流公司模型
  21. * Class Express
  22. * @package app\common\model
  23. */
  24. class Express extends BaseModel
  25. {
  26. // 定义表名
  27. protected $name = 'express';
  28. // 定义主键
  29. protected $pk = 'express_id';
  30. /**
  31. * 获取全部记录
  32. * @param array $param
  33. * @return \think\Collection
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getAll($param = [])
  39. {
  40. // 检索查询条件
  41. $filter = $this->getFilter($param);
  42. // 查询列表数据
  43. return $this->where($filter)->order(['sort', $this->getPk()])->select();
  44. }
  45. /**
  46. * 获取列表
  47. * @param array $param
  48. * @return \think\Paginator
  49. * @throws \think\db\exception\DbException
  50. */
  51. public function getList($param = [])
  52. {
  53. // 检索查询调价你
  54. $filter = $this->getFilter($param);
  55. // 查询列表数据
  56. return $this->where($filter)->order(['sort', 'express_id'])->paginate(15);
  57. }
  58. /**
  59. * 检索查询条件
  60. * @param array $param
  61. * @return array
  62. */
  63. private function getFilter($param = [])
  64. {
  65. // 默认查询条件
  66. $params = $this->setQueryDefaultValue($param, ['search' => '']);
  67. // 检索查询条件
  68. $filter = [];
  69. !empty($params['search']) && $filter[] = ['express_name', 'like', "%{$params['search']}%"];
  70. return $filter;
  71. }
  72. /**
  73. * 物流公司详情
  74. * @param int $expressId
  75. * @return null|static
  76. */
  77. public static function detail(int $expressId)
  78. {
  79. return self::get($expressId);
  80. }
  81. /**
  82. * 获取物流动态信息
  83. * @param string $expressName
  84. * @param string $expressCode
  85. * @param string $expressNo
  86. * @return false|string[]
  87. * @throws \app\common\exception\BaseException
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function dynamic(string $expressName, string $expressCode, string $expressNo)
  93. {
  94. $data = [
  95. 'express_name' => $expressName,
  96. 'express_no' => $expressNo
  97. ];
  98. // 获取快递100配置项
  99. $config = $this->getKuaidi100Config();
  100. // 实例化快递100类
  101. $Kuaidi100 = new Kuaidi100($config);
  102. // 请求查询接口
  103. $data['list'] = $Kuaidi100->query($expressCode, $expressNo);
  104. if ($data['list'] === false) {
  105. $this->error = $Kuaidi100->getError();
  106. return false;
  107. }
  108. return $data;
  109. }
  110. public function dynamicUsps(string $uspsUserId, string $expressNo)
  111. {
  112. // 实例化快递100类
  113. $Usps = new Usps($uspsUserId);
  114. // 请求查询接口
  115. $data['list'] = $Usps->query($expressNo);
  116. if ($data['list'] === false) {
  117. Log::error('有问题');
  118. return false;
  119. }
  120. return $data;
  121. }
  122. /**
  123. * 获取快递100配置项
  124. * @return mixed
  125. * @throws \app\common\exception\BaseException
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. private function getKuaidi100Config()
  131. {
  132. // 实例化快递100类
  133. $config = SettingModel::getItem(SettingEnum::DELIVERY);
  134. if (empty($config['kuaidi100']['customer']) || empty($config['kuaidi100']['key'])) {
  135. throwError('请先到后台-设置-配送方式 补充物流查询API配置');
  136. }
  137. return $config['kuaidi100'];
  138. }
  139. }