Express.php 4.2 KB

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