Banner.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\model\relation\HasOne;
  16. use think\Paginator;
  17. /**
  18. * 轮播幻灯片模型类
  19. * Class Banner
  20. * @package app\common\model
  21. */
  22. class Banner extends BaseModel
  23. {
  24. protected $globalScope = [''];
  25. // 定义表名
  26. protected $name = 'banners';
  27. // 定义主键
  28. protected $pk = 'id';
  29. /**
  30. * 获取列表
  31. * @param array $param 查询条件
  32. * @param int $listRows 分页数量
  33. * @return mixed
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getList(array $param = [], int $listRows = 15)
  37. {
  38. // 筛选条件
  39. $query = $this->getQueryFilter($param);
  40. // 排序条件
  41. $sort = $this->setQuerySort($param);
  42. // 执行查询
  43. $list = $query->alias($this->name)
  44. ->order($sort)
  45. ->paginate($listRows);
  46. // 整理列表数据并返回
  47. return $list;
  48. }
  49. /**
  50. * 设置商品展示的数据
  51. * @param Collection|Paginator $list 商品列表
  52. * @param callable|null $callback 回调函数
  53. * @return mixed
  54. */
  55. protected function setListData($list, callable $callback = null)
  56. {
  57. if ($list->isEmpty()) return $list;
  58. // 遍历商品列表整理数据
  59. foreach ($list as &$item) {
  60. $data = $this->setData($item, $callback);
  61. }
  62. return $list;
  63. }
  64. /**
  65. * 整理数据
  66. * @param Collection|static $info
  67. * @param callable|null $callback
  68. * @return mixed
  69. */
  70. protected function setData($info, callable $callback = null)
  71. {
  72. // 回调函数
  73. is_callable($callback) && call_user_func($callback, $info);
  74. return $info->hidden(array_merge($this->hidden, ['update_time']));
  75. }
  76. /**
  77. * 检索查询条件
  78. * @param array $params
  79. * @return \think\db\BaseQuery
  80. */
  81. private function getQueryFilter(array $params)
  82. {
  83. $filter = [];
  84. // 实例化新查询对象
  85. $query = $this->getNewQuery();
  86. // 位置
  87. !empty($params['position']) && $filter[] = ['position', '=', "{$params['position']}"];
  88. // 类型
  89. !empty($params['type']) && $filter[] = ['type', '=', "{$params['type']}"];
  90. // 状态
  91. !empty($params['status']) && $filter[] = ['status', '=', "{$params['status']}"];
  92. // 实例化新查询对象
  93. return $query->where($filter)->where(function($query) use ($params){
  94. // 关键词
  95. if(!empty($params['keyword'])){
  96. $query->where('name','like', "%{$params['keyword']}%");
  97. }
  98. });
  99. }
  100. /**
  101. * 检索排序条件
  102. * @param array $param
  103. * @return array|string[]
  104. */
  105. private function setQuerySort(array $param = [])
  106. {
  107. $params = $this->setQueryDefaultValue($param, [
  108. 'sortType' => 'all', // 排序类型
  109. 'sort_order' => false, // 排序 (true高到低 false低到高)
  110. ]);
  111. // 排序规则
  112. $sort = [];
  113. if ($params['sortType'] === 'all') {
  114. $sort = ['sort_order' => 'desc','id'=>'desc'];
  115. } elseif ($params['sortType'] === 'pay') {
  116. $sort = ['id' => 'desc'];
  117. }
  118. return array_merge($sort, [$this->getPk() => 'desc']);
  119. }
  120. }