Blogger Information
Blog 70
fans 4
comment 5
visits 104851
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:【商城后台管理系统】部署管理员一级菜单和二级菜单列表添加功能
JiaJieChen
Original
1786 people have browsed it

PHP:【商城后台管理系统】部署管理员一级菜单和二级菜单列表添加功能

一.1级菜单和2级菜单列表添加页面

  • 一级菜单

  • 二级菜单

二.部署流程

  1. 数据库后台菜单列表,字段设置,pid 是 菜单列表级别,0代表1级菜单,>0的是二级菜单,并且二级菜单pid字段值需要对应mid字段值,这样才可以相互绑定。每个菜单对应的还有Controller控制器,action方法,都要一一记录,这样才能映射到thinkphp控制器层还有视图层,用来区分每一级菜单的前端视图
  2. 框架采用ThinkPHP6.0

HTML 1级菜单 代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>菜单列表管理</title>
  8. <link rel="stylesheet" href="/static/layui-v2.6.8/layui/css/layui.css" />
  9. <script src="/static/layui-v2.6.8/layui/layui.js"></script>
  10. </head>
  11. <body style="padding: 10px;min-width:737px">
  12. <div class="layui-item" >
  13. <span style="color:#777;font-size:20px;height:30px;line-height:30px">首页/</span>
  14. <span style="color:#ccc">菜单列表</span>
  15. <button class="layui-btn layui-btn-sm" style="float:right;margin:5px" onclick="add()">添加</button>
  16. </div>
  17. <table class="layui-table" >
  18. <thead>
  19. <tr>
  20. <th>ID</th>
  21. <th>菜单名称</th>
  22. <th>controller</th>
  23. <th>action</th>
  24. <th>是否隐藏</th>
  25. <th>状态</th>
  26. <th>编辑</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. {foreach $data as $menu}
  31. <tr>
  32. <td>{$menu['mid']}</td>
  33. <td>{$menu['title']}</td>
  34. <td>{$menu['controller']}</td>
  35. <td>{$menu['action']}</td>
  36. <td style="color:{$menu['ishidden']==0 ? 'green' : 'red'};">
  37. {$menu['ishidden']==0 ? '不隐藏' : '隐藏'}
  38. </td>
  39. <td style="color:{$menu['status']==0 ? 'green' : 'red'};">
  40. {$menu['status']==0 ? '开启' : '关闭'}
  41. </td>
  42. <td>
  43. <button class="layui-btn layui-btn-primary layui-btn-xs" onclick="lower({$menu['mid']})">下级菜单</button>
  44. <button class="layui-btn layui-btn-xs" onclick="edit({$menu['mid']})">编辑</button>
  45. <button class="layui-btn layui-btn-danger layui-btn-xs"onclick="dle({$menu['mid']})">删除</button>
  46. </td>
  47. </tr>
  48. {/foreach}
  49. </tbody>
  50. </table>
  51. </body>
  52. <script>
  53. let $ = layui.jquery;
  54. //编辑菜单
  55. function edit(mid)
  56. {
  57. layer.open({
  58. type:2,
  59. title: '编辑菜单',
  60. shadeClose: true,
  61. shade: 0.8,
  62. area: ['450px', '450px'],
  63. content: '/admin/Menus/edit?mid='+mid
  64. });
  65. }
  66. //删除菜单
  67. function dle(mid)
  68. {
  69. layer.confirm('确定删除吗?', {
  70. btn: ['确定','取消'],
  71. },
  72. function(){
  73. let date = {};
  74. $.post('/admin/Menus/dle?mid='+mid,date,function(res){
  75. if (res.id == 1) {
  76. layer.alert(res.msg,{icon:1});
  77. setTimeout(() => {
  78. window.location.reload();
  79. }, 1000);
  80. }else{
  81. layer.alert(res.msg,{icon:2});
  82. }
  83. },'json')
  84. });
  85. }
  86. //添加菜单
  87. function add()
  88. {
  89. layer.open({
  90. type:2,
  91. title: '添加菜单',
  92. shadeClose: true,
  93. shade: 0.8,
  94. area: ['450px', '450px'],
  95. content: '/admin/Menus/add'
  96. });
  97. }
  98. //下级菜单
  99. function lower(mid)
  100. {
  101. layer.open({
  102. type:2,
  103. title: '下级菜单',
  104. shadeClose: true,
  105. shade: 0.8,
  106. area: ['800px', '600px'],
  107. content: '/admin/Menus/lower?mid='+mid
  108. });
  109. }
  110. </script>
  111. </html>

