Category.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * | 栏目控制制器
  6. * +----------------------------------------------------------------------
  7. */
  8. namespace app\sys\controller;
  9. // 引入框架内置类
  10. use think\facade\View;
  11. use app\common\model\Category as CategoryModel;
  12. class Category extends Base
  13. {
  14. protected $modelName = "Category";
  15. public function index()
  16. {
  17. $list = CategoryModel::getList(['order'=>'id DESC']);
  18. $list = list_tree($list, 'id', 'parent_id');
  19. View::assign('list', $list);
  20. View::assign('types', ['','一般','目录','单页','锚点','链接']);
  21. return View::fetch();
  22. }
  23. /**
  24. * 新增 or 修改
  25. * @param int $id info id
  26. * @return mix
  27. */
  28. public function save($id = 0)
  29. {
  30. if ($id != 0) {
  31. $data = CategoryModel::find($id);
  32. } else {
  33. $data = new CategoryModel();
  34. $data->is_nav = 1;
  35. }
  36. View::assign('data', $data);
  37. $list = list_tree(CategoryModel::select());
  38. View::assign('list', $list);
  39. return View::fetch();
  40. }
  41. public function doSave($id)
  42. {
  43. if ($this->app->request->isPost()) {
  44. $params = $this->app->request->param();
  45. if ($params['name'] == '') {
  46. $this->error("名称不能为空");
  47. }
  48. try {
  49. $id = $params['id'];
  50. unset($params['id']);
  51. if ($id != 0) {
  52. CategoryModel::update($params, ['id' => $id]);
  53. } else {
  54. CategoryModel::create($params);
  55. }
  56. } catch (\Exception $e) {
  57. $msg = $e->getMessage();
  58. $this->error("错误提示:" . $msg);
  59. }
  60. $this->success('操作成功', (string)url('index'));
  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 ['code' => 0, 'msg' => '子栏目不为空, 若要删除请先清空子栏目'];
  69. }
  70. if (CategoryModel::destroy($id)) {
  71. return ['code' => 0,'msg'=>'删除成功'];
  72. } else {
  73. return ['code' => 1,'msg'=>'删除失败'];
  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. }