Blogger Information
Blog 70
fans 4
comment 5
visits 104889
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:【商城后台管理系统】部署角色管理,角色添加,菜单权限,删除角色功能
JiaJieChen
Original
1653 people have browsed it

PHP:【商城后台管理系统】部署角色管理,角色添加,菜单权限,删除角色功能

一.角色管理界面

①首页

②角色添加

③角色编辑

④角色删除

二.部署流程

  • 部署流程
    • 后端采用thinkphp6.0框架,角色管理看似简单,但是在菜单权限这一块是很难得,为什么难呢?因为要设计菜单无限级构造树,还要将无限极菜单构造树递归遍历成二级菜单,这样才好将菜单渲染到视图层,在代码块注释中将有这些问题的解决方案。
    • 前端采用Layui框架

HTML 代码块

  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>角色名称</th>
  23. <th>编辑</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. {foreach $data as $v}
  28. <tr>
  29. <td>{$v['id']}</td>
  30. <td>{$v['gid']}</td>
  31. <td>{$v['title']}</td>
  32. <td>
  33. <button class="layui-btn layui-btn-sm " onclick="edit({$v['id']})">编辑</button>
  34. <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="dle({$v['id']})">删除</button>
  35. </td>
  36. </tr>
  37. {/foreach}
  38. </tbody>
  39. </table>
  40. </body>
  41. <script>
  42. $ = layui.jquery;
  43. //编辑角色
  44. function edit(id) {
  45. layer.open({
  46. type:2,
  47. title: '编辑角色',
  48. shadeClose: true,
  49. shade: 0.8,
  50. area: ['500px', '500px'],
  51. content: '/admin/Group/edit?id='+id
  52. });
  53. }
  54. //删除
  55. function dle(id) {
  56. layer.confirm('确定删除吗?', {
  57. btn: ['确定','取消'],
  58. },
  59. function(){
  60. let date = {};
  61. $.post('/admin/Group/dle?id='+id,date,function(res){
  62. if (res.id == 1) {
  63. layer.alert(res.msg,{icon:1});
  64. setTimeout(() => {
  65. window.location.reload();
  66. }, 1000);
  67. }else{
  68. layer.alert(res.msg,{icon:2});
  69. }
  70. },'json')
  71. });
  72. }
  73. //添加角色
  74. function add()
  75. {
  76. layer.open({
  77. type:2,
  78. title: '添加角色',
  79. shadeClose: true,
  80. shade: 0.8,
  81. area: ['500px', '500px'],
  82. content: '/admin/Group/add'
  83. });
  84. }
  85. </script>
  86. </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 Group extends Base
  12. {
  13. //角色列表
  14. public function index()
  15. {
  16. //角色渲染
  17. $data['group'] = Db::table('admin_group')->select()->toArray();
  18. View::assign([
  19. 'data' => $data['group']
  20. ]);
  21. return View('/group/index');
  22. }
  23. //角色添加
  24. public function add()
  25. {
  26. if (Request::isPost()) {
  27. $data['gid'] = Request::post('gid');
  28. $data['title'] = Request::post('title');
  29. $data['rights'] = array_keys(Request::post('rights'));
  30. $data['rights'] = json_encode($data['rights']);
  31. if (empty($data['gid'])) {
  32. exit(json_encode(['id' => 0 ,'msg' => '角色编号不能为空']));
  33. }
  34. if (empty($data['title'])) {
  35. exit(json_encode(['id' => 0 ,'msg' => '角色名称不能为空']));
  36. }
  37. $insert = Db::table('admin_group')->insert($data);
  38. if (!empty($insert)) {
  39. echo json_encode(['id' => 1 ,'msg' => '角色添加成功']);
  40. }else {
  41. echo json_encode(['id' => 0 ,'msg' => '角色添加失败']);
  42. }
  43. }else {
  44. //角色权限菜单视图层渲染
  45. $tmp_menu_list = Db::table('admin_menu')->where('status',0)->select()->toArray();
  46. //将角色权限菜单数组下标换成角色的mid
  47. $menu_list = $this->SetKey($tmp_menu_list,array());
  48. //构造树菜单(无限级)
  49. $menus = $this->buildMenuTree($menu_list);
  50. //递归 构造树菜单(无限级)变成二级菜单
  51. $result = $this->SetMenu($menus,array());
  52. View::assign([
  53. 'data' => $result,
  54. ]);
  55. return View('/group/add');
  56. }
  57. }
  58. //角色编辑
  59. public function edit()
  60. {
  61. if (Request::isPost()) {
  62. $id = Request::post('id');
  63. $data['title'] = Request::post('title');
  64. $data['rights'] = array_keys(Request::post('rights'));
  65. $data['rights'] = json_encode($data['rights']);
  66. if (empty($data['title'])) {
  67. exit(json_encode(['id' => 0 ,'msg' => '角色名称不能为空']));
  68. }
  69. $update = Db::table('admin_group')->where('id',$id)->update($data);
  70. if (!empty($update)) {
  71. echo json_encode(['id' => 1,'msg' => '保存成功']);
  72. }else {
  73. echo json_encode(['id' => 0,'msg' => '保存失败']);
  74. }
  75. }else {
  76. $id = Request::get('id');
  77. $data['group'] = Db::table('admin_group')->where('id',$id)->find();
  78. $data['group']['rights'] = json_decode($data['group']['rights'],true);
  79. //角色权限菜单视图层渲染
  80. $tmp_menu_list = Db::table('admin_menu')->where('status',0)->select()->toArray();
  81. //将角色权限菜单数组下标换成角色的mid
  82. $menu_list = $this->SetKey($tmp_menu_list,array());
  83. //构造树菜单(无限级)
  84. $menus = $this->buildMenuTree($menu_list);
  85. //递归 构造树菜单(无限级)变成二级菜单
  86. $result = $this->SetMenu($menus,array());
  87. View::assign([
  88. 'result' => $result,
  89. 'id' => $id,
  90. 'group' => $data['group'],
  91. 'rights' => $data['group']['rights']
  92. ]);
  93. return View('/group/edit');
  94. }
  95. }
  96. //角色删除
  97. public function dle()
  98. {
  99. $id = Request::param("id");
  100. if (!empty($id)) {
  101. $delete = Db::table("admin_group")->where('id',$id)->delete();
  102. }
  103. if (!empty($delete)) {
  104. echo json_encode(['id' => 1 ,'msg'=>'删除成功']);
  105. }else {
  106. echo json_encode(['id' => 0 ,'msg'=>'删除失败']);
  107. }
  108. }
  109. //将数组下标换成角色的mid
  110. private function SetKey($item,$array)
  111. {
  112. foreach ($item as $v) {
  113. $array[$v['mid']] = $v;
  114. }
  115. return $array;
  116. }
  117. //遍历递归menu菜单
  118. private function SetMenu($item,$array)
  119. {
  120. foreach ($item as $value) {
  121. //如果构造树菜单的子菜单有children 则进行递归 如果没有则定位空数组
  122. $value['children'] = isset($value['children']) ?$this->formatMenus($value['children']) : [];
  123. //剩余则赋值给$result
  124. $array[] = $value;
  125. }
  126. return $array;
  127. }
  128. //构造树菜单(无限级)
  129. private function buildMenuTree($items)
  130. {
  131. $tree = [];
  132. foreach ($items as $item) {
  133. //如果items 的 下标 是$item pid 则 创建新的children子数组存放 $item
  134. if (isset($items[$item['pid']])) {
  135. $items[$item['pid']]['children'][] = &$items[$item['mid']];
  136. }else {
  137. $tree[] = &$items[$item['mid']];
  138. }
  139. }
  140. return $tree;
  141. }
  142. //递归 构造树菜单(无限级)变成二级菜单
  143. private function formatMenus($items,&$res=[])
  144. {
  145. foreach ($items as $item) {
  146. if (!isset($item['children'])) {
  147. //如果没有children 则存放在 $res
  148. $res[] = $item;
  149. }else {
  150. //如果有children 则进行递归处理
  151. $temp = $item['children'];
  152. //释放$item['children']
  153. unset($item['children']);
  154. $res[] = $item;
  155. $this->formatMenus($temp,$res);
  156. }
  157. }
  158. return $res;
  159. }
  160. }
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