Menu.php 7.2 KB

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