laravel--通用后台管理系统--用户的登录和退出--用户的增删改查
程序员文章站
2022-05-16 21:44:51
...
学习总结
1.用户的退出用到Auth::logout()
方法
2.保存数据时用save
方法,需要通过判断是否存在用户id值判断是增加还是修改用户数据
3.可以通过layer.open()
中的btn
和yes
属性给弹窗添加按钮和按钮方法
1.admin控制器admin.php
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;//哈希密码加密
use Illuminate\Http\Request;
//引入数据库查询构造器,链式调用
use Illuminate\Support\Facades\DB;
//后台账号管理控制器
class Admin extends Controller
{
//账号列表
public function index(){
$res['admins'] = DB::table('admin')->lists();
$res['admin_group']=DB::table('admin_group')->select('gid','title')->cate('gid');
return view('/admins/admin/index',$res);
}
//添加账号
public function add()
{
$res['groups']=DB::table('admin_group')->lists();
return view('/admins/admin/add',$res);
}
//修改账号
public function edit(Request $req)
{
$aid = $req->id;
$res['admin'] = DB::table('admin')->where('id',$aid)->item();
$res['groups']=DB::table('admin_group')->lists();
// echo '<pre>';
// print_r($res);
return view('/admins/admin/edit',$res);
}
//保存账号
public function save(Request $req)
{
$aid =(int)$req->aid;
if($aid):
//如果aid不为0,则代表是修改管理员
if(trim($req->password)!=='')://如果修改管理员是密码框为空,代理不修改密码,就不用更新密码字段
$data['password'] =Hash::make(trim($req->password));
endif;
else:
//如果aid为0,则代表是添加管理员
//判断用户名是否为空
if(trim($req->username)===''):
return json_encode(['status'=>'1','msg'=>'用户名不能为空']);
endif;
$data['username'] = trim($req->username);
//判断用户是否存在
$res = DB::table('admin')->where('username',$data['username'])->item();
if($res):
return json_encode(['status'=>'1','msg'=>'用户已存在']);
endif;
//判断密码是否为空
if(trim($req->password)===''):
return json_encode(['status'=>'1','msg'=>'密码不能为空']);
endif;
$data['password'] =Hash::make(trim($req->password));
//$data['password'] =password_hash(trim($req->password),PASSWORD_DEFAULT);与上一条语句功能一样
endif;
if(trim($req->gid)===''):
return json_encode(['status'=>'1','msg'=>'必须选择角色']);
endif;
$data['gid'] = $req->gid;
$data['real_name'] = $req->real_name;
$data['status'] = $req->status?0:1;//0代表正常,1代表禁用
$data['add_time']=time();
if($aid):
$res = DB::table('admin')->where('id',$aid)->update($data);
else:
$res = DB::table('admin')->insert($data);
endif;
if($res):
return json_encode(['status'=>'0','msg'=>'保存成功']);
else:
return json_encode(['status'=>'1','msg'=>'保存失败']);
endif;
// echo '<pre>';
// print_r($data);
}
//删除账号
public function del(Request $req)
{
$aid = $req->id;
$res =DB::table('admin')->where('id',$aid)->delete();
if($res)
{
return json_encode(['status'=>'0','msg'=>'删除成功']);
}
else
{
return json_encode(['status'=>'1','msg'=>'删除失败']);
}
}
}
2.用户列表显示页面index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<title>账号管理</title>
</head>
<style>
.addBtn {
margin-top: 10px;
width: 120px;
margin-left: auto;
margin-right: 50px;
}
.layui-table {
width: 95%;
margin: 20px;
}
</style>
<body>
<div class="addBtn">
<button class="layui-btn" onclick="add()">添加管理员</button>
</div>
<table class="layui-table">
<thead>
<tr>
<th>编号</th>
<th>用户名</th>
<th>角色</th>
<th>真实姓名</th>
<th>状态</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach($admins as $admin)
<tr>
<td>{{$admin['id']}}</td>
<td>{{$admin['username']}}</td>
<td>{{$admin_group[$admin['gid']]['title']}}</td>
<td>{{$admin['real_name']}}</td>
<td>{{$admin['status']?'禁用':'正常'}}</td>
<td>{{$admin['add_time']?date('Y-m-d',$admin['add_time']):''}}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="edit({{$admin['id']}})">修改</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({{$admin['id']}})">删除</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</body>
<script>
layui.use(['layer'], function() {
layer = layui.layer;
$ = layui.jquery;
});
//添加管理员
function add() {
//iframe层
layer.open({
type: 2,
title: '添加管理员',
shadeClose: true,
shade: 0.8,
area: ['380px', '80%'],
content: '/admins/admin/add', //iframe的url
btn: ['保存'],
yes: function(index, layero) {
//得到iframe页的窗口对象,执行iframe页的方法:iframeWin.子页面的方法名称();
var iframeWin = window[layero.find('iframe')[0]['name']];;
iframeWin.save();
}
});
}
//修改管理员
function edit(id) {
//iframe层
layer.open({
type: 2,
title: '修改管理员',
shadeClose: true,
shade: 0.8,
area: ['380px', '80%'],
content: '/admins/admin/edit?id='+id, //iframe的url
btn: ['保存'],
yes: function(index, layero) {
//得到iframe页的窗口对象,执行iframe页的方法:iframeWin.子页面的方法名称();
var iframeWin = window[layero.find('iframe')[0]['name']];;
iframeWin.save();
}
});
}
//删除管理员
function del(id) {
layer.confirm('确认要删除管理员吗?', function() {
$.get('/admins/admin/del?id=' + id, function(res) {
if (res.status > 0) {
layer.alert(res.msg, {
icon: 2
});
} else {
layer.msg(res.msg, {
time: 3000
});
window.location.reload();
}
}, 'json');
});
}
</script>
</html>
3.添加用户add.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<title>添加管理员</title>
<style>
.layui-form {
margin-top: 20px;
margin-right: 20px;
}
</style>
</head>
<body>
<form action="" class="layui-form">
@csrf
<div class="layui-form-item">
<label for="" class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="username">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="password" class="layui-input" name="password">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">角色</label>
<div class="layui-input-inline">
<select name="gid">
<option value=""></option>
@foreach($groups as $group )
<option value="{{$group['gid']}}">{{$group['title']}}</option>
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">真实姓名</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="real_name">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">状态</label>
<div class="layui-input-inline">
<input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用' checked>
</div>
</div>
</form>
</body>
<script>
layui.use(['form', 'layer'], function() {
var form = layui.form;
var layer = layui.layer;
$ = layui.jquery;
});
//在父窗口中调用了save方法
function save() {
if ($('input[name="username"]').val().trim() === '') {
error('请输入用户名');
return false;
}
if ($('input[name="password"]').val().trim() === '') {
error('请输入密码');
return false;
}
if ($('select[name="gid"]').val().trim() === '') {
error('请选择角色');
return false;
}
//把form表单中的数据序列化后通过post提交
var form = $('form').serialize();
$.post('/admins/admin/save',
form,
function(res) {
if (res.status > 0) {
error(res.msg);
} else {
layer.msg(res.msg, {
time: 3000
});
//假设这是iframe页
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//关闭当前窗口
parent.layer.close(index); //再执行关闭
//刷新父窗口,重新显示账号管理员列表
parent.window.location.reload();
}
}, 'json');
function error(msg) {
layer.alert(msg, {
icon: 2
});
}
}
</script>
</html>
4.修改用户edit.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<title>修改管理员</title>
<style>
.layui-form {
margin-top: 20px;
margin-right: 20px;
}
</style>
</head>
<body>
<form action="" class="layui-form">
@csrf
<div class="layui-form-item">
<input type="hidden" name="aid" value="{{$admin['id']}}">
<label for="" class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="username" disabled value="{{$admin['username']}}">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="password" class="layui-input" name="password">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">角色</label>
<div class="layui-input-inline">
<select name="gid">
<option value=""></option>
@foreach($groups as $group )
@if($group['gid']==$admin['gid'])
<option value="{{$group['gid']}}" selected>{{$group['title']}}</option>
@else
<option value="{{$group['gid']}}">{{$group['title']}}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">真实姓名</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="real_name" value="{{$admin['real_name']}}">
</div>
</div>
<div class="layui-form-item">
<label for="" class="layui-form-label">状态</label>
<div class="layui-input-inline">
@if($admin['status'])
<input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用'>
@else
<input type="checkbox" name="status" lay-skin='switch' lay-text='正常|禁用' checked>
@endif
</div>
</div>
</form>
</body>
<script>
layui.use(['form', 'layer'], function() {
var form = layui.form;
var layer = layui.layer;
$ = layui.jquery;
});
//在父窗口中调用了save方法
function save() {
if ($('select[name="gid"]').val().trim() === '') {
error('请选择角色');
return false;
}
//把form表单中的数据序列化后通过post提交
var form = $('form').serialize();
$.post('/admins/admin/save',
form,
function(res) {
if (res.status > 0) {
error(res.msg);
} else {
layer.msg(res.msg, {
time: 3000
});
//假设这是iframe页
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//关闭当前窗口
parent.layer.close(index); //再执行关闭
//刷新父窗口,重新显示账号管理员列表
parent.window.location.reload();
}
}, 'json');
function error(msg) {
layer.alert(msg, {
icon: 2
});
}
}
</script>
</html>
5.home控制器home.php
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
//引入数据库查询构造器,链式调用
use Illuminate\Support\Facades\DB;
//后台主页
class Home extends Controller
{
public function index()
{
$menus = DB::table('admin_menu')->lists();
$res = [];
foreach($menus as $menu ):
//ishidden为0代表该显示该菜单
//pid等于0的并且的为*菜单存储在pmenus中
if($menu['pid']=='0' && $menu['ishidden']=='0'):
$res['pmenus'][] = $menu;
endif;
//pid不等于0的为子菜单,按照pid进行分组存储在cmenus中
if(($menu['pid']!='0')&&($menu['ishidden']=='0')):
$res['cmenus'][$menu['pid']][] = $menu;
endif;
endforeach;
$res['username'] = Auth::user()->username;
// echo '<pre>';
// print_r($res);
// exit;
return view('/admins/home/index',$res);
}
public function welcome()
{
return view('/admins/home/welcome');
}
public function logout()
{
Auth::logout();
return view('/admins/account/login');
}
}
?>
6.后台首页index.blade.php,菜单自动加载,显示用户名称,实现用户退出功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<script src="/static/plugins/layui/layui.js"></script>
<title>后台首页</title>
<style>
body {
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
/* 头部 */
.header {
width: 100%;
height: 50px;
line-height: 50px;
background-color: #01AAED;
color: #fff;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
}
.header h3 {
margin-left: 20px;
}
.header>div {
margin-right: 20px;
}
.header>div>a {
color: #fff;
}
.header>div>a:hover {
color: orange;
}
/* 侧边栏和主页面显示区 */
.container {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
}
/* 侧边栏 */
.aside {
width: 200px;
background-color: #01AAED;
}
.aside>.layui-collapse {
width: 200px;
}
.aside .layui-colla-title {
background-color: #01AAED;
color: #fff;
}
.aside .layui-nav {
margin-left: -15px;
margin-top: -10px;
margin-bottom: -10px;
width: 197px;
background-color: #fff;
}
.aside .layui-nav .layui-nav-item {
border-bottom: 1px solid #e2e2e2;
border-right: 1px solid #e2e2e2;
}
.aside .layui-nav .layui-nav-item a {
color: #333;
}
.aside .layui-nav .layui-nav-item a:hover {
background-color: lightseagreen;
}
/* 主页面显示区 */
.main {
width: 100%;
}
.main>iframe {
width: 100%;
}
</style>
</head>
<body>
<!-- 头部 -->
<div class="header">
<h3>瑄然软件后台管理系统</h3>
<div>
<span class="layui-icon layui-icon-username">{{$username}}</span>
<a href="/admins/home/logout">退出</a>
</div>
</div>
<div class="container">
<!-- 侧边栏 -->
<div class="aside">
<div class="layui-collapse" lay-accordion>
@foreach($pmenus as $key => $pmenu)
<div class="layui-colla-item">
<h2 class="layui-colla-title">{{$pmenu['title']}}</h2>
<!-- 默认显示第一个菜单的子菜单 -->
<div class="layui-colla-content {{$key?'':'layui-show'}}">
<!-- <div class="layui-colla-content layui-show"> -->
<ul class="layui-nav layui-nav-tree">
<!-- 找到一级菜单所对应的子菜单 -->
<?php $cmenu = $cmenus[$pmenu['mid']];?>
@foreach($cmenu as $item)
<li class="layui-nav-item">
<a href="/admins/{{$item['controller']}}/{{$item['action']}}"
target="showMain">{{$item['title']}}</a>
</li>
@endforeach
</ul>
</div>
</div>
@endforeach
</div>
</div>
<!-- 主页面显示区 -->
<div class="main">
<iframe src="/admins/home/welcome" frameborder="0" name="showMain"></iframe>
</div>
</div>
</body>
<script>
layui.use(['element', 'layer'], function() {
var element = layui.element;
$ = layui.jquery;
var aside_height = document.documentElement.clientHeight - 50;
$('.aside').height(aside_height);
//主页面的高度和侧边栏高度相同
$('.main>iframe').height(aside_height);
});
</script>
</html>
7.点击退出
按钮,用户退出到登录页面
上一篇: php 数据库操作mysqli