Blogger Information
Blog 41
fans 0
comment 0
visits 25302
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
后台五 角色管理
自由之上
Original
955 people have browsed it

一、角色列表 模块

1、创建 角色列表 视图模板

2、编辑 角色列表 视图模板


resources/views/admins/groups/index.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>角色列表</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <div style="text-align: right;">
  10. <button class="layui-btn layui-btn-sm" onclick="add()"><i class="layui-icon layui-icon-addition"></i>添加</button>
  11. </div>
  12. <table class="layui-table">
  13. <thead>
  14. <tr>
  15. <th>ID</th>
  16. <th>角色名称</th>
  17. <th>操作</th>
  18. </tr>
  19. </thead>
  20. <tbody>
  21. @foreach($list as $item)
  22. <tr>
  23. <td>{{$item['gid']}}</td>
  24. <td>{{$item['title']}}</td>
  25. <td>
  26. <button class="layui-btn layui-btn-xs" onclick="edit({{$item['gid']}})">修改</button>
  27. </td>
  28. </tr>
  29. @endforeach
  30. </tbody>
  31. </table>
  32. </body>
  33. </html>
  34. <script type="text/javascript">
  35. layui.use(['layer'],function(){
  36. $ = layui.jquery;
  37. layer = layui.layer;
  38. });
  39. // 添加管理员
  40. function add(){
  41. layer.open({
  42. type: 2,
  43. title: '添加角色',
  44. shade: 0.3,
  45. area: ['650px', '90%'],
  46. content: '/admins/groups/add'
  47. });
  48. }
  49. // 修改菜单
  50. function edit(gid){
  51. layer.open({
  52. type: 2,
  53. title: '修改角色',
  54. shade: 0.3,
  55. area: ['650px', '90%'],
  56. content: '/admins/groups/edit?gid='+gid
  57. });
  58. }
  59. </script>

3、创建 角色管理 控制器


app/Http/Controllers/admins/Groups.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. /**
  5. * 角色管理
  6. * @package App\Http\Controllers\admins
  7. */
  8. class Groups extends Controller{
  9. }

4、角色列表 控制器处理

app/Http/Controllers/admins/Groups.php

  1. use Illuminate\Support\Facades\DB;
  2. // 角色列表
  3. public function index() {
  4. $data['list'] = DB::table('admin_group')->lists();
  5. return view('admins/groups/index', $data);
  6. }

5、角色列表 注册路由

routes/web.php

  1. use App\Http\Controllers\admins\Groups;
  2. //显示角色管理列表
  3. Route::get('/admins/Groups/index', [Groups::class, 'index']);

6、查看效果

二、添加与保存角色 模块

1、创建 添加与保存角色 视图模板

2、编辑 添加与保存角色 视图模板


resources/views/admins/groups/add.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>添加角色</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <form class="layui-form">
  10. @csrf
  11. <div class="layui-form-item">
  12. <label class="layui-form-label">角色名称</label>
  13. <div class="layui-input-block">
  14. <input type="text" name="title" class="layui-input">
  15. </div>
  16. </div>
  17. <div class="layui-form-item">
  18. <label class="layui-form-label">菜单</label>
  19. <div class="layui-input-block">
  20. @foreach($menus_list as $menu)
  21. <div>
  22. <input type="checkbox" lay-skin="primary" name="menus[{{$menu['mid']}}]"
  23. title="<span style='font-weight:bold'>{{$menu['title']}}</span>">
  24. </div>
  25. @foreach($menu['children'] as $chd)
  26. <input type="checkbox" lay-skin="primary" name="menus[{{$chd['mid']}}]" title="{{$chd['title']}}">
  27. @endforeach
  28. <hr>
  29. @endforeach
  30. </div>
  31. </div>
  32. </form>
  33. <div class="layui-input-block">
  34. <button class="layui-btn" onclick="save()">保存</button>
  35. </div>
  36. </body>
  37. </html>
  38. <script type="text/javascript">
  39. layui.use(['form', 'layer'], function () {
  40. $ = layui.jquery;
  41. form = layui.form;
  42. layer = layui.layer;
  43. });
  44. // 保存
  45. function save() {
  46. var title = $('input[name="title"]').val();
  47. if (title == '') {
  48. return layer.alert('请填写角色名称', {icon: 2});
  49. }
  50. $.post('/admins/groups/save', $('form').serialize(), function (res) {
  51. if (res.code > 0) {
  52. return layer.alert(res.msg, {icon: 2});
  53. }
  54. layer.msg(res.msg);
  55. setTimeout(function () {
  56. parent.window.location.reload();
  57. }, 1000);
  58. }, 'json');
  59. }
  60. </script>

3、添加与保存角色 控制器处理

app/Http/Controllers/admins/Groups.php

  1. // 添加角色
  2. public function add() {
  3. // 查询数据库可用的菜单
  4. $data['menus_list'] = DB::table('admin_menu')->where('pid', 0)->where('isdel', 0)->where('status', 0)->lists();
  5. // 所有菜单
  6. $all_menus = DB::table('admin_menu')->where('isdel', 0)->where('status', 0)->lists();
  7. foreach ($data['menus_list'] as $key => $val) {
  8. $data['menus_list'][$key]['children'] = [];
  9. foreach ($all_menus as $k => $item) {
  10. if ($val['mid'] == $item['pid']) {
  11. $data['menus_list'][$key]['children'][] = $item;
  12. }
  13. }
  14. }
  15. return view('admins/groups/add', $data);
  16. }
  17. // 保存角色
  18. public function save(Request $req) {
  19. $data['title'] = trim($req->title);
  20. $menus = $req->menus;
  21. $menu_ids = array_keys($menus);
  22. if ($data['title'] == '') {
  23. return json_encode(array('code' => 1, 'msg' => '角色名称不能为空'));
  24. }
  25. $data['rights'] = json_encode($menu_ids);
  26. DB::table('admin_group')->insert($data);
  27. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  28. }

