Blogger Information
Blog 34
fans 1
comment 1
visits 40826
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ThinkkPHP6关于角色管理总结
嘿哈的博客
Original
990 people have browsed it

角色管理总结

效果图

前端代码

遍历权限列表时 name=”menu[$vo.mid]” 利用数组进行存储可能被选中的值并传至服务器

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>角色管理</title>
  5. <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px">
  9. <form class="layui-form">
  10. <input type="hidden" name="gid" value="{$item.gid}">
  11. <div class="layui-form-item">
  12. <label class="layui-form-label">角色名称</label>
  13. <div class="layui-input-inline">
  14. <input type="text" name="title" value="{$item.title}" class="layui-input">
  15. </div>
  16. </div>
  17. <div class="layui-form-item">
  18. <label class="layui-form-label">权限管理</label>
  19. </div>
  20. {volist name="menus" id="vo"}
  21. <hr>
  22. <div class="layui-input-block">
  23. <input type="checkbox" name="menu[{$vo.mid}]" lay-skin="primary" title="{$vo.title}" {:isset($item['rights']) && $item['rights'] && in_array($vo.mid,$item['rights'])?'checked':''}>
  24. <hr>
  25. {volist name="vo.children" id="cvo"}
  26. <input type="checkbox" name="menu[{$cvo.mid}]" lay-skin="primary" title="{$cvo.title}" {:isset($item['rights']) && $item['rights'] && in_array($cvo.mid,$item['rights'])?'checked':''}>
  27. {/volist}
  28. </div>
  29. {/volist}
  30. </form>
  31. <div class="layui-form-item" style="margin-top: 10px;">
  32. <div class="layui-input-block">
  33. <button class="layui-btn " onclick="save()">保存</button>
  34. </div>
  35. </div>
  36. </body>
  37. </html>
  38. <script type="text/javascript">
  39. layui.use(['layer','form'],function(){
  40. var form = layui.form;
  41. layer = layui.layer;
  42. $ = layui.jquery;
  43. });
  44. function save(){
  45. var title = $.trim($('input[name="title"]').val());
  46. if(title==''){
  47. layer.msg('请填写角色名称',{'icon':2});
  48. return;
  49. }
  50. $.post('/admins/roles/save',$('form').serialize(),function(res){
  51. if(res.code>0){
  52. layer.msg(res.msg,{'icon':2});
  53. }else{
  54. layer.msg(res.msg,{'icon':1});
  55. setTimeout(function(){parent.window.location.reload();},1000);
  56. }
  57. },'json');
  58. }
  59. </script>

服务器端代码

  1. <?php
  2. namespace app\admins\controller;
  3. use app\BaseController;
  4. use think\facade\View;
  5. use think\facade\Db;
  6. /**
  7. * 角色管理
  8. */
  9. class Roles extends Bases
  10. {
  11. //角色列表
  12. public function index()
  13. {
  14. //查询角色列表
  15. $data['item'] = Db::table('admin_groups')->lists();
  16. return view('',$data); //渲染
  17. }
  18. //添加编辑角色
  19. public function add()
  20. {
  21. $gid = (int)input('get.gid'); //get获取url中gid的值
  22. $item = Db::table('admin_groups')->where('gid',$gid)->item();//查询对应gid数据
  23. //逻辑与 判断两者是否存在 条件成立 将字符串转换成数组
  24. $item && $item['rights'] && $item['rights'] = json_decode($item['rights']);
  25. //if($role && $role['rights']){$role['rights'] = json_decode($role['rights']);}
  26. $data['item'] =$item;
  27. //查询所有菜单列表 并将下标更换与mid等值 cates是自己封装的 下方有解释
  28. $menu_lists = Db::table('admin_menus')->where('status',0)->cates('mid');
  29. //进行树状处理 组成无限级菜单 gettreeitems是自己封装的 下方有解释
  30. $menus = $this->gettreeitems($menu_lists);
  31. //创建一个空数组
  32. $temp = array();
  33. foreach($menus as $value){
  34. //当数据中有children时,进行递归处理,转换成二级菜单
  35. $value['children'] = isset($value['children'])?$this->formatMenus($value['children']):false;
  36. //将转换的新数据赋值给空数组
  37. $temp[] = $value;
  38. }
  39. $data['menus'] = $temp;
  40. // dump($data['menus']);
  41. return view('',$data);
  42. }
  43. //保存
  44. public function save()
  45. {
  46. $gid = (int)input('post.gid');
  47. $title = trim(input('post.title'));
  48. $menus = input('post.menu');
  49. $menus = $menus?$menus:[];
  50. if($gid>0){
  51. Db::table('admin_groups')->where('gid',$gid)->update(array('title'=>$title,'rights'=>json_encode(array_keys($menus))));
  52. }else{
  53. Db::table('admin_groups')->insert(array('title'=>$title,'rights'=>json_encode(array_keys($menus))));
  54. }
  55. exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
  56. }
  57. //树状处理
  58. /*
  59. ** 创建新的数组 传进的数据进行遍历 该数据下标已进行处理,与mid一致 mid与pid进行父子关系绑定
  60. ** 当父级pid为0时,为最高级;
  61. ** 当遍历到某条数据pid不为0时,则在该下标为pid这条数据中 增加一个下标children
  62. ** 并将这条数据 添加进下标为该pid的数据中children中去
  63. */
  64. private function gettreeitems($items)
  65. {
  66. $tree = array();
  67. foreach ($items as $item) {
  68. if(isset($items[$item['pid']])){
  69. $items[$item['pid']]['children'][] = &$items[$item['mid']];
  70. }else{
  71. $tree[] = &$items[$item['mid']];
  72. }
  73. }
  74. return $tree;
  75. }
  76. // 递归无限级菜单,使其转换为二级菜单
  77. /*
  78. ** 创建一个空数组 当传进的数组 没有children下标这一数据 存进res空数组中
  79. ** 当某个数据有children下标时 将下标中里的数据存进 tem新的数组中
  80. ** 销毁下标后的数据存进res空数组中 tem中的数组在检测是否有children
  81. ** 有的话按157 158行文字处理 没有的话按156行文字处理 然后返回res数组
  82. */
  83. private function formatMenus($items,&$res = array()){
  84. foreach($items as $item){
  85. if(!isset($item['children'])){
  86. $res[] = $item;
  87. }else{
  88. $tem = $item['children'];
  89. unset($item['children']);
  90. $res[] = $item;
  91. $this->formatMenus($tem,$res);
  92. }
  93. }
  94. return $res;
  95. }
  96. }

树状处理

  1. //树状处理 需要指针 转换成无限级菜单
  2. private function gettreeitems($items)
  3. {
  4. $tree = array();
  5. foreach ($items as $item) {
  6. if(isset($items[$item['pid']])){
  7. $items[$item['pid']]['children'][] = &$items[$item['mid']];
  8. }else{
  9. $tree[] = &$items[$item['mid']];
  10. }
  11. }
  12. return $tree;
  13. }

递归无限级菜单,转换为二级菜单

  1. // 递归无限级菜单,使其转换为二级菜单
  2. private function formatMenus($items,&$res = array()){
  3. foreach($items as $item){
  4. if(!isset($item['children'])){
  5. $res[] = $item;
  6. }else{
  7. $tem = $item['children'];
  8. unset($item['children']);
  9. $res[] = $item;
  10. $this->formatMenus($tem,$res);
  11. }
  12. }
  13. return $res;
  14. }
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