Menu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller;
  14. use controller\BasicAdmin;
  15. use service\DataService;
  16. use service\NodeService;
  17. use service\ToolsService;
  18. use think\Db;
  19. /**
  20. * 系统后台管理管理
  21. * Class Menu
  22. * @package app\admin\controller
  23. * @author Anyon <zoujingli@qq.com>
  24. * @date 2017/02/15
  25. */
  26. class Menu extends BasicAdmin {
  27. /**
  28. * 绑定操作模型
  29. * @var string
  30. */
  31. public $table = 'SystemMenu';
  32. /**
  33. * 菜单列表
  34. */
  35. public function index() {
  36. $this->title = '系统菜单管理';
  37. $db = Db::name($this->table)->order('sort asc,id asc');
  38. return parent::_list($db, false);
  39. }
  40. /**
  41. * 列表数据处理
  42. * @param array $data
  43. */
  44. protected function _index_data_filter(&$data) {
  45. foreach ($data as &$vo) {
  46. ($vo['url'] !== '#') && ($vo['url'] = url($vo['url']));
  47. $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
  48. }
  49. $data = ToolsService::arr2table($data);
  50. }
  51. /**
  52. * 添加菜单
  53. */
  54. public function add() {
  55. return $this->_form($this->table, 'form');
  56. }
  57. /**
  58. * 编辑菜单
  59. */
  60. public function edit() {
  61. return $this->_form($this->table, 'form');
  62. }
  63. /**
  64. * 表单数据前缀方法
  65. * @param array $vo
  66. */
  67. protected function _form_filter(&$vo) {
  68. if ($this->request->isGet()) {
  69. // 上级菜单处理
  70. $_menus = Db::name($this->table)->where('status', '1')->order('sort desc,id desc')->select();
  71. $_menus[] = ['title' => '顶级菜单', 'id' => '0', 'pid' => '-1'];
  72. $menus = ToolsService::arr2table($_menus);
  73. foreach ($menus as $key => &$menu) {
  74. if (substr_count($menu['path'], '-') > 3) {
  75. unset($menus[$key]);
  76. continue;
  77. }
  78. if (isset($vo['pid'])) {
  79. $current_path = "-{$vo['pid']}-{$vo['id']}";
  80. if ($vo['pid'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
  81. unset($menus[$key]);
  82. }
  83. }
  84. }
  85. // 读取系统功能节点
  86. $nodes = NodeService::get();
  87. foreach ($nodes as $key => $_vo) {
  88. if (empty($_vo['is_menu'])) {
  89. unset($nodes[$key]);
  90. }
  91. }
  92. $this->assign('nodes', array_column($nodes, 'node'));
  93. $this->assign('menus', $menus);
  94. }
  95. }
  96. /**
  97. * 删除菜单
  98. */
  99. public function del() {
  100. if (DataService::update($this->table)) {
  101. $this->success("菜单删除成功!", '');
  102. }
  103. $this->error("菜单删除失败,请稍候再试!");
  104. }
  105. /**
  106. * 菜单禁用
  107. */
  108. public function forbid() {
  109. if (DataService::update($this->table)) {
  110. $this->success("菜单禁用成功!", '');
  111. }
  112. $this->error("菜单禁用失败,请稍候再试!");
  113. }
  114. /**
  115. * 菜单禁用
  116. */
  117. public function resume() {
  118. if (DataService::update($this->table)) {
  119. $this->success("菜单启用成功!", '');
  120. }
  121. $this->error("菜单启用失败,请稍候再试!");
  122. }
  123. }