Menu.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\admin\controller;
  3. use think\admin\Controller;
  4. use think\admin\extend\DataExtend;
  5. use think\admin\service\AdminService;
  6. use think\admin\service\MenuService;
  7. use think\admin\service\NodeService;
  8. /**
  9. * 系统菜单管理
  10. * Class Menu
  11. * @package app\admin\controller
  12. */
  13. class Menu extends Controller
  14. {
  15. /**
  16. * 当前操作数据库
  17. * @var string
  18. */
  19. private $table = 'SystemMenu';
  20. /**
  21. * 系统菜单管理
  22. * @auth true
  23. * @menu true
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function index()
  29. {
  30. $this->title = '系统菜单管理';
  31. $this->type = input('type', 'index');
  32. $this->_query($this->table)->order('sort desc,id asc')->page(false, true);
  33. }
  34. /**
  35. * 列表数据处理
  36. * @param array $data
  37. */
  38. protected function _index_page_filter(array &$data)
  39. {
  40. $data = DataExtend::arr2tree($data);
  41. // 回收站过滤有效菜单
  42. if ($this->type === 'recycle') foreach ($data as $k1 => &$p1) {
  43. if (!empty($p1['sub'])) foreach ($p1['sub'] as $k2 => &$p2) {
  44. if (!empty($p2['sub'])) foreach ($p2['sub'] as $k3 => $p3) {
  45. if ($p3['status'] > 0) unset($p2['sub'][$k3]);
  46. }
  47. if (empty($p2['sub']) && ($p2['url'] === '#' or $p1['status'] > 0)) unset($p1['sub'][$k2]);
  48. }
  49. if (empty($p1['sub']) && ($p1['url'] === '#' or $p1['status'] > 0)) unset($data[$k1]);
  50. }
  51. // 菜单数据树数据变平化
  52. $data = DataExtend::arr2table($data);
  53. foreach ($data as &$vo) {
  54. if ($vo['url'] !== '#' && !preg_match('#^https?://#', $vo['url'])) {
  55. $vo['url'] = trim(url($vo['url']) . ($vo['params'] ? "?{$vo['params']}" : ''), '\\/');
  56. }
  57. $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
  58. }
  59. }
  60. /**
  61. * 添加系统菜单
  62. * @auth true
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function add()
  68. {
  69. $this->_applyFormToken();
  70. $this->_form($this->table, 'form');
  71. }
  72. /**
  73. * 编辑系统菜单
  74. * @auth true
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function edit()
  80. {
  81. $this->_applyFormToken();
  82. $this->_form($this->table, 'form');
  83. }
  84. /**
  85. * 表单数据处理
  86. * @param array $vo
  87. * @throws \ReflectionException
  88. */
  89. protected function _form_filter(array &$vo)
  90. {
  91. if ($this->request->isGet()) {
  92. /* 清理权限节点 */
  93. if ($this->app->isDebug()) {
  94. AdminService::instance()->clearCache();
  95. }
  96. /* 选择自己的上级菜单 */
  97. $vo['pid'] = $vo['pid'] ?? input('pid', '0');
  98. /* 读取系统功能节点 */
  99. $this->auths = [];
  100. $this->nodes = MenuService::instance()->getList();
  101. foreach (NodeService::instance()->getMethods() as $node => $item) {
  102. if ($item['isauth'] && substr_count($node, '/') >= 2) {
  103. $this->auths[] = ['node' => $node, 'title' => $item['title']];
  104. }
  105. }
  106. /* 列出可选上级菜单 */
  107. $menus = $this->app->db->name($this->table)->order('sort desc,id asc')->column('id,pid,icon,url,node,title,params', 'id');
  108. $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'url' => '#', 'title' => '顶部菜单']]));
  109. if (isset($vo['id'])) foreach ($this->menus as $menu) if ($menu['id'] === $vo['id']) $vo = $menu;
  110. foreach ($this->menus as $key => $menu) if ($menu['spt'] >= 3 || $menu['url'] !== '#') unset($this->menus[$key]);
  111. if (isset($vo['spt']) && isset($vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) {
  112. foreach ($this->menus as $key => $menu) if ($vo['spt'] <= $menu['spt']) unset($this->menus[$key]);
  113. }
  114. }
  115. }
  116. /**
  117. * 修改菜单状态
  118. * @auth true
  119. * @throws \think\db\exception\DbException
  120. */
  121. public function state()
  122. {
  123. $this->_applyFormToken();
  124. $this->_save($this->table, $this->_vali([
  125. 'status.in:0,1' => '状态值范围异常!',
  126. 'status.require' => '状态值不能为空!',
  127. ]));
  128. }
  129. /**
  130. * 删除系统菜单
  131. * @auth true
  132. * @throws \think\db\exception\DbException
  133. */
  134. public function remove()
  135. {
  136. $this->_applyFormToken();
  137. $this->_delete($this->table);
  138. }
  139. /**
  140. * 表单结果处理
  141. * @param bool $result
  142. */
  143. protected function _add_form_result(bool $result)
  144. {
  145. if ($result) {
  146. $id = $this->app->db->name($this->table)->getLastInsID();
  147. sysoplog('系统菜单管理', "添加系统菜单[{$id}]成功");
  148. $this->success('系统菜单添加成功!', 'javascript:location.reload()');
  149. }
  150. }
  151. /**
  152. * 表单结果处理
  153. * @param boolean $result
  154. */
  155. protected function _edit_form_result(bool $result)
  156. {
  157. if ($result) {
  158. $id = input('id') ?: 0;
  159. sysoplog('系统菜单管理', "修改系统菜单[{$id}]成功");
  160. $this->success('系统菜单修改成功!', 'javascript:location.reload()');
  161. }
  162. }
  163. /**
  164. * 状态结果处理
  165. * @param boolean $result
  166. */
  167. protected function _state_save_result(bool $result)
  168. {
  169. if ($result) {
  170. [$id, $state] = [input('id'), input('status')];
  171. sysoplog('系统菜单管理', ($state ? '激活' : '禁用') . "系统菜单[{$id}]成功");
  172. $this->success('系统菜单修改成功!', 'javascript:location.reload()');
  173. }
  174. }
  175. /**
  176. * 删除结果处理
  177. * @param boolean $result
  178. */
  179. protected function _remove_delete_result(bool $result)
  180. {
  181. if ($result) {
  182. $id = input('id') ?: 0;
  183. sysoplog('系统菜单管理', "删除系统菜单[{$id}]成功");
  184. $this->success('系统菜单删除成功!', 'javascript:location.reload()');
  185. }
  186. }
  187. }