欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

laravel--通用后台管理系统--用户的登录和退出--用户的增删改查

程序员文章站 2022-05-16 21:44:51
...

学习总结

1.用户的退出用到Auth::logout()方法

2.保存数据时用save方法,需要通过判断是否存在用户id值判断是增加还是修改用户数据

3.可以通过layer.open()中的btnyes属性给弹窗添加按钮和按钮方法

1.admin控制器admin.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\Hash;//哈希密码加密
  5. use Illuminate\Http\Request;
  6. //引入数据库查询构造器,链式调用
  7. use Illuminate\Support\Facades\DB;
  8. //后台账号管理控制器
  9. class Admin extends Controller
  10. {
  11. //账号列表
  12. public function index(){
  13. $res['admins'] = DB::table('admin')->lists();
  14. $res['admin_group']=DB::table('admin_group')->select('gid','title')->cate('gid');
  15. return view('/admins/admin/index',$res);
  16. }
  17. //添加账号
  18. public function add()
  19. {
  20. $res['groups']=DB::table('admin_group')->lists();
  21. return view('/admins/admin/add',$res);
  22. }
  23. //修改账号
  24. public function edit(Request $req)
  25. {
  26. $aid = $req->id;
  27. $res['admin'] = DB::table('admin')->where('id',$aid)->item();
  28. $res['groups']=DB::table('admin_group')->lists();
  29. // echo '<pre>';
  30. // print_r($res);
  31. return view('/admins/admin/edit',$res);
  32. }
  33. //保存账号
  34. public function save(Request $req)
  35. {
  36. $aid =(int)$req->aid;
  37. if($aid):
  38. //如果aid不为0,则代表是修改管理员
  39. if(trim($req->password)!=='')://如果修改管理员是密码框为空,代理不修改密码,就不用更新密码字段
  40. $data['password'] =Hash::make(trim($req->password));
  41. endif;
  42. else:
  43. //如果aid为0,则代表是添加管理员
  44. //判断用户名是否为空
  45. if(trim($req->username)===''):
  46. return json_encode(['status'=>'1','msg'=>'用户名不能为空']);
  47. endif;
  48. $data['username'] = trim($req->username);
  49. //判断用户是否存在
  50. $res = DB::table('admin')->where('username',$data['username'])->item();
  51. if($res):
  52. return json_encode(['status'=>'1','msg'=>'用户已存在']);
  53. endif;
  54. //判断密码是否为空
  55. if(trim($req->password)===''):
  56. return json_encode(['status'=>'1','msg'=>'密码不能为空']);
  57. endif;
  58. $data['password'] =Hash::make(trim($req->password));
  59. //$data['password'] =password_hash(trim($req->password),PASSWORD_DEFAULT);与上一条语句功能一样
  60. endif;
  61. if(trim($req->gid)===''):
  62. return json_encode(['status'=>'1','msg'=>'必须选择角色']);
  63. endif;
  64. $data['gid'] = $req->gid;
  65. $data['real_name'] = $req->real_name;
  66. $data['status'] = $req->status?0:1;//0代表正常,1代表禁用
  67. $data['add_time']=time();
  68. if($aid):
  69. $res = DB::table('admin')->where('id',$aid)->update($data);
  70. else:
  71. $res = DB::table('admin')->insert($data);
  72. endif;
  73. if($res):
  74. return json_encode(['status'=>'0','msg'=>'保存成功']);
  75. else:
  76. return json_encode(['status'=>'1','msg'=>'保存失败']);
  77. endif;
  78. // echo '<pre>';
  79. // print_r($data);
  80. }
  81. //删除账号
  82. public function del(Request $req)
  83. {
  84. $aid = $req->id;
  85. $res =DB::table('admin')->where('id',$aid)->delete();
  86. if($res)
  87. {
  88. return json_encode(['status'=>'0','msg'=>'删除成功']);
  89. }
  90. else
  91. {
  92. return json_encode(['status'=>'1','msg'=>'删除失败']);
  93. }
  94. }
  95. }

