Category.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\api\controller;
  3. use addons\shop\model\Category as CategoryModel;
  4. use fast\Tree;
  5. /**
  6. * 商品分类
  7. */
  8. class Category extends Base
  9. {
  10. protected $noNeedLogin = ['index', 'alls'];
  11. //分类列表
  12. public function index()
  13. {
  14. $category_mode = $this->request->param('category_mode');
  15. if ($category_mode == 1) { //一级分类
  16. $list = CategoryModel::relation([
  17. 'goods' => function ($query) {
  18. $query->field('id,title,image,price,sales,views,description,marketprice,createtime')->limit(10);
  19. }
  20. ])->where('pid', 0)->where('isnav', 1)->order('weigh desc,id asc')->select();
  21. } else //二级分类 //三级分类
  22. {
  23. $tree = Tree::instance();
  24. $categoryList = CategoryModel::field('id,pid,name,image')->order('weigh desc,id asc')->where('isnav', 1)->select();
  25. $tree->init(collection($categoryList)->toArray(), 'pid');
  26. $list = $tree->getTreeArray(0);
  27. }
  28. $this->success('获取成功', $list);
  29. }
  30. //所有分类
  31. public function alls()
  32. {
  33. $tree = Tree::instance();
  34. $list = CategoryModel::field('id,pid,name,image')->order('weigh asc,id asc')->where('isnav', 1)->select();
  35. $tree->init(collection($list)->toArray(), 'pid');
  36. $categoryList = $tree->getTreeList($tree->getTreeArray(0), 'name');
  37. $this->success('获取成功', $categoryList);
  38. }
  39. }