Express.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. !empty($params['type']) && $filter[] = ['type', '=', $params['type']];
  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. * 获取物流信息
  110. * @param string $expressNo 快递单号
  111. * @param string $delivery_time 订单发货的时间
  112. * @param int $type 1获取列表 2获取最新消息
  113. * @return array|bool
  114. * @throws \app\common\exception\BaseException
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @author: zjwhust
  119. * @Time: 2021/9/29 13:51
  120. */
  121. public function deliverylist(string $expressNo, string $delivery_time, int $type = 1)
  122. {
  123. // $data = [
  124. // 'express_no' => $expressNo
  125. // ];
  126. // 获取快递100配置项
  127. $config = $this->getKuaidi100Config();
  128. // 实例化快递100类
  129. $Kuaidi100 = new Kuaidi100($config);
  130. // 请求查询接口
  131. $data = $Kuaidi100->deliverylist($expressNo, $delivery_time, $type);
  132. if ($data === false) {
  133. $this->error = $Kuaidi100->getError();
  134. return false;
  135. }
  136. return $data;
  137. }
  138. /**
  139. * 获取快递100配置项
  140. * @return mixed
  141. * @throws \app\common\exception\BaseException
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. private function getKuaidi100Config()
  147. {
  148. // 实例化快递100类
  149. $config = SettingModel::getItem(SettingEnum::DELIVERY);
  150. if (empty($config['kuaidi100']['customer']) || empty($config['kuaidi100']['key'])) {
  151. throwError('请先到后台-设置-配送方式 补充物流查询API配置');
  152. }
  153. return $config['kuaidi100'];
  154. }
  155. public static function getExpressNames(){
  156. $names = self::field('express_name')->select()->column('express_name');
  157. return implode(',',$names);
  158. }
  159. /**
  160. * 获取所有列表
  161. * @return array
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. */
  166. public static function getAllList()
  167. {
  168. // 查询列表数据
  169. return self::field('express_id,express_name')->order(['sort', 'express_id'])->select()->toArray();
  170. }
  171. }