SysMenu.php 2.6 KB

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