StoreCategory.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. $page = (int)$page;
  21. $pageSize = (int)$pageSize;
  22. $query = $this->where($where);
  23. $totalCount = $query->count();
  24. $list = $query
  25. ->order('sort', 'desc')
  26. ->order('id', 'desc')
  27. ->page($page, $pageSize)
  28. ->select()
  29. ->toArray();
  30. return [
  31. 'list' => $list,
  32. 'totalCount' => $totalCount,
  33. 'pageSize' => $pageSize,
  34. 'page' => $page
  35. ];
  36. }
  37. /**
  38. * 获取树形分类
  39. * @param int $pid
  40. * @return array
  41. */
  42. public function getTree($pid = 0)
  43. {
  44. $list = $this->where('pid', $pid)->order('sort', 'desc')->select()->toArray();
  45. foreach ($list as &$item) {
  46. $children = $this->getTree($item['id']);
  47. if (!empty($children)) {
  48. $item['children'] = $children;
  49. }
  50. }
  51. return $list;
  52. }
  53. }