Category.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. return CategoryModel::del($id);
  71. }
  72. }
  73. }