Blogger Information
Blog 41
fans 0
comment 0
visits 25299
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
后台三 账号列表
自由之上
Original
1001 people have browsed it

一、账号列表 模块

1、创建 账号列表 视图模板

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

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>管理员列表</title>
  9. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  10. <script type="text/javascript" src="/static/layui/layui.js"></script>
  11. </head>
  12. <body style="padding:10px;">
  13. <div style="text-align:right;">
  14. <button class="layui-btn layui-btn-sm" onclick="add()"><i class="layui-icon layui-icon-addition"></i>添加</button>
  15. </div>
  16. <table class="layui-table">
  17. <thead>
  18. <tr>
  19. <th>ID</th>
  20. <th>用户名</th>
  21. <th>角色</th>
  22. <th>真实姓名</th>
  23. <th>手机号</th>
  24. <th>最后登录时间</th>
  25. <th>状态</th>
  26. <th>操作</th>
  27. </tr>
  28. </thead>
  29. <tbody>
  30. @foreach($list as $item)
  31. <tr>
  32. <td>{{$item['id']}}</td>
  33. <td>{{$item['username']}}</td>
  34. <td>{{isset($groups[$item['gid']])?$groups[$item['gid']]['title']:''}}</td>
  35. <td>{{$item['real_name']}}</td>
  36. <td>{{$item['phone']}}</td>
  37. <td>{{$item['lastlogin']>0?date('Y-m-d H:i:s',$item['lastlogin']):''}}</td>
  38. <td>{!! $item['status']==0?'<span style="color:green">正常</span>':'<span style="color:red;">禁用</span>' !!}</td>
  39. <td>
  40. <button class="layui-btn layui-btn-xs layui-btn-normal" onclick="edit({{$item['id']}})">修改</button>
  41. <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$item['id']}})">删除</button>
  42. </td>
  43. </tr>
  44. @endforeach
  45. </tbody>
  46. </table>
  47. <script>
  48. layui.use(['layer'], function () {
  49. $ = layui.jquery;
  50. layer = layui.layer;
  51. });
  52. //添加管理员
  53. function add() {
  54. layer.open({
  55. type: 2,
  56. title: '添加管理员',
  57. shade: 0.3,
  58. area: ['500px', '450px'],
  59. content: '/admins/admin/add'
  60. });
  61. }
  62. //修改管理员
  63. function edit(id) {
  64. layer.open({
  65. type: 2,
  66. title: '修改管理员',
  67. shade: 0.3,
  68. area: ['500px', '450px'],
  69. content: '/admins/admin/edit?id=' + id
  70. });
  71. }
  72. //删除管理员
  73. function del(id) {
  74. layer.confirm('确定要删除吗?', {
  75. icon: 3,
  76. btn: ['确定', '取消']
  77. }, function () {
  78. $.get('/admins/admin/del', {admin_id: id}, function (res) {
  79. if(res.code>0){
  80. return layer.alert(res.msg,{icon:2});
  81. }
  82. layer.msg(res.msg);
  83. setTimeout(function () {
  84. window.location.reload();
  85. },500);
  86. },'json');
  87. });
  88. }
  89. </script>
  90. </body>
  91. </html>

2、提供者
在数据库服务提供者文件中添加函数
app/Providers/DBServiceProvider.php

  1. // 查询数据列表
  2. QueryBuilder::macro('lists', function () {
  3. $data = $this->get()->toArray();
  4. foreach ($data as $key => $val) {
  5. $data[$key] = (array)$val;
  6. }
  7. return $data;
  8. });
  9. // 自定义索引
  10. QueryBuilder::macro('cates', function ($index) {
  11. $data = $this->get()->toArray();
  12. $res = [];
  13. foreach ($data as $key => $val) {
  14. $res[$val->$index] = (array)$val;
  15. }
  16. return $res;
  17. });


3、创建 账号列表 控制器

app/Http/Controllers/admins/Admin.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 Admin extends Controller {
  9. }


4、添加 处理账号列表 函数
app/Http/Controllers/admins/Admin.php

  1. use Illuminate\Support\Facades\DB;
  2. /**
  3. * 管理员列表
  4. * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
  5. */
  6. public function index() {
  7. $data['list'] = DB::table('admin')->orderBy('id', 'desc')->lists();
  8. $data['groups'] = DB::table('admin_group')->cates('gid');
  9. return view('/admins/admin/index', $data);
  10. }


5、注册 账号列表 路由

  1. use App\Http\Controllers\admins\Admin;
  2. //显示账号列表
  3. Route::get('/admins/admin/index', [Admin::class, 'index']);


6、查看效果

二、添加与保存账号 模块

