<?php
/**
* 分类接口
*/
namespace App\Controller\api\public;
use App\Controller\api\BaseController;
use App\Service\CommonService;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\Folder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CategoryController extends BaseController
{
/**
* 1.分类树
* @Route("/api/category/tree")
* @param Request $request
* @return array
*/
public function treeAction(Request $request)
{
$params = $request->request->all();
$lan = $params['lan'] ?? 'zh';
$folder = Folder::getByPath('/Category');
$data = $this->_getChildren($folder, $lan);
return $this->rsps(true, '列表', $data);
}
// for tree
private function _getChildren(Category|Folder $category, $lan='zh') {
if(!$category->hasChildren()) return [];
$data = [];
foreach($category->getChildren() as $item) {
$icon = $item->getIcon();
$data[] = [
'id' => $item->getId(),
'name' => $item->getName($lan),
'grade' => $item->getGrade(),
'icon' => $icon ? APP_DOMAIN.$icon->getFullPath() : '',
'isLeaf' => $item->getIsLeaf() ?? false,
'sort' => $item->getIdx(),
'pid' => $category->getId(),
'child'=>$this->_getChildren($item, $lan)
];
}
usort($data, function ($a, $b) {
return $a['sort']<=>$b['sort'];
});
return $data;
}
/**
* 2.分类参数定义
* @Route("/api/category/params")
* @param Request $request
* @return array
*/
public function paramsAction(Request $request)
{
$categoryId = $request->request->get('categoryId');
$lan = $request->request->get('lan', 'zh');
$category = Category::getById($categoryId);
if(!$category) {
return $this->rsps(false, '分类不存在');
}
$res = CommonService::getCategoryParamDefination($category, $lan);
return $this->rsps(true, 'success', $res);
}
/**
* 3.查一级分类
* @Route("/api/category/tops")
* @param Request $request
* @return array
*/
public function topsAction(Request $request)
{
$lan = $request->request->get('lan', 'zh');
$listing = new Category\Listing();
$listing->filterByGrade(1);
$listing->setOrderKey('idx');
$listing->setOrder('asc');
$list = $listing->getData();
$data = [];
foreach($list as $item) {
$icon = $item->getIcon();
$data[] = [
'id' => $item->getId(),
'name' => $item->getName($lan),
'icon' => $icon ? APP_DOMAIN.$icon->getFullPath() : '',
'isLeaf' => $item->getIsLeaf() ?? false,
'sort' => $item->getidx(),
'pid' => $item->getParent()->getId()
];
}
return $this->rsps(true, 'success', $data);
}
/**
* 4.查子分类
* @Route("/api/category/children")
* @param Request $request
* @return array
*/
public function childrenAction(Request $request)
{
$pid = $request->request->get('id');
$lan = $request->request->get('lan', 'zh');
$category = Category::getById($pid);
if(!$category) {
return $this->rsps(false, '分类id不存在');
}
$list = $category->getChildren([Category::OBJECT_TYPE_OBJECT]);
foreach($list as $item) {
$icon = $item->getIcon();
$data[] = [
'id' => $item->getId(),
'name' => $item->getName($lan),
'grade' => $item->getGrade(),
'icon' => $icon ? APP_DOMAIN.$icon->getFullPath() : '',
'isLeaf' => $item->getIsLeaf() ?? false,
'sort' => $item->getIdx(),
'pid' => $category->getId()
];
}
array_multisort(array_column($data, 'sort'), SORT_ASC, $data);
return $this->rsps(true, 'success', $data);
}
}