CategoriesController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\controller;
  10. use api\portal\service\PortalCategoryService;
  11. use cmf\controller\RestBaseController;
  12. use api\portal\model\PortalCategoryModel;
  13. class CategoriesController extends RestBaseController
  14. {
  15. /**
  16. * 获取分类列表
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function index()
  22. {
  23. $params = $this->request->get();
  24. $categoryService = new PortalCategoryService();
  25. $data = $categoryService->categories($params);
  26. if (empty($this->apiVersion) || $this->apiVersion == '1.0.0') {
  27. $response = $data;
  28. } else {
  29. $response = ['list' => $data];
  30. }
  31. $this->success('请求成功!', $response);
  32. }
  33. /**
  34. * 显示指定的分类
  35. * @param $id
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public function read($id)
  41. {
  42. $categoryModel = new PortalCategoryModel();
  43. $data = $categoryModel
  44. ->where('delete_time', 0)
  45. ->where('status', 1)
  46. ->where('id', $id)
  47. ->find();
  48. if ($data) {
  49. $this->success('请求成功!', $data);
  50. } else {
  51. $this->error('该分类不存在!');
  52. }
  53. }
  54. /**
  55. * 获取指定分类的子分类列表
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function subCategories()
  61. {
  62. $id = $this->request->get('category_id', 0, 'intval');
  63. $categoryModel = new PortalCategoryModel();
  64. $categories = $categoryModel->where(['parent_id' => $id])->select();
  65. if (!$categories->isEmpty()) {
  66. $this->success('请求成功', ['categories' => $categories]);
  67. } else {
  68. $this->error('该分类下没有子分类!');
  69. }
  70. }
  71. }