Menu.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\base\Permissions;
  4. use app\admin\model\AdminMenu as menuModel;
  5. use think\Db;
  6. class Menu extends Permissions
  7. {
  8. public function index()
  9. {
  10. $model = new menuModel();
  11. if ($this->request->isAjax()) {
  12. $data = $model->order('orders asc')->select();
  13. foreach ($data as $k => $v) {
  14. $v['type_text'] = $v->type == 1 ? "权限节点" : "普通节点";
  15. $v['is_display_text'] = $v->is_display == 1 ? "显示在左侧菜单" : "操作节点";
  16. $data[$k] = $v;
  17. }
  18. return $data;
  19. } else {
  20. $menus = $model->order('orders asc')->select();
  21. $menus_all = $model->menulist($menus);
  22. $this->assign('menus', $menus_all);
  23. $this->assign('noInsertRoutes', $this->noInsertRoutes());
  24. return $this->fetch();
  25. }
  26. }
  27. public function publish()
  28. {
  29. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  30. $model = new menuModel();
  31. $post = $this->request->post();
  32. if ($this->request->isPost()) {
  33. $validate = new \think\Validate([
  34. ['name', 'require', '菜单名称不能为空'],
  35. ['pid', 'require', '请选择上级菜单'],
  36. // ['module', 'require', '请填写模块名称'],
  37. // ['controller', 'require', '请填写控制器名称'],
  38. // ['function', 'require', '请填写方法名称'],
  39. ['type', 'require', '请选择菜单类型'],
  40. ]);
  41. if (!$validate->check($post)) {
  42. $this->error('提交失败:' . $validate->getError());
  43. }
  44. }
  45. if ($id > 0) {
  46. if ($this->request->isPost()) {
  47. if ($id == $post['pid']) {
  48. $this->error('不能选自己当上级分类');
  49. }
  50. $menu = $model->where('id', $id)->find();
  51. if (empty($menu)) {
  52. $this->error('id不正确');
  53. }
  54. //如果关闭默认展开,给默认值0
  55. if (empty($post['is_open'])) {
  56. $post['is_open'] = 0;
  57. }
  58. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  59. $this->error('修改失败');
  60. } else {
  61. $this->success('修改菜单信息成功', 'admin/menu/index');
  62. }
  63. } else {
  64. $menu = $model->where('id', $id)->find();
  65. $menus = $model->order('orders asc')->select();
  66. $menus_all = $model->menulist($menus);
  67. $this->assign('menus', $menus_all);
  68. if (!empty($menu)) {
  69. $this->assign('menu', $menu);
  70. return $this->fetch();
  71. } else {
  72. $this->error('id不正确');
  73. }
  74. }
  75. } else {
  76. if ($this->request->isPost()) {
  77. if (false == $model->allowField(true)->save($post)) {
  78. $this->error('添加菜单失败');
  79. } else {
  80. $this->success('添加菜单成功', 'admin/menu/index');
  81. }
  82. } else {
  83. $pid = $this->request->has('pid') ? $this->request->param('pid', null, 'intval') : null;
  84. if (!empty($pid)) {
  85. $this->assign('pid', $pid);
  86. }
  87. $menu = $model->order('orders asc')->select();
  88. $menus = $model->menulist($menu);
  89. $this->assign('menus', $menus);
  90. return $this->fetch();
  91. }
  92. }
  93. }
  94. public function delete()
  95. {
  96. if ($this->request->isAjax()) {
  97. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  98. if (Db::name('admin_menu')->where('pid', $id)->select() == null) {
  99. if (false == Db::name('admin_menu')->where('id', $id)->delete()) {
  100. $this->error('删除失败');
  101. } else {
  102. $this->success('删除成功', 'admin/menu/index');
  103. }
  104. } else {
  105. $this->error('该菜单下还有子菜单,不能删除');
  106. }
  107. }
  108. }
  109. public function orders()
  110. {
  111. if ($this->request->isPost()) {
  112. $post = $this->request->post();
  113. $i = 0;
  114. foreach ($post['id'] as $k => $val) {
  115. $order = Db::name('admin_menu')->where('id', $val)->value('orders');
  116. if ($order != $post['orders'][$k]) {
  117. if (false == Db::name('admin_menu')->where('id', $val)->update(['orders' => $post['orders'][$k]])) {
  118. $this->error('更新失败');
  119. } else {
  120. $i++;
  121. }
  122. }
  123. }
  124. $this->success('成功更新' . $i . '个数据', 'admin/menu/index');
  125. }
  126. }
  127. /**
  128. * 未添加的路由
  129. * @return array
  130. */
  131. private function noInsertRoutes()
  132. {
  133. $allRoutes = [];
  134. $controllers = getFilenameList(APP_PATH . 'admin' . DS . 'controller' . DS, '*.php');
  135. foreach ($controllers as $controller) {
  136. if ($controller != "Permissions") {
  137. $actions = getActions("\app\admin\controller\\" . $controller, $base = '\app\admin\controller\base\Permissions');
  138. foreach ($actions as $action) {
  139. $allRoutes[] = "admin-$controller-$action";
  140. }
  141. }
  142. }
  143. $allMenus = (new menuModel())->field("concat(module,'-',controller,'-',function) as route")->where('module', '<>', '')->select();
  144. $allMenusLowCase = [];
  145. foreach ($allMenus as $menu) {
  146. $allMenusLowCase[] = strtolower($menu['route']);
  147. }
  148. $noInsertRoutes = [];
  149. foreach ($allRoutes as $route) {
  150. if (!in_array(strtolower($route), $allMenusLowCase)) {
  151. $noInsertRoutes[] = explode('-', $route);
  152. }
  153. }
  154. return $noInsertRoutes;
  155. }
  156. /**
  157. * 批量添加
  158. */
  159. public function batchAdd()
  160. {
  161. $post = $this->request->param();
  162. if (isset($post['name']) && is_array($post['name'])) {
  163. $data = [];
  164. foreach ($post['name'] as $key => $name) {
  165. if (!isset($post['pid'][$key])) {
  166. continue;//没有选择上级节点
  167. }
  168. $data[] = [
  169. 'name' => $name,
  170. 'module' => $post['module'][$key],
  171. 'controller' => $post['controller'][$key],
  172. 'function' => $post['function'][$key],
  173. 'is_display' => $post['is_display'][$key],
  174. 'type' => $post['type'][$key],
  175. 'pid' => $post['pid'][$key],
  176. 'icon' => 'fa-tag',
  177. ];
  178. }
  179. $model = new menuModel();
  180. if (false == $model->allowField(true)->saveAll($data)) {
  181. $this->error('添加菜单失败');
  182. } else {
  183. $this->success('添加菜单成功', 'admin/menu/index');
  184. }
  185. }
  186. $this->error('当前没有路由可添加');
  187. }
  188. }