亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 70
粉絲 4
評論 5
訪問量 121971
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP:【商城后臺管理系統(tǒng)】部署管理員一級菜單和二級菜單列表添加功能
JiaJieChen
原創(chuàng)
2102人瀏覽過

PHP:【商城后臺管理系統(tǒng)】部署管理員一級菜單和二級菜單列表添加功能

一.1級菜單和2級菜單列表添加頁面

  • 一級菜單

  • 二級菜單

二.部署流程

  1. 數(shù)據(jù)庫后臺菜單列表,字段設(shè)置,pid 是 菜單列表級別,0代表1級菜單,>0的是二級菜單,并且二級菜單pid字段值需要對應(yīng)mid字段值,這樣才可以相互綁定。每個(gè)菜單對應(yīng)的還有Controller控制器,action方法,都要一一記錄,這樣才能映射到thinkphp控制器層還有視圖層,用來區(qū)分每一級菜單的前端視圖
  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>狀態(tài)</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 ? '開啟' : '關(guān)閉'}
  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>狀態(tài)</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 ? '開啟' : '關(guān)閉'}
  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. //當(dāng)前角色信息
  39. $admin = $this->admin;
  40. $MyGroup = $this->MyGroup;
  41. if (!empty($data)) {
  42. $insert = Db::table('admin_menu')->insert($data);
  43. //插入的最大值行數(shù)
  44. $count = (int)Db::table('admin_menu')->max('mid');
  45. //再次插入到角色權(quán)限中
  46. array_push($MyGroup['rights'],$count);
  47. $MyGroup['rights'] = json_encode($MyGroup['rights']);
  48. //更新角色權(quán)限
  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' => '輸入的信息錯(cuò)誤']));
  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' => '輸入的信息錯(cuò)誤']));
  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. //當(dāng)前角色信息
  118. $admin = $this->admin;
  119. $MyGroup = $this->MyGroup;
  120. //刪除角色對應(yīng)的菜單權(quán)限
  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. //更新角色權(quán)限
  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. }
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)