Category.php 972 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Category as CategoryModel;
  4. use fast\Tree;
  5. use app\common\Enum\StatusEnum;
  6. /**
  7. * 商品分类
  8. */
  9. class Category extends Base
  10. {
  11. protected $noNeedLogin = ['index'];
  12. //分类列表
  13. public function index()
  14. {
  15. $tree = Tree::instance();
  16. $categoryList = CategoryModel::field('id,pid,name,image')
  17. ->order('weigh desc,id asc')
  18. ->where('status',StatusEnum::ENABLED)
  19. ->select();
  20. $tree->init(collection($categoryList)->toArray(), 'pid');
  21. $list = $tree->getTreeArray(0);
  22. $this->success('获取成功', $list);
  23. }
  24. // 查询所有分类
  25. public function getCategoryList()
  26. {
  27. $categoryList = CategoryModel::field('id,pid,name,image')
  28. ->order('weigh asc,id asc')
  29. ->where('status',StatusEnum::ENABLED)
  30. ->select();
  31. $this->success('获取成功', $categoryList);
  32. }
  33. }