StoreCategory.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class StoreCategory extends Model
  9. {
  10. protected $name = 'store_category';
  11. /**
  12. * 获取分类列表
  13. * @param array $where
  14. * @param int $page
  15. * @param int $pageSize
  16. * @return array
  17. */
  18. public function getList($where = [], $page = 1, $pageSize = 20)
  19. {
  20. $query = $this->where($where);
  21. $totalCount = $query->count();
  22. $list = $query
  23. ->order('sort', 'desc')
  24. ->order('id', 'desc')
  25. ->page($page, $pageSize)
  26. ->select()
  27. ->toArray();
  28. return [
  29. 'list' => $list,
  30. 'totalCount' => $totalCount,
  31. 'pageSize' => $pageSize,
  32. 'page' => $page
  33. ];
  34. }
  35. /**
  36. * 获取树形分类
  37. * @param int $pid
  38. * @return array
  39. */
  40. public function getTree($pid = 0)
  41. {
  42. $list = $this->where('pid', $pid)->order('sort', 'desc')->select()->toArray();
  43. foreach ($list as &$item) {
  44. $children = $this->getTree($item['id']);
  45. if (!empty($children)) {
  46. $item['children'] = $children;
  47. }
  48. }
  49. return $list;
  50. }
  51. }