Express.php 4.2 KB

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