SysMenu.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\sys\controller;
  3. // 引入框架内置类
  4. use think\facade\View;
  5. // 引入表格和表单构建器
  6. use app\common\facade\MakeBuilder;
  7. // use app\common\builder\FormBuilder;
  8. use app\common\builder\TableBuilder;
  9. use app\admin\model\SysMenu as SysMenuModel;
  10. use think\facade\Request;
  11. class SysMenu extends Base
  12. {
  13. public function index()
  14. {
  15. $list = SysMenuModel::select();
  16. View::assign('list', obj_tree($list));
  17. View::assign('type', ['目录', '菜单', '按钮']);
  18. return View::fetch();
  19. }
  20. public function save($id = 0)
  21. {
  22. if ($this->app->request->isPost()) {
  23. $param = $this->app->request->param();
  24. if ($param['name'] == '') {
  25. $this->error("目录名不能为空");
  26. }
  27. try {
  28. if ($param['id'] != 0) {
  29. SysMenuModel::update([
  30. 'pid' => $param['pid'],
  31. 'name' => $param['name'],
  32. 'url' => $param['url'] ?: null,
  33. 'type' => $param['type'],
  34. 'icon' => $param['icon']
  35. ], ['id'=>$param['id']]);
  36. } else {
  37. SysMenuModel::create([
  38. 'pid' => $param['pid'],
  39. 'name' => $param['name'],
  40. 'url' => $param['url'] ?: null,
  41. 'type' => $param['type'],
  42. 'icon' => $param['icon']
  43. ]);
  44. }
  45. // 删除菜单目录缓存
  46. \think\Facade\Cache::set('menus', null);
  47. } catch (\Exception $e) {
  48. $msg = $e->getMessage();
  49. $this->error("错误提示:".$msg);
  50. }
  51. $this->success('操作成功', 'sys_menu/index');
  52. } else {
  53. if ($id != 0) {
  54. $data = SysMenuModel::find($id);
  55. } else {
  56. $data = [
  57. 'id' => 0,
  58. 'pid' => 0,
  59. 'name' => '',
  60. 'url' => '',
  61. 'type' => 0,
  62. 'icon' =>''
  63. ];
  64. }
  65. View::assign('data', $data);
  66. return View::fetch();
  67. }
  68. }
  69. public function delete($id = null)
  70. {
  71. if ($this->app->request->isAjax()) {
  72. if (SysMenuModel::where('pid', $id)->value('id')) {
  73. return ['code'=>0, 'msg'=>'此权限子权限不为空, 若要删除请先清空子权限'];
  74. }
  75. if (SysMenuModel::destroy($id)) {
  76. return ['code' => 1,'msg'=>"删除成功"];
  77. } else {
  78. return ['code' => 0,'msg'=>"删除失败"];
  79. }
  80. }
  81. }
  82. }