Category.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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();
  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 = [
  34. "id" => 0,
  35. "parent_id" => 0,
  36. "name" => "",
  37. "url" => "",
  38. "route" => "",
  39. "tablename" => "",
  40. "template" => "",
  41. "type" => 0,
  42. "is_nav" => 0,
  43. "remark" => "",
  44. "sort" => 0,
  45. "title" => "",
  46. "keywords" => "",
  47. "description" => "",
  48. "is_blank" => 0
  49. ];
  50. }
  51. View::assign('data', $data);
  52. $list = list_tree(CategoryModel::select());
  53. View::assign('list', $list);
  54. return View::fetch();
  55. }
  56. public function doSave($id)
  57. {
  58. if ($this->app->request->isPost()) {
  59. $params = $this->app->request->param();
  60. if ($params['name'] == '') {
  61. $this->error("名称不能为空");
  62. }
  63. try {
  64. $id = $params['id'];
  65. unset($params['id']);
  66. if ($id != 0) {
  67. CategoryModel::update($params, ['id' => $id]);
  68. } else {
  69. CategoryModel::create($params);
  70. }
  71. } catch (\Exception $e) {
  72. $msg = $e->getMessage();
  73. $this->error("错误提示:" . $msg);
  74. }
  75. $this->success('操作成功', (string)url('index'));
  76. }
  77. }
  78. // 删除
  79. public function delete($id)
  80. {
  81. if ($this->app->request->isPost()) {
  82. if (CategoryModel::where('parent_id', $id)->value('id')) {
  83. return ['code' => 0, 'msg' => '子栏目不为空, 若要删除请先清空子栏目'];
  84. }
  85. return CategoryModel::del($id);
  86. }
  87. }
  88. }