Menu.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use think\admin\Controller;
  16. use think\admin\extend\DataExtend;
  17. use think\admin\service\AdminService;
  18. use think\admin\service\MenuService;
  19. /**
  20. * 系统菜单管理
  21. * Class Menu
  22. * @package app\admin\controller
  23. */
  24. class Menu extends Controller
  25. {
  26. /**
  27. * 当前操作数据库
  28. * @var string
  29. */
  30. protected $table = 'SystemMenu';
  31. /**
  32. * 系统菜单管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function index()
  40. {
  41. $this->title = '系统菜单管理';
  42. $this->_page($this->table, false);
  43. }
  44. /**
  45. * 列表数据处理
  46. * @param array $data
  47. */
  48. protected function _index_page_filter(&$data)
  49. {
  50. foreach ($data as &$vo) {
  51. if ($vo['url'] !== '#') {
  52. $vo['url'] = trim(url($vo['url']) . (empty($vo['params']) ? '' : "?{$vo['params']}"), '/\\');
  53. }
  54. $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
  55. }
  56. $data = DataExtend::arr2table($data);
  57. }
  58. /**
  59. * 添加系统菜单
  60. * @auth true
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function add()
  66. {
  67. $this->_applyFormToken();
  68. $this->_form($this->table, 'form');
  69. }
  70. /**
  71. * 编辑系统菜单
  72. * @auth true
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function edit()
  78. {
  79. $this->_applyFormToken();
  80. $this->_form($this->table, 'form');
  81. }
  82. /**
  83. * 表单数据处理
  84. * @param array $vo
  85. * @throws \ReflectionException
  86. */
  87. protected function _form_filter(&$vo)
  88. {
  89. if ($this->request->isGet()) {
  90. // 清理权限节点
  91. AdminService::instance()->clearCache();
  92. // 选择自己的上级菜单
  93. $vo['pid'] = $vo['pid'] ?? input('pid', '0');
  94. // 读取系统功能节点
  95. $this->nodes = MenuService::instance()->getList();
  96. // 列出可选上级菜单
  97. $menus = $this->app->db->name($this->table)->order('sort desc,id asc')->column('id,pid,icon,url,title,params', 'id');
  98. $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'url' => '#', 'title' => '顶部菜单']]));
  99. if (isset($vo['id'])) foreach ($this->menus as $key => $menu) if ($menu['id'] === $vo['id']) $vo = $menu;
  100. foreach ($this->menus as $key => $menu) {
  101. if ($menu['spt'] >= 3 || $menu['url'] !== '#') unset($this->menus[$key]);
  102. if (isset($vo['spt']) && $vo['spt'] <= $menu['spt']) unset($this->menus[$key]);
  103. }
  104. }
  105. }
  106. /**
  107. * 修改菜单状态
  108. * @auth true
  109. * @throws \think\db\exception\DbException
  110. */
  111. public function state()
  112. {
  113. $this->_applyFormToken();
  114. $this->_save($this->table, $this->_vali([
  115. 'status.in:0,1' => '状态值范围异常!',
  116. 'status.require' => '状态值不能为空!',
  117. ]));
  118. }
  119. /**
  120. * 删除系统菜单
  121. * @auth true
  122. * @throws \think\db\exception\DbException
  123. */
  124. public function remove()
  125. {
  126. $this->_applyFormToken();
  127. $this->_delete($this->table);
  128. }
  129. }