Menu.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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\MenuService;
  18. /**
  19. * 系统菜单管理
  20. * Class Menu
  21. * @package app\admin\controller
  22. */
  23. class Menu extends Controller
  24. {
  25. /**
  26. * 当前操作数据库
  27. * @var string
  28. */
  29. protected $table = 'SystemMenu';
  30. /**
  31. * 系统菜单管理
  32. * @auth true
  33. * @menu true
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function index()
  39. {
  40. $this->title = '系统菜单管理';
  41. $this->_page($this->table, false);
  42. }
  43. /**
  44. * 列表数据处理
  45. * @param array $data
  46. */
  47. protected function _index_page_filter(&$data)
  48. {
  49. foreach ($data as &$vo) {
  50. if ($vo['url'] !== '#') {
  51. $vo['url'] = url($vo['url']) . (empty($vo['params']) ? '' : "?{$vo['params']}");
  52. }
  53. $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
  54. }
  55. $data = DataExtend::arr2table($data);
  56. }
  57. /**
  58. * 添加系统菜单
  59. * @auth true
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function add()
  65. {
  66. $this->_applyFormToken();
  67. $this->_form($this->table, 'form');
  68. }
  69. /**
  70. * 编辑系统菜单
  71. * @auth true
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function edit()
  77. {
  78. $this->_applyFormToken();
  79. $this->_form($this->table, 'form');
  80. }
  81. /**
  82. * 表单数据处理
  83. * @param array $vo
  84. * @throws \ReflectionException
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. protected function _form_filter(&$vo)
  90. {
  91. if ($this->request->isGet()) {
  92. // 选择自己的上级菜单
  93. if (empty($vo['pid']) && $this->request->get('pid', '0')) {
  94. $vo['pid'] = $this->request->get('pid', '0');
  95. }
  96. // 读取系统功能节点
  97. $this->nodes = MenuService::instance()->getList();
  98. // 列出可选上级菜单
  99. $menus = $this->app->db->name($this->table)->where(['status' => '1'])->order('sort desc,id asc')->select()->toArray();
  100. $menus[] = ['title' => '顶级菜单', 'id' => '0', 'pid' => '-1'];
  101. foreach ($this->menus = DataExtend::arr2table($menus) as $key => &$menu) {
  102. if (substr_count($menu['path'], '-') > 3) unset($this->menus[$key]); # 移除三级以下的菜单
  103. elseif (isset($vo['pid']) && $vo['pid'] !== '' && $cur = "-{$vo['pid']}-{$vo['id']}") {
  104. if (stripos("{$menu['path']}-", "{$cur}-") !== false || $menu['path'] === $cur) unset($this->menus[$key]); # 移除与自己相关联的菜单
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * 修改菜单状态
  111. * @auth true
  112. * @throws \think\db\exception\DbException
  113. */
  114. public function state()
  115. {
  116. $this->_applyFormToken();
  117. $this->_save($this->table, ['status' => intval(input('status'))]);
  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. }