Menu.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use library\Controller;
  15. use library\tools\Data;
  16. use think\Db;
  17. /**
  18. * 系统菜单管理
  19. * Class Menu
  20. * @package app\admin\controller
  21. */
  22. class Menu extends Controller
  23. {
  24. /**
  25. * 当前操作数据库
  26. * @var string
  27. */
  28. protected $table = 'SystemMenu';
  29. /**
  30. * 系统菜单管理
  31. */
  32. public function index()
  33. {
  34. $this->title = '系统菜单管理';
  35. $this->_page($this->table, false);
  36. }
  37. /**
  38. * 列表数据处理
  39. * @param array $data
  40. */
  41. protected function _index_page_filter(&$data)
  42. {
  43. foreach ($data as &$vo) {
  44. if ($vo['url'] !== '#') {
  45. $vo['url'] = url($vo['url']) . (empty($vo['params']) ? '' : "?{$vo['params']}");
  46. }
  47. $vo['ids'] = join(',', Data::getArrSubIds($data, $vo['id']));
  48. }
  49. $data = Data::arr2table($data);
  50. }
  51. /**
  52. * 添加系统菜单
  53. */
  54. public function add()
  55. {
  56. $this->applyCsrfToken();
  57. $this->_form($this->table, 'form');
  58. }
  59. /**
  60. * 编辑系统菜单
  61. */
  62. public function edit()
  63. {
  64. $this->applyCsrfToken();
  65. $this->_form($this->table, 'form');
  66. }
  67. /**
  68. * 表单数据处理
  69. * @param array $vo
  70. * @throws \ReflectionException
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. protected function _form_filter(&$vo)
  76. {
  77. if ($this->request->isGet()) {
  78. // 上级菜单处理
  79. $_menus = Db::name($this->table)->where(['status' => '1'])->order('sort asc,id asc')->select();
  80. $_menus[] = ['title' => '顶级菜单', 'id' => '0', 'pid' => '-1'];
  81. $menus = Data::arr2table($_menus);
  82. foreach ($menus as $key => &$menu) if (substr_count($menu['path'], '-') > 3) unset($menus[$key]); # 移除三级以下的菜单
  83. elseif (isset($vo['pid']) && $vo['pid'] !== '' && $cur = "-{$vo['pid']}-{$vo['id']}")
  84. if (stripos("{$menu['path']}-", "{$cur}-") !== false || $menu['path'] === $cur) unset($menus[$key]); # 移除与自己相关联的菜单
  85. // 选择自己的上级菜单
  86. if (!isset($vo['pid']) && $this->request->get('pid', '0')) $vo['pid'] = $this->request->get('pid', '0');
  87. // 读取系统功能节点
  88. $nodes = \app\admin\service\AuthService::get();
  89. foreach ($nodes as $key => $node) {
  90. if (empty($node['is_menu'])) unset($nodes[$key]);
  91. unset($nodes[$key]['pnode'], $nodes[$key]['is_login'], $nodes[$key]['is_menu'], $nodes[$key]['is_auth']);
  92. }
  93. list($this->menus, $this->nodes) = [$menus, array_values($nodes)];
  94. }
  95. }
  96. /**
  97. * 启用系统菜单
  98. */
  99. public function resume()
  100. {
  101. $this->applyCsrfToken();
  102. $this->_save($this->table, ['status' => '1']);
  103. }
  104. /**
  105. * 禁用系统菜单
  106. */
  107. public function forbid()
  108. {
  109. $this->applyCsrfToken();
  110. $this->_save($this->table, ['status' => '0']);
  111. }
  112. /**
  113. * 删除系统菜单
  114. */
  115. public function remove()
  116. {
  117. $this->applyCsrfToken();
  118. $this->_delete($this->table);
  119. }
  120. }