1、创建 添加账号 视图模板

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

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>添加管理员</title>
  9. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  10. <script type="text/javascript" src="/static/layui/layui.js"></script>
  11. </head>
  12. <body style="padding:10px;">
  13. <div class="layui-form">
  14. @csrf
  15. <div class="layui-form-item">
  16. <label class="layui-form-label">用户名</label>
  17. <div class="layui-input-block">
  18. <input type="text" class="layui-input" name="username">
  19. </div>
  20. </div>
  21. <div class="layui-form-item">
  22. <label class="layui-form-label">用户角色</label>
  23. <div class="layui-input-block">
  24. <select name="gid" id="">
  25. @foreach($groups as $item)
  26. <option value="{{$item['gid']}}">{{$item['title']}}</option>
  27. @endforeach
  28. </select>
  29. </div>
  30. </div>
  31. <div class="layui-form-item">
  32. <label class="layui-form-label">登录密码</label>
  33. <div class="layui-input-block">
  34. <input type="password" class="layui-input" name="pwd">
  35. </div>
  36. </div>
  37. <div class="layui-form-item">
  38. <label class="layui-form-label">重复密码</label>
  39. <div class="layui-input-block">
  40. <input type="password" class="layui-input" name="repwd">
  41. </div>
  42. </div>
  43. <div class="layui-form-item">
  44. <label class="layui-form-label">状态</label>
  45. <div class="layui-input-block">
  46. <input type="checkbox" lay-skin="primary" title="禁用" name="status">
  47. </div>
  48. </div>
  49. <div class="layui-input-block">
  50. <button class="layui-btn" onclick="save()">保存</button>
  51. </div>
  52. </div>
  53. <script>
  54. layui.use(['layer', 'form'], function () {
  55. $ = layui.jquery;
  56. form = layui.form;
  57. layer = layui.layer;
  58. });
  59. //保存
  60. function save() {
  61. var username = $('input[name="username"]').val();
  62. var gid = $('select[name="gid"]').val();
  63. var pwd = $('input[name="pwd"]').val();
  64. var repwd = $('input[name="repwd"]').val();
  65. var status = $('input[name="status"]').is(':checked') ? 1 : 0;
  66. var _token = $('input[name="_token"]').val();
  67. if (username == '') {
  68. return layer.alert('请填写用户名', {icon: 2});
  69. }
  70. if (pwd == "") {
  71. return layer.alert('请填写密码', {icon: 2});
  72. }
  73. if (pwd != repwd) {
  74. return layer.alert('两次密码输入不一致', {icon: 2});
  75. }
  76. $.post('/admins/admin/save', {
  77. username: username,
  78. gid: gid,
  79. pwd: pwd,
  80. status: status,
  81. _token: _token
  82. }, function (res) {
  83. if (res.code > 0) {
  84. return layer.alert(res.msg, {icon: 2});
  85. }
  86. layer.msg(res.msg);
  87. setTimeout(function () {
  88. parent.window.location.reload();
  89. }, 1000);
  90. },'json')
  91. }
  92. </script>
  93. </body>
  94. </html>

2、 添加与保存账号 控制器处理
app/Http/Controllers/admins/Admin.php

  1. use Illuminate\Http\Request;
  2. /**
  3. * 添加管理员
  4. */
  5. public function add() {
  6. $data['groups'] = DB::table('admin_group')->select('gid', 'title')->lists();
  7. return view('admins/admin/add', $data);
  8. }
  9. /**
  10. * 保存
  11. */
  12. public function save(Request $req) {
  13. $data['username'] = trim($req->username);
  14. $data['gid'] = (int)$req->gid;
  15. $data['password'] = password_hash(trim($req->pwd), PASSWORD_DEFAULT);
  16. $data['status'] = (int)$req->status;
  17. //查询用户名有没有被注册
  18. $user = DB::table('admin')->where('username', $data['username'])->first();
  19. if ($user) {
  20. return json_encode(array('code' => 1, 'msg' => '该用户已存在'));
  21. }
  22. $res = DB::table('admin')->insert($data);
  23. if (!$res) {
  24. return json_encode(array('code' => 1, 'msg' => '保存失败'));
  25. }
  26. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  27. }


3、添加与保存账号 注册路由

  1. //添加账号 处理
  2. Route::get('/admins/admin/add', [Admin::class, 'add']);
  3. //保存账号 处理
  4. Route::post('/admins/admin/save', [Admin::class, 'save']);


4、查看效果

待添加完权限菜单再测试

三、编辑与保存账号 模块

