CategoryController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * 视频分类控制器.
  4. *
  5. * @author nj <nj@gmail.com>
  6. * @date 2020-01-22 09:37:54
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Controllers;
  10. use App\Repositories\CategoryRepository;
  11. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  12. class CategoryController extends BaseController
  13. {
  14. /**
  15. * 获取导航
  16. *
  17. * @param ValidatorFactoryInterface $validatorFactory
  18. * @param CategoryRepository $categoryRepository
  19. * @return void
  20. */
  21. public function list(ValidatorFactoryInterface $validatorFactory, CategoryRepository $categoryRepository)
  22. {
  23. $inputData = $this->getJsonData();
  24. $rules = [
  25. 'parent_id' => 'nullable|int',
  26. 'type' => 'required|int',
  27. ];
  28. $validator = $validatorFactory->make($inputData, $rules);
  29. if ($validator->fails()) {
  30. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  31. return json_fail($validator->errors()->first());
  32. }
  33. $categoryData = $categoryRepository->allCategory($inputData['type']);
  34. return json_success($categoryData);
  35. }
  36. public function tagList(ValidatorFactoryInterface $validatorFactory, CategoryRepository $categoryRepository)
  37. {
  38. $inputData = $this->getJsonData();
  39. $rules = [
  40. 'is_hot' => 'nullable|int',
  41. 'type' => 'required|int',
  42. ];
  43. $validator = $validatorFactory->make($inputData, $rules);
  44. if ($validator->fails()) {
  45. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  46. return json_fail($validator->errors()->first());
  47. }
  48. $isHot = '';
  49. if (isset($inputData['is_hot'])) {
  50. $isHot = $inputData['is_hot'];
  51. }
  52. $tagData = $categoryRepository->getTag($inputData['type'], $isHot);
  53. return json_success($tagData);
  54. }
  55. }