Category.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * | 栏目控制制器
  6. * +----------------------------------------------------------------------
  7. */
  8. namespace app\controller\sys;
  9. // 引入框架内置类
  10. use think\facade\View;
  11. use app\model\Category as CategoryModel;
  12. use app\utils\ReUtils;
  13. class Category extends Base
  14. {
  15. protected $modelName = "Category";
  16. protected $types = ['一般', '目录', '链接'];
  17. public function index()
  18. {
  19. $list = CategoryModel::getList(['order' => 'id DESC']);
  20. $list = list_tree($list, 'id', 'parent_id');
  21. View::assign('list', $list);
  22. View::assign('types', $this->types);
  23. return View::fetch();
  24. }
  25. /**
  26. * 新增 or 修改
  27. * @param int $id info id
  28. * @return mix
  29. */
  30. public function save($id = 0)
  31. {
  32. if ($id != 0) {
  33. $data = CategoryModel::find($id);
  34. } else {
  35. $data = new CategoryModel();
  36. }
  37. View::assign('data', $data);
  38. $list = CategoryModel::selectOne();
  39. View::assign('list', $list);
  40. return View::fetch();
  41. }
  42. public function doSave($id)
  43. {
  44. if ($this->app->request->isPost()) {
  45. $params = $this->app->request->param();
  46. if ($params['name'] == '') {
  47. $this->error("名称不能为空");
  48. }
  49. try {
  50. $id = $params['id'];
  51. unset($params['id']);
  52. if ($id != 0) {
  53. CategoryModel::update($params, ['id' => $id]);
  54. } else {
  55. CategoryModel::create($params);
  56. }
  57. return ReUtils::success();
  58. } catch (\Exception $e) {
  59. return ReUtils::error($e->getMessage());
  60. }
  61. }
  62. }
  63. // 删除
  64. public function delete($id)
  65. {
  66. if ($this->app->request->isPost()) {
  67. if (CategoryModel::where('parent_id', $id)->value('id')) {
  68. return ReUtils::error('子栏目不为空, 若要删除请先清空子栏目');
  69. }
  70. if (CategoryModel::destroy($id)) {
  71. return ReUtils::success();
  72. } else {
  73. return ReUtils::error();
  74. }
  75. }
  76. }
  77. // 导航状态变更
  78. public function navStatus(int $id)
  79. {
  80. if ($this->app->request->isPost()) {
  81. return CategoryModel::navStaus($id);
  82. }
  83. }
  84. }