2.用户列表显示页面index.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <title>账号管理</title>
  9. </head>
  10. <style>
  11. .addBtn {
  12. margin-top: 10px;
  13. width: 120px;
  14. margin-left: auto;
  15. margin-right: 50px;
  16. }
  17. .layui-table {
  18. width: 95%;
  19. margin: 20px;
  20. }
  21. </style>
  22. <body>
  23. <div class="addBtn">
  24. <button class="layui-btn" onclick="add()">添加管理员</button>
  25. </div>
  26. <table class="layui-table">
  27. <thead>
  28. <tr>
  29. <th>编号</th>
  30. <th>用户名</th>
  31. <th>角色</th>
  32. <th>真实姓名</th>
  33. <th>状态</th>
  34. <th>注册时间</th>
  35. <th>操作</th>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. @foreach($admins as $admin)
  40. <tr>
  41. <td>{{$admin['id']}}</td>
  42. <td>{{$admin['username']}}</td>
  43. <td>{{$admin_group[$admin['gid']]['title']}}</td>
  44. <td>{{$admin['real_name']}}</td>
  45. <td>{{$admin['status']?'禁用':'正常'}}</td>
  46. <td>{{$admin['add_time']?date('Y-m-d',$admin['add_time']):''}}</td>
  47. <td>
  48. <button class="layui-btn layui-btn-xs" onclick="edit({{$admin['id']}})">修改</button>
  49. <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$admin['id']}})">删除</button>
  50. </td>
  51. </tr>
  52. @endforeach
  53. </tbody>
  54. </table>
  55. </body>
  56. <script>
  57. layui.use(['layer'], function() {
  58. layer = layui.layer;
  59. $ = layui.jquery;
  60. });
  61. //添加管理员
  62. function add() {
  63. //iframe层
  64. layer.open({
  65. type: 2,
  66. title: '添加管理员',
  67. shadeClose: true,
  68. shade: 0.8,
  69. area: ['380px', '80%'],
  70. content: '/admins/admin/add', //iframe的url
  71. btn: ['保存'],
  72. yes: function(index, layero) {
  73. //得到iframe页的窗口对象,执行iframe页的方法:iframeWin.子页面的方法名称();
  74. var iframeWin = window[layero.find('iframe')[0]['name']];;
  75. iframeWin.save();
  76. }
  77. });
  78. }
  79. //修改管理员
  80. function edit(id) {
  81. //iframe层
  82. layer.open({
  83. type: 2,
  84. title: '修改管理员',
  85. shadeClose: true,
  86. shade: 0.8,
  87. area: ['380px', '80%'],
  88. content: '/admins/admin/edit?id='+id, //iframe的url
  89. btn: ['保存'],
  90. yes: function(index, layero) {
  91. //得到iframe页的窗口对象,执行iframe页的方法:iframeWin.子页面的方法名称();
  92. var iframeWin = window[layero.find('iframe')[0]['name']];;
  93. iframeWin.save();
  94. }
  95. });
  96. }
  97. //删除管理员
  98. function del(id) {
  99. layer.confirm('确认要删除管理员吗?', function() {
  100. $.get('/admins/admin/del?id=' + id, function(res) {
  101. if (res.status > 0) {
  102. layer.alert(res.msg, {
  103. icon: 2
  104. });
  105. } else {
  106. layer.msg(res.msg, {
  107. time: 3000
  108. });
  109. window.location.reload();
  110. }
  111. }, 'json');
  112. });
  113. }
  114. </script>
  115. </html>

laravel--通用后台管理系统--用户的登录和退出--用户的增删改查