1、创建 编辑账号 视图模板

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

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>修改管理员</title>
  9. <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
  10. <script type="text/javascript" src="/static/layui/layui.js"></script>
  11. </head>
  12. <body style="padding:10px;">
  13. <div class="layui-form">
  14. @csrf
  15. <input type="hidden" name="admin_id" value="{{$item['id']}}">
  16. <div class="layui-form-item">
  17. <label class="layui-form-label">用户名</label>
  18. <div class="layui-input-block">
  19. <input type="text" class="layui-input" name="username" value="{{$item['username']}}" disabled>
  20. </div>
  21. </div>
  22. <div class="layui-form-item">
  23. <lable class="layui-form-label">用户角色</lable>
  24. <div class="layui-input-block">
  25. <select name="gid">
  26. @foreach($groups as $val)
  27. <option value="{{$val['gid']}}" {{$item['gid']==$val['gid']?'selected':''}}>{{$val['title']}}</option>
  28. @endforeach
  29. </select>
  30. </div>
  31. </div>
  32. <div class="layui-form-item">
  33. <label class="layui-form-label">登录密码</label>
  34. <div class="layui-input-block">
  35. <input type="password" class="layui-input" name="pwd" placeholder="留空表示不修改密码">
  36. </div>
  37. </div>
  38. <div class="layui-form-item">
  39. <label class="layui-form-label">重复密码</label>
  40. <div class="layui-input-block">
  41. <input type="password" class="layui-input" name="repwd" placeholder="留空表示不修改密码">
  42. </div>
  43. </div>
  44. <div class="layui-form-item">
  45. <label class="layui-form-label">状态</label>
  46. <div class="layui-input-inline">
  47. <input type="checkbox" lay-skin="primary" name="status" title="禁用"{{$item['status']==1?'checked':''}}>
  48. </div>
  49. </div>
  50. <div class="layui-input-block">
  51. <button class="layui-btn" onclick="save()">保存</button>
  52. </div>
  53. </div>
  54. <script>
  55. layui.use(['layer', 'form'], function () {
  56. $ = layui.jquery;
  57. form = layui.form;
  58. layer = layui.layer;
  59. });
  60. //保存
  61. function save() {
  62. var admin_id = $('input[name="admin_id"]').val();
  63. var gid = $('select[name="gid"]').val();
  64. var pwd = $('input[name="pwd"]').val();
  65. var repwd = $('input[name="repwd"]').val();
  66. var status = $('input[name="status"]').is(':checked') ? 1 : 0;
  67. var _token = $('input[name="_token"]').val();
  68. if (pwd !== repwd) {
  69. return layer.alert('两次密码不一致', {icon: 2});
  70. }
  71. $.post('/admins/admin/save_edit', {
  72. admin_id: admin_id,
  73. gid: gid,
  74. pwd: pwd,
  75. status: status,
  76. _token: _token,
  77. },
  78. function (res) {
  79. console.log("编辑角色结果:", res);
  80. if (res.code > 0) {
  81. return layer.alert(res.msg, {icon: 2});
  82. }
  83. layer.msg(res.msg);
  84. setTimeout(function () {
  85. parent.window.location.reload();
  86. }, 1000)
  87. }, 'json'
  88. );
  89. }
  90. </script>
  91. </body>
  92. </html>

2、编辑与保存账号列表 控制器处理
app/Http/Controllers/admins/Admin.php

  1. /**
  2. * 编辑-角色信息
  3. */
  4. public function edit(Request $req) {
  5. $id = (int)$req->id;
  6. $data['item'] = DB::table('admin')->where('id', $id)->item();
  7. $data['groups'] = DB::table('admin_group')->select('gid', 'title')->lists();
  8. return view('admins/admin/edit', $data);
  9. }
  10. /**
  11. * 保存编辑
  12. */
  13. public function save_edit(Request $req) {
  14. $id = (int)$req->admin_id;
  15. $pwd = trim($req->pwd);
  16. $data['gid'] = (int)$req->gid;
  17. $data['status'] = (int)$req->status;
  18. if ($pwd) {
  19. $data['password'] = password_hash($pwd, PASSWORD_DEFAULT);
  20. }
  21. DB::table('admin')->where('id', $id)->update($data);
  22. return json_encode(array('code' => 0, 'msg' => '保存成功'));
  23. }


3、编辑与保存注册路由
routes/web.php

  1. //编辑账号 界面
  2. Route::get('/admins/admin/edit', [Admin::class, 'edit']);
  3. //保存编辑账号 处理
  4. Route::post('/admins/admin/save_edit', [Admin::class, 'save_edit']);


4、查看效果

等做完权限菜单再来检测

四、删除账号 模块

1、删除账号 控制器处理
app/Http/Controllers/admins/Admin.php

  1. /**
  2. * 删除
  3. * @param Request $req
  4. * @return false|string
  5. */
  6. public function del(Request $req) {
  7. $admin_id = (int)$req->admin_id;
  8. DB::table('admin')->where('id', $admin_id)->delete();
  9. return json_encode(array('code'=>0,'msg'=>'删除成功'));
  10. }


2、删除账号 注册路由
routes/web.php

  1. //删除账号 处理
  2. Route::get('/admins/admin/del', [Admin::class, 'del']);


3、查看效果

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