Express.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\api\controller\points\Log;
  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. !empty($params['type']) && $filter[] = ['type', '=', $params['type']];
  69. return $filter;
  70. }
  71. /**
  72. * 物流公司详情
  73. * @param int $expressId
  74. * @return null|static
  75. */
  76. public static function detail(int $expressId)
  77. {
  78. return self::get($expressId);
  79. }
  80. /**
  81. * 获取物流动态信息
  82. * @param string $expressName
  83. * @param string $expressCode
  84. * @param string $expressNo
  85. * @return false|string[]
  86. * @throws \app\common\exception\BaseException
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function dynamic(string $expressName, string $expressCode, string $expressNo, $phone = '')
  92. {
  93. $data = [
  94. 'express_name' => $expressName,
  95. 'express_no' => $expressNo
  96. ];
  97. // 获取快递100配置项
  98. $config = $this->getKuaidi100Config();
  99. // 实例化快递100类
  100. $Kuaidi100 = new Kuaidi100($config);
  101. // 请求查询接口
  102. $data['list'] = $Kuaidi100->query($expressCode, $expressNo,$phone);
  103. if ($data['list'] === false) {
  104. $this->error = $Kuaidi100->getError();
  105. return false;
  106. }
  107. return $data;
  108. }
  109. /**
  110. * 获取物流信息
  111. * @param string $expressNo 快递单号
  112. * @param string $delivery_time 订单发货的时间
  113. * @param int $type 1获取列表 2获取最新消息
  114. * @return array|bool
  115. * @throws \app\common\exception\BaseException
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @author: zjwhust
  120. * @Time: 2021/9/29 13:51
  121. */
  122. public function deliverylist(string $expressNo, string $delivery_time, int $type = 1)
  123. {
  124. // $data = [
  125. // 'express_no' => $expressNo
  126. // ];
  127. // 获取快递100配置项
  128. $config = $this->getKuaidi100Config();
  129. // 实例化快递100类
  130. $Kuaidi100 = new Kuaidi100($config);
  131. // 请求查询接口
  132. $data = $Kuaidi100->deliverylist($expressNo, $delivery_time, $type);
  133. \think\facade\Log::info('delivery:'.$expressNo.':'.json_encode($data));
  134. if ($data === false) {
  135. $this->error = $Kuaidi100->getError();
  136. return false;
  137. }
  138. return $data;
  139. }
  140. /**
  141. * 获取快递100配置项
  142. * @return mixed
  143. * @throws \app\common\exception\BaseException
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. private function getKuaidi100Config()
  149. {
  150. // 实例化快递100类
  151. $config = SettingModel::getItem(SettingEnum::DELIVERY);
  152. if (empty($config['kuaidi100']['customer']) || empty($config['kuaidi100']['key'])) {
  153. throwError('请先到后台-设置-配送方式 补充物流查询API配置');
  154. }
  155. return $config['kuaidi100'];
  156. }
  157. public static function getExpressNames(){
  158. $names = self::field('express_name')->select()->column('express_name');
  159. return implode(',',$names);
  160. }
  161. /**
  162. * 获取所有列表
  163. * @return array
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public static function getAllList()
  169. {
  170. // 查询列表数据
  171. return self::field('express_id,express_name')->order(['sort', 'express_id'])->select()->toArray();
  172. }
  173. }