src/Controller/api/public/CategoryController.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * 分类接口
  4.  */
  5. namespace App\Controller\api\public;
  6. use App\Controller\api\BaseController;
  7. use App\Service\CommonService;
  8. use Pimcore\Model\DataObject\Category;
  9. use Pimcore\Model\DataObject\Folder;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class CategoryController extends BaseController
  14. {
  15.     /**
  16.      * 1.分类树
  17.      * @Route("/api/category/tree")
  18.      * @param Request $request
  19.      * @return array
  20.      */
  21.     public function treeAction(Request $request)
  22.     {
  23.         $params $request->request->all();
  24.         $lan $params['lan'] ?? 'zh';
  25.         $folder Folder::getByPath('/Category');
  26.         $data $this->_getChildren($folder$lan);
  27.         return $this->rsps(true'列表'$data);
  28.     }
  29.     // for tree
  30.     private function _getChildren(Category|Folder $category$lan='zh') {
  31.         if(!$category->hasChildren()) return [];
  32.         $data = [];
  33.         foreach($category->getChildren() as $item) {
  34.             $icon $item->getIcon();
  35.             $data[] = [
  36.                 'id' => $item->getId(),
  37.                 'name' => $item->getName($lan),
  38.                 'grade' => $item->getGrade(),
  39.                 'icon' => $icon APP_DOMAIN.$icon->getFullPath() : '',
  40.                 'isLeaf' => $item->getIsLeaf() ?? false,
  41.                 'sort' => $item->getIdx(),
  42.                 'pid' => $category->getId(),
  43.                 'child'=>$this->_getChildren($item$lan)
  44.             ];
  45.         }
  46.         usort($data, function ($a$b) {
  47.             return $a['sort']<=>$b['sort'];
  48.         });
  49.         return $data;
  50.     }
  51.         /**
  52.      * 2.分类参数定义
  53.      * @Route("/api/category/params")
  54.      * @param Request $request
  55.      * @return array
  56.      */
  57.     public function paramsAction(Request $request)
  58.     {
  59.         $categoryId $request->request->get('categoryId');
  60.         $lan $request->request->get('lan''zh');
  61.         $category Category::getById($categoryId);
  62.         if(!$category) {
  63.             return $this->rsps(false'分类不存在');
  64.         }
  65.         $res CommonService::getCategoryParamDefination($category$lan);
  66.         return $this->rsps(true'success'$res);
  67.     }
  68.     /**
  69.      * 3.查一级分类
  70.      * @Route("/api/category/tops")
  71.      * @param Request $request
  72.      * @return array
  73.      */
  74.     public function topsAction(Request $request)
  75.     {
  76.         $lan $request->request->get('lan''zh');
  77.         $listing = new Category\Listing();
  78.         $listing->filterByGrade(1);
  79.         $listing->setOrderKey('idx');
  80.         $listing->setOrder('asc');
  81.         $list $listing->getData();
  82.         $data = [];
  83.         foreach($list as $item) {
  84.             $icon $item->getIcon();
  85.             $data[] = [
  86.                 'id' => $item->getId(),
  87.                 'name' => $item->getName($lan),
  88.                 'icon' => $icon APP_DOMAIN.$icon->getFullPath() : '',
  89.                 'isLeaf' => $item->getIsLeaf() ?? false,
  90.                 'sort' => $item->getidx(),
  91.                 'pid' => $item->getParent()->getId()
  92.             ];
  93.         }
  94.         return $this->rsps(true'success'$data);
  95.     }
  96.     /**
  97.      * 4.查子分类
  98.      * @Route("/api/category/children")
  99.      * @param Request $request
  100.      * @return array
  101.      */
  102.     public function childrenAction(Request $request)
  103.     {
  104.         $pid $request->request->get('id');
  105.         $lan $request->request->get('lan''zh');
  106.         $category Category::getById($pid);
  107.         if(!$category) {
  108.             return $this->rsps(false'分类id不存在');
  109.         }
  110.         $list $category->getChildren([Category::OBJECT_TYPE_OBJECT]);
  111.         foreach($list as $item) {
  112.             $icon $item->getIcon();
  113.             $data[] = [
  114.                 'id' => $item->getId(),
  115.                 'name' => $item->getName($lan),
  116.                 'grade' => $item->getGrade(),
  117.                 'icon' => $icon APP_DOMAIN.$icon->getFullPath() : '',
  118.                 'isLeaf' => $item->getIsLeaf() ?? false,
  119.                 'sort' => $item->getIdx(),
  120.                 'pid' => $category->getId()
  121.             ];
  122.         }
  123.         array_multisort(array_column($data'sort'), SORT_ASC$data);
  124.         return $this->rsps(true'success'$data);
  125.     }
  126. }