Menu.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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('get.type', 'index');
  41. SystemMenu::mQuery()->layTable();
  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 $p2['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. }
  67. }
  68. /**
  69. * 添加系统菜单
  70. * @auth true
  71. */
  72. public function add()
  73. {
  74. $this->_applyFormToken();
  75. SystemMenu::mForm('form');
  76. }
  77. /**
  78. * 编辑系统菜单
  79. * @auth true
  80. */
  81. public function edit()
  82. {
  83. $this->_applyFormToken();
  84. SystemMenu::mForm('form');
  85. }
  86. /**
  87. * 表单数据处理
  88. * @param array $vo
  89. * @throws \ReflectionException
  90. */
  91. protected function _form_filter(array &$vo)
  92. {
  93. if ($this->request->isGet()) {
  94. /* 清理权限节点 */
  95. if ($isDebug = $this->app->isDebug()) {
  96. AdminService::clearCache();
  97. }
  98. /* 读取系统功能节点 */
  99. $this->auths = [];
  100. $this->nodes = MenuService::getList($isDebug);
  101. foreach (NodeService::getMethods($isDebug) as $node => $item) {
  102. if ($item['isauth'] && substr_count($node, '/') >= 2) {
  103. $this->auths[] = ['node' => $node, 'title' => $item['title']];
  104. }
  105. }
  106. /* 选择自己上级菜单 */
  107. $vo['pid'] = $vo['pid'] ?? input('pid', '0');
  108. /* 列出可选上级菜单 */
  109. $menus = SystemMenu::mk()->order('sort desc,id asc')->column('id,pid,icon,url,node,title,params', 'id');
  110. $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'url' => '#', 'title' => '顶部菜单']]));
  111. if (isset($vo['id'])) foreach ($this->menus as $menu) if ($menu['id'] === $vo['id']) $vo = $menu;
  112. foreach ($this->menus as $key => $menu) if ($menu['spt'] >= 3 || $menu['url'] !== '#') unset($this->menus[$key]);
  113. if (isset($vo['spt']) && isset($vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) {
  114. foreach ($this->menus as $key => $menu) if ($vo['spt'] <= $menu['spt']) unset($this->menus[$key]);
  115. }
  116. }
  117. }
  118. /**
  119. * 修改菜单状态
  120. * @auth true
  121. */
  122. public function state()
  123. {
  124. SystemMenu::mSave($this->_vali([
  125. 'status.in:0,1' => '状态值范围异常!',
  126. 'status.require' => '状态值不能为空!',
  127. ]));
  128. }
  129. /**
  130. * 删除系统菜单
  131. * @auth true
  132. */
  133. public function remove()
  134. {
  135. SystemMenu::mDelete();
  136. }
  137. }