* @date 2020-01-22 09:37:54 */ declare(strict_types=1); namespace Modules\Api\Controllers; use App\Repositories\CategoryRepository; use Hyperf\Validation\Contract\ValidatorFactoryInterface; class CategoryController extends BaseController { /** * 获取导航 * * @param ValidatorFactoryInterface $validatorFactory * @param CategoryRepository $categoryRepository * @return void */ public function list(ValidatorFactoryInterface $validatorFactory, CategoryRepository $categoryRepository) { $inputData = $this->getJsonData(); $rules = [ 'parent_id' => 'nullable|int', 'type' => 'required|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $categoryData = $categoryRepository->allCategory($inputData['type']); return json_success($categoryData); } public function tagList(ValidatorFactoryInterface $validatorFactory, CategoryRepository $categoryRepository) { $inputData = $this->getJsonData(); $rules = [ 'is_hot' => 'nullable|int', 'type' => 'required|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $isHot = ''; if (isset($inputData['is_hot'])) { $isHot = $inputData['is_hot']; } $tagData = $categoryRepository->getTag($inputData['type'], $isHot); return json_success($tagData); } }