3.添加用户add.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <title>添加管理员</title>
  9. <style>
  10. .layui-form {
  11. margin-top: 20px;
  12. margin-right: 20px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <form action="" class="layui-form">
  18. @csrf
  19. <div class="layui-form-item">
  20. <label for="" class="layui-form-label">用户名</label>
  21. <div class="layui-input-inline">
  22. <input type="text" class="layui-input" name="username">
  23. </div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label for="" class="layui-form-label">密码</label>
  27. <div class="layui-input-inline">
  28. <input type="password" class="layui-input" name="password">
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label for="" class="layui-form-label">角色</label>
  33. <div class="layui-input-inline">
  34. <select name="gid">
  35. <option value=""></option>
  36. @foreach($groups as $group )
  37. <option value="{{$group['gid']}}">{{$group['title']}}</option>
  38. @endforeach
  39. </select>
  40. </div>
  41. </div>
  42. <div class="layui-form-item">
  43. <label for="" class="layui-form-label">真实姓名</label>
  44. <div class="layui-input-inline">
  45. <input type="text" class="layui-input" name="real_name">
  46. </div>
  47. </div>
  48. <div class="layui-form-item">
  49. <label for="" class="layui-form-label">状态</label>
  50. <div class="layui-input-inline">
  51. <input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用' checked>
  52. </div>
  53. </div>
  54. </form>
  55. </body>
  56. <script>
  57. layui.use(['form', 'layer'], function() {
  58. var form = layui.form;
  59. var layer = layui.layer;
  60. $ = layui.jquery;
  61. });
  62. //在父窗口中调用了save方法
  63. function save() {
  64. if ($('input[name="username"]').val().trim() === '') {
  65. error('请输入用户名');
  66. return false;
  67. }
  68. if ($('input[name="password"]').val().trim() === '') {
  69. error('请输入密码');
  70. return false;
  71. }
  72. if ($('select[name="gid"]').val().trim() === '') {
  73. error('请选择角色');
  74. return false;
  75. }
  76. //把form表单中的数据序列化后通过post提交
  77. var form = $('form').serialize();
  78. $.post('/admins/admin/save',
  79. form,
  80. function(res) {
  81. if (res.status > 0) {
  82. error(res.msg);
  83. } else {
  84. layer.msg(res.msg, {
  85. time: 3000
  86. });
  87. //假设这是iframe页
  88. var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  89. //关闭当前窗口
  90. parent.layer.close(index); //再执行关闭
  91. //刷新父窗口,重新显示账号管理员列表
  92. parent.window.location.reload();
  93. }
  94. }, 'json');
  95. function error(msg) {
  96. layer.alert(msg, {
  97. icon: 2
  98. });
  99. }
  100. }
  101. </script>
  102. </html>

laravel--通用后台管理系统--用户的登录和退出--用户的增删改查

4.修改用户edit.blade.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <title>修改管理员</title>
  9. <style>
  10. .layui-form {
  11. margin-top: 20px;
  12. margin-right: 20px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <form action="" class="layui-form">
  18. @csrf
  19. <div class="layui-form-item">
  20. <input type="hidden" name="aid" value="{{$admin['id']}}">
  21. <label for="" class="layui-form-label">用户名</label>
  22. <div class="layui-input-inline">
  23. <input type="text" class="layui-input" name="username" disabled value="{{$admin['username']}}">
  24. </div>
  25. </div>
  26. <div class="layui-form-item">
  27. <label for="" class="layui-form-label">密码</label>
  28. <div class="layui-input-inline">
  29. <input type="password" class="layui-input" name="password">
  30. </div>
  31. </div>
  32. <div class="layui-form-item">
  33. <label for="" class="layui-form-label">角色</label>
  34. <div class="layui-input-inline">
  35. <select name="gid">
  36. <option value=""></option>
  37. @foreach($groups as $group )
  38. @if($group['gid']==$admin['gid'])
  39. <option value="{{$group['gid']}}" selected>{{$group['title']}}</option>
  40. @else
  41. <option value="{{$group['gid']}}">{{$group['title']}}</option>
  42. @endif
  43. @endforeach
  44. </select>
  45. </div>
  46. </div>
  47. <div class="layui-form-item">
  48. <label for="" class="layui-form-label">真实姓名</label>
  49. <div class="layui-input-inline">
  50. <input type="text" class="layui-input" name="real_name" value="{{$admin['real_name']}}">
  51. </div>
  52. </div>
  53. <div class="layui-form-item">
  54. <label for="" class="layui-form-label">状态</label>
  55. <div class="layui-input-inline">
  56. @if($admin['status'])
  57. <input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用'>
  58. @else
  59. <input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用' checked>
  60. @endif
  61. </div>
  62. </div>
  63. </form>
  64. </body>
  65. <script>
  66. layui.use(['form', 'layer'], function() {
  67. var form = layui.form;
  68. var layer = layui.layer;
  69. $ = layui.jquery;
  70. });
  71. //在父窗口中调用了save方法
  72. function save() {
  73. if ($('select[name="gid"]').val().trim() === '') {
  74. error('请选择角色');
  75. return false;
  76. }
  77. //把form表单中的数据序列化后通过post提交
  78. var form = $('form').serialize();
  79. $.post('/admins/admin/save',
  80. form,
  81. function(res) {
  82. if (res.status > 0) {
  83. error(res.msg);
  84. } else {
  85. layer.msg(res.msg, {
  86. time: 3000
  87. });
  88. //假设这是iframe页
  89. var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
  90. //关闭当前窗口
  91. parent.layer.close(index); //再执行关闭
  92. //刷新父窗口,重新显示账号管理员列表
  93. parent.window.location.reload();
  94. }
  95. }, 'json');
  96. function error(msg) {
  97. layer.alert(msg, {
  98. icon: 2
  99. });
  100. }
  101. }
  102. </script>
  103. </html>

laravel--通用后台管理系统--用户的登录和退出--用户的增删改查

5.home控制器home.php

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Http\Request;
  6. //引入数据库查询构造器,链式调用
  7. use Illuminate\Support\Facades\DB;
  8. //后台主页
  9. class Home extends Controller
  10. {
  11. public function index()
  12. {
  13. $menus = DB::table('admin_menu')->lists();
  14. $res = [];
  15. foreach($menus as $menu ):
  16. //ishidden为0代表该显示该菜单
  17. //pid等于0的并且的为*菜单存储在pmenus中
  18. if($menu['pid']=='0' && $menu['ishidden']=='0'):
  19. $res['pmenus'][] = $menu;
  20. endif;
  21. //pid不等于0的为子菜单,按照pid进行分组存储在cmenus中
  22. if(($menu['pid']!='0')&&($menu['ishidden']=='0')):
  23. $res['cmenus'][$menu['pid']][] = $menu;
  24. endif;
  25. endforeach;
  26. $res['username'] = Auth::user()->username;
  27. // echo '<pre>';
  28. // print_r($res);
  29. // exit;
  30. return view('/admins/home/index',$res);
  31. }
  32. public function welcome()
  33. {
  34. return view('/admins/home/welcome');
  35. }
  36. public function logout()
  37. {
  38. Auth::logout();
  39. return view('/admins/account/login');
  40. }
  41. }
  42. ?>

6.后台首页index.blade.php,菜单自动加载,显示用户名称,实现用户退出功能

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <script src="/static/plugins/layui/layui.js"></script>
  8. <title>后台首页</title>
  9. <style>
  10. body {
  11. margin: 0px;
  12. padding: 0px;
  13. box-sizing: border-box;
  14. }
  15. /* 头部 */
  16. .header {
  17. width: 100%;
  18. height: 50px;
  19. line-height: 50px;
  20. background-color: #01AAED;
  21. color: #fff;
  22. display: flex;
  23. flex-flow: row nowrap;
  24. justify-content: space-between;
  25. }
  26. .header h3 {
  27. margin-left: 20px;
  28. }
  29. .header>div {
  30. margin-right: 20px;
  31. }
  32. .header>div>a {
  33. color: #fff;
  34. }
  35. .header>div>a:hover {
  36. color: orange;
  37. }
  38. /* 侧边栏和主页面显示区 */
  39. .container {
  40. display: flex;
  41. flex-flow: row nowrap;
  42. justify-content: space-between;
  43. align-items: center;
  44. }
  45. /* 侧边栏 */
  46. .aside {
  47. width: 200px;
  48. background-color: #01AAED;
  49. }
  50. .aside>.layui-collapse {
  51. width: 200px;
  52. }
  53. .aside .layui-colla-title {
  54. background-color: #01AAED;
  55. color: #fff;
  56. }
  57. .aside .layui-nav {
  58. margin-left: -15px;
  59. margin-top: -10px;
  60. margin-bottom: -10px;
  61. width: 197px;
  62. background-color: #fff;
  63. }
  64. .aside .layui-nav .layui-nav-item {
  65. border-bottom: 1px solid #e2e2e2;
  66. border-right: 1px solid #e2e2e2;
  67. }
  68. .aside .layui-nav .layui-nav-item a {
  69. color: #333;
  70. }
  71. .aside .layui-nav .layui-nav-item a:hover {
  72. background-color: lightseagreen;
  73. }
  74. /* 主页面显示区 */
  75. .main {
  76. width: 100%;
  77. }
  78. .main>iframe {
  79. width: 100%;
  80. }
  81. </style>
  82. </head>
  83. <body>
  84. <!-- 头部 -->
  85. <div class="header">
  86. <h3>瑄然软件后台管理系统</h3>
  87. <div>
  88. <span class="layui-icon layui-icon-username">{{$username}}</span>
  89. <a href="/admins/home/logout">退出</a>
  90. </div>
  91. </div>
  92. <div class="container">
  93. <!-- 侧边栏 -->
  94. <div class="aside">
  95. <div class="layui-collapse" lay-accordion>
  96. @foreach($pmenus as $key => $pmenu)
  97. <div class="layui-colla-item">
  98. <h2 class="layui-colla-title">{{$pmenu['title']}}</h2>
  99. <!-- 默认显示第一个菜单的子菜单 -->
  100. <div class="layui-colla-content {{$key?'':'layui-show'}}">
  101. <!-- <div class="layui-colla-content layui-show"> -->
  102. <ul class="layui-nav layui-nav-tree">
  103. <!-- 找到一级菜单所对应的子菜单 -->
  104. <?php $cmenu = $cmenus[$pmenu['mid']];?>
  105. @foreach($cmenu as $item)
  106. <li class="layui-nav-item">
  107. <a href="/admins/{{$item['controller']}}/{{$item['action']}}"
  108. target="showMain">{{$item['title']}}</a>
  109. </li>
  110. @endforeach
  111. </ul>
  112. </div>
  113. </div>
  114. @endforeach
  115. </div>
  116. </div>
  117. <!-- 主页面显示区 -->
  118. <div class="main">
  119. <iframe src="/admins/home/welcome" frameborder="0" name="showMain"></iframe>
  120. </div>
  121. </div>
  122. </body>
  123. <script>
  124. layui.use(['element', 'layer'], function() {
  125. var element = layui.element;
  126. $ = layui.jquery;
  127. var aside_height = document.documentElement.clientHeight - 50;
  128. $('.aside').height(aside_height);
  129. //主页面的高度和侧边栏高度相同
  130. $('.main>iframe').height(aside_height);
  131. });
  132. </script>
  133. </html>

7.点击退出按钮,用户退出到登录页面

laravel--通用后台管理系统--用户的登录和退出--用户的增删改查