Menu.php 5.2 KB

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