Category.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * +----------------------------------------------------------------------
  5. * | 栏目控制制器
  6. * +----------------------------------------------------------------------
  7. */
  8. namespace app\admin\controller;
  9. // 引入框架内置类
  10. use think\facade\View;
  11. class Category extends Base
  12. {
  13. protected $modelName = "\app\common\model\Category";
  14. public function index()
  15. {
  16. $list = $this->modelName::getList();
  17. $list = obj_tree($list, $pk = 'id', $pid = 'parent_id');
  18. View::assign('list', $list);
  19. return View::fetch();
  20. }
  21. /**
  22. * 新增 or 修改
  23. * @param int $id info id
  24. * @return mix
  25. */
  26. public function save($id = 0)
  27. {
  28. if ($this->app->request->isPost()) {
  29. $params = $this->app->request->param();
  30. if ($params['category_name'] == '') {
  31. $this->error("名称不能为空");
  32. }
  33. try {
  34. $id = $params['id'];
  35. unset($params['id']);
  36. if ($id != 0) {
  37. $this->modelName::update($params, ['id' => $id]);
  38. } else {
  39. $this->modelName::create($params);
  40. }
  41. } catch (\Exception $e) {
  42. $msg = $e->getMessage();
  43. $this->error("错误提示:".$msg);
  44. }
  45. $this->success('操作成功', (String)url('index'));
  46. } else {
  47. if ($id != 0) {
  48. $data = $this->modelName::find($id);
  49. } else {
  50. $data = null;
  51. }
  52. View::assign('data', $data);
  53. $list = obj_tree($this->modelName::select());
  54. View::assign('list', $list);
  55. return View::fetch();
  56. }
  57. }
  58. // 删除
  59. public function delete($id)
  60. {
  61. if ($this->app->request->isPost()) {
  62. if ($this->modelName::where('parent_id', $id)->value('id')) {
  63. return ['code'=>0, 'msg'=>'子栏目不为空, 若要删除请先清空子栏目'];
  64. }
  65. return $this->modelName::del($id);
  66. }
  67. }
  68. }