HTML 2级菜单 代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>二级菜单</title>
  8. <link rel="stylesheet" href="/static/layui-v2.6.8/layui/css/layui.css" />
  9. <script src="/static/layui-v2.6.8/layui/layui.js"></script>
  10. </head>
  11. <body style="padding: 10px;">
  12. <div class="layui-item" >
  13. <span style="color:#777;font-size:20px;height:30px;line-height:30px">首页/</span>
  14. <span style="color:#ccc">二级菜单列表</span>
  15. <button class="layui-btn layui-btn-sm" style="float:right;margin:5px" onclick="add({$data[0]['pid']})">添加</button>
  16. </div>
  17. <table class="layui-table" >
  18. <thead>
  19. <tr>
  20. <th>ID</th>
  21. <th>菜单名称</th>
  22. <th>controller</th>
  23. <th>action</th>
  24. <th>是否隐藏</th>
  25. <th>状态</th>
  26. <th>编辑</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. {foreach $data as $menu}
  31. <tr>
  32. <td>{$menu['mid']}</td>
  33. <td>{$menu['title']}</td>
  34. <td>{$menu['controller']}</td>
  35. <td>{$menu['action']}</td>
  36. <td style="color:{$menu['ishidden']==0 ? 'green' : 'red'};">
  37. {$menu['ishidden']==0 ? '不隐藏' : '隐藏'}
  38. </td>
  39. <td style="color:{$menu['status']==0 ? 'green' : 'red'};">
  40. {$menu['status']==0 ? '开启' : '关闭'}
  41. </td>
  42. <td>
  43. <button class="layui-btn layui-btn-xs" onclick="edit({$menu['mid']})">编辑</button>
  44. <button class="layui-btn layui-btn-danger layui-btn-xs"onclick="dle({$menu['mid']})">删除</button>
  45. </td>
  46. </tr>
  47. {/foreach}
  48. </tbody>
  49. </table>
  50. </body>
  51. <script>
  52. let $ = layui.jquery;
  53. //编辑菜单
  54. function edit(mid)
  55. {
  56. layer.open({
  57. type:2,
  58. title: '编辑菜单',
  59. shadeClose: true,
  60. shade: 0.8,
  61. area: ['450px', '450px'],
  62. content: '/admin/Menus/edit?mid='+mid
  63. });
  64. }
  65. //删除菜单
  66. function dle(mid)
  67. {
  68. layer.confirm('确定删除吗?', {
  69. btn: ['确定','取消'],
  70. },
  71. function(){
  72. let date = {};
  73. $.post('/admin/Menus/dle?mid='+mid,date,function(res){
  74. if (res.id == 1) {
  75. layer.alert(res.msg,{icon:1});
  76. setTimeout(() => {
  77. window.location.reload();
  78. }, 1000);
  79. }else{
  80. layer.alert(res.msg,{icon:2});
  81. }
  82. },'json')
  83. });
  84. }
  85. //添加菜单
  86. function add(pid)
  87. {
  88. layer.open({
  89. type:2,
  90. title: '添加菜单',
  91. shadeClose: true,
  92. shade: 0.8,
  93. area: ['450px', '450px'],
  94. content: '/admin/Menus/add?pid='+pid
  95. });
  96. }
  97. </script>
  98. </html>