4、添加与保存角色 注册路由

routes/web.php

  1. //添加角色 处理
  2. Route::get('/admins/groups/add', [Groups::class, 'add']);
  3. //保存角色 处理
  4. Route::post('/admins/groups/save', [Groups::class, 'save']);

5、查看效果

三、编辑与保存编辑角色 模块

1、创建 编辑与保存编辑角色 视图模板

2、编辑 编辑与保存编辑角色 视图模板


resources/views/admins/groups/edit.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>修改角色</title>
  5. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  6. <script type="text/javascript" src="/static/layui/layui.js"></script>
  7. </head>
  8. <body style="padding: 10px;">
  9. <form class="layui-form">
  10. @csrf
  11. <input type="hidden" name="gid" value="{{$group['gid']}}">
  12. <div class="layui-form-item">
  13. <label class="layui-form-label">角色名称</label>
  14. <div class="layui-input-block">
  15. <input type="text" name="title" class="layui-input" value="{{$group['title']}}">
  16. </div>
  17. </div>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">菜单</label>
  20. <div class="layui-input-block">
  21. @foreach($menus_list as $menu)
  22. <div>
  23. <input type="checkbox" lay-skin="primary" name="menus[{{$menu['mid']}}]"
  24. {{in_array($menu['mid'],$group['rights'])?'checked':''}}
  25. title="<span style='font-weight:bold'>{{$menu['title']}}</span>">
  26. </div>
  27. @foreach($menu['children'] as $chd)
  28. <input type="checkbox" lay-skin="primary" name="menus[{{$chd['mid']}}]"
  29. {{in_array($chd['mid'],$group['rights'])?'checked':''}} title="{{$chd['title']}}">
  30. @endforeach
  31. <hr>
  32. @endforeach
  33. </div>
  34. </div>
  35. </form>
  36. <div class="layui-input-block">
  37. <button class="layui-btn" onclick="save()">保存</button>
  38. </div>
  39. </body>
  40. </html>
  41. <script type="text/javascript">
  42. layui.use(['form', 'layer'], function () {
  43. $ = layui.jquery;
  44. form = layui.form;
  45. layer = layui.layer;
  46. });
  47. // 保存
  48. function save() {
  49. var title = $('input[name="title"]').val();
  50. if (title == '') {
  51. return layer.alert('请填写角色名称', {icon: 2});
  52. }
  53. $.post('/admins/groups/save_edit', $('form').serialize(), function (res) {
  54. if (res.code > 0) {
  55. return layer.alert(res.msg, {icon: 2});
  56. }
  57. layer.msg(res.msg);
  58. setTimeout(function () {
  59. parent.window.location.reload();
  60. }, 1000);
  61. }, 'json');
  62. }
  63. </script>

3、编辑与编辑保存角色 控制器处理

app/Http/Controllers/admins/Groups.php

  1. // 修改
  2. public function edit(Request $req) {
  3. // 当前角色
  4. $gid = (int)$req->gid;
  5. $data['group'] = DB::table('admin_group')->where('gid', $gid)->item();
  6. if ($data['group']['rights']) {
  7. $data['group']['rights'] = json_decode($data['group']['rights'], true);
  8. }
  9. // 查询数据库可用的菜单
  10. $data['menus_list'] = DB::table('admin_menu')->where('pid', 0)->where('isdel', 0)->where('status', 0)->lists();
  11. // 所有菜单
  12. $all_menus = DB::table('admin_menu')->where('isdel', 0)->where('status', 0)->lists();
  13. foreach ($data['menus_list'] as $key => $val) {
  14. $data['menus_list'][$key]['children'] = [];
  15. foreach ($all_menus as $k => $item) {
  16. if ($val['mid'] == $item['pid']) {
  17. $data['menus_list'][$key]['children'][] = $item;
  18. }
  19. }
  20. }
  21. return view('admins/groups/edit', $data);
  22. }
  23. // 保存修改
  24. public function save_edit(Request $req) {
  25. $gid = (int)$req->gid;
  26. $data['title'] = trim($req->title);
  27. $menus = $req->menus;
  28. $menu_ids = array_keys($menus);
  29. if ($data['title'] == '') {
  30. return json_encode(array('code' => 1, 'msg' => '角色名称不能为空'));
  31. }
  32. $data['rights'] = json_encode($menu_ids);
  33. DB::table('admin_group')->where('gid', $gid)->update($data);
  34. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  35. }

4、编辑与编辑保存角色 注册路由

  1. //编辑角色 弹窗
  2. Route::get('/admins/groups/edit', [Groups::class, 'edit']);
  3. //保存编辑角色 处理
  4. Route::post('/admins/groups/save_edit', [Groups::class, 'save_edit']);

5、查看效果

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