Category.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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', 'alls'];
  12. //分类列表
  13. public function index()
  14. {
  15. $category_mode = $this->request->param('category_mode');
  16. if ($category_mode == 1) { //一级分类
  17. $list = CategoryModel::relation([
  18. 'goods' => function ($query) {
  19. $query->field('id,title,sub_title,image,price,sales,views,description,lineation_price,createtime')->limit(10);
  20. }
  21. ])->where('pid', 0)
  22. ->where('status',StatusEnum::ENABLED)
  23. ->order('weigh desc,id asc')->select();
  24. } else //二级分类 //三级分类
  25. {
  26. $tree = Tree::instance();
  27. $categoryList = CategoryModel::field('id,pid,name,image')
  28. ->order('weigh desc,id asc')
  29. ->where('status',StatusEnum::ENABLED)
  30. ->select();
  31. $tree->init(collection($categoryList)->toArray(), 'pid');
  32. $list = $tree->getTreeArray(0);
  33. }
  34. $this->success('获取成功', $list);
  35. }
  36. //所有分类
  37. public function alls()
  38. {
  39. $tree = Tree::instance();
  40. $list = CategoryModel::field('id,pid,name,image')->order('weigh asc,id asc')->where('isnav', 1)->select();
  41. $tree->init(collection($list)->toArray(), 'pid');
  42. $categoryList = $tree->getTreeList($tree->getTreeArray(0), 'name');
  43. $this->success('获取成功', $categoryList);
  44. }
  45. }