PHP 代码块

  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\Base;
  4. use think\facade\Request;
  5. use think\facade\Db;
  6. use think\facade\View;
  7. use think\facade\Session;
  8. /**
  9. * 菜单管理
  10. */
  11. class Menus extends Base
  12. {
  13. //菜单列表
  14. public function index()
  15. {
  16. //一级菜单
  17. $data['menuList'] = Db::table('admin_menu')->where('pid',0)->select()->toArray();
  18. // print_r($data);
  19. // die;
  20. View::assign([
  21. 'data' => $data['menuList']
  22. ]);
  23. return View('/menus/index');
  24. }
  25. //菜单添加
  26. public function add()
  27. {
  28. //如果pid 为空 则是一级菜单,如果不为空则是二级菜单
  29. $pid = Request::param('pid');
  30. if (Request::isPost()) {
  31. //菜单信息
  32. $data['pid'] = Request::post('pid');
  33. $data['title'] = Request::post('title');
  34. $data['controller'] = Request::post('controller');
  35. $data['action'] = Request::post('action');
  36. $data['ishidden'] = Request::post('ishidden');
  37. $data['status'] = Request::post('status');
  38. //当前角色信息
  39. $admin = $this->admin;
  40. $MyGroup = $this->MyGroup;
  41. if (!empty($data)) {
  42. $insert = Db::table('admin_menu')->insert($data);
  43. //插入的最大值行数
  44. $count = (int)Db::table('admin_menu')->max('mid');
  45. //再次插入到角色权限中
  46. array_push($MyGroup['rights'],$count);
  47. $MyGroup['rights'] = json_encode($MyGroup['rights']);
  48. //更新角色权限
  49. $mygroup = Db::table('admin_group')->where('gid',$MyGroup['gid'])->update([
  50. 'rights' => $MyGroup['rights']
  51. ]);
  52. }else {
  53. exit(json_encode(['id' => 0 ,'msg' => '输入的信息错误']));
  54. }
  55. if (!empty($insert)) {
  56. echo json_encode(['id' => 1 ,'msg' => '添加成功']);
  57. }else {
  58. echo json_encode(['id' => 0 ,'msg' => '添加失败']);
  59. }
  60. }else {
  61. //菜单信息
  62. $data['menu'] = Db::table('admin_menu')->where('pid',$pid)->find();
  63. View::assign([
  64. 'pid' => $pid,
  65. 'data' => $data['menu']
  66. ]);
  67. return View('/menus/add');
  68. }
  69. }
  70. //下级菜单
  71. public function lower()
  72. {
  73. //下级菜单查询
  74. $mid = Request::param('mid');
  75. $data['MenuList'] = Db::table('admin_menu')->where('pid',$mid)->select()->toArray();
  76. View::assign([
  77. 'data' => $data['MenuList']
  78. ]);
  79. return View('/menus/lower');
  80. }
  81. //菜单修改
  82. public function edit()
  83. {
  84. //菜单信息
  85. if (Request::isPost()) {
  86. $data['mid'] = Request::post('mid');
  87. $data['title'] = Request::post('title');
  88. $data['controller'] = Request::post('controller');
  89. $data['action'] = Request::post('action');
  90. $data['ishidden'] = Request::post('ishidden');
  91. $data['status'] = Request::post('status');
  92. if (!empty($data)) {
  93. $update = Db::table('admin_menu')->where('mid',$data['mid'])->update($data);
  94. }else {
  95. exit(json_encode(['id' => 0 ,'msg' => '输入的信息错误']));
  96. }
  97. if (!empty($update)) {
  98. echo json_encode(['id' => 1 ,'msg' => '修改成功']);
  99. }else {
  100. echo json_encode(['id' => 0 ,'msg' => '修改失败']);
  101. }
  102. }else {
  103. //菜单id
  104. $mid = Request::get('mid');
  105. //菜单信息
  106. $data['menu'] = Db::table('admin_menu')->where('mid',$mid)->find();
  107. View::assign([
  108. 'data' => $data['menu']
  109. ]);
  110. return View('/menus/edit');
  111. }
  112. }
  113. //菜单删除
  114. public function dle ()
  115. {
  116. $mid = (int)Request::param('mid');
  117. //当前角色信息
  118. $admin = $this->admin;
  119. $MyGroup = $this->MyGroup;
  120. //删除角色对应的菜单权限
  121. $GroupKey = (int)array_search($mid,$MyGroup['rights'],true);
  122. array_splice($MyGroup['rights'],$GroupKey,1);
  123. $MyGroup['rights'] = json_encode($MyGroup['rights']);
  124. if (!empty($mid)) {
  125. $delete = Db::table('admin_menu')->where('mid',$mid)->delete();
  126. //更新角色权限
  127. $mygroup = Db::table('admin_group')->where('gid',$MyGroup['gid'])->update([
  128. 'rights' => $MyGroup['rights']
  129. ]);
  130. }
  131. if (!empty($delete)) {
  132. echo json_encode(['id' => 1 ,'msg' => '删除成功']);
  133. }else {
  134. echo json_encode(['id' => 0 ,'msg' => '删除失败']);
  135. }
  136. }
  137. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post