Correction status:Uncorrected
Teacher's comments:
thinkphp:使用thinkphp6.0 ,在数据库表里每个管理员都有相对应的id字段,我们点击编辑的时候,会自动获取管理员id字段,进行判断点击的是哪个管理员,当我们修改信息的时候,会判断所填的信息是否为空,如果不为空则添加或修改成功,会默认给一个修改和添加时间,删除就是判断当前点击的管理员字段id,进行where条件查找,然后进行删除
HTML 代码块
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>管理员账号列表</title>
<link rel="stylesheet" href="/static/layui-v2.6.8/layui/css/layui.css" />
<script src="/static/layui-v2.6.8/layui/layui.js"></script>
</head>
<body style="padding: 10px;min-width:737px">
<div class="layui-item" >
<span style="color:#777;font-size:20px;height:30px;line-height:30px">首页/</span>
<span style="color:#ccc">管理员列表</span>
<button class="layui-btn layui-btn-sm" style="float:right;margin:5px" onclick="add()">添加</button>
</div>
<table class="layui-table" >
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>管理员级别</th>
<th>真实姓名</th>
<th>注册时间</th>
<th>状态</th>
<th>编辑</th>
</tr>
</thead>
<tbody>
{foreach $admin as $v}
<tr>
<td>{$v['id']}</td>
<td>{$v['username']}</td>
<td>{$v['gid']}</td>
<td>{$v['truename']}</td>
<td>{:date('Y-m-d H:i:s',$v['add_time'])}</td>
<td style="color:{$v['status']==0 ? 'green' : 'red'};">
{$v['status']==0 ? '开启' : '关闭'}
</td>
<td>
<button class="layui-btn layui-btn-sm " onclick="edit({$v['id']})">编辑</button>
<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="dle({$v['id']})">删除</button>
</td>
</tr>
{/foreach}
</tbody>
</table>
</body>
<script>
$ = layui.jquery;
//修改信息
function edit(aid) {
layer.open({
type:2,
title: '修改信息',
shadeClose: true,
shade: 0.8,
area: ['450px', '450px'],
content: '/admin/Admin/edit?aid='+aid
});
}
//删除
function dle(aid) {
layer.confirm('确定删除吗?', {
btn: ['确定','取消'],
},
function(){
let date = {};
$.post('/admin/Admin/dle?aid='+aid,date,function(res){
if (res.id == 1) {
layer.alert(res.msg,{icon:1});
setTimeout(() => {
window.location.reload();
}, 1000);
}else{
layer.alert(res.msg,{icon:2});
}
},'json')
});
}
//添加管理员
function add()
{
layer.open({
type:2,
title: '添加信息',
shadeClose: true,
shade: 0.8,
area: ['450px', '450px'],
content: '/admin/Admin/add'
});
}
</script>
</html>
<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;
use think\facade\Session;
/**
* 管理员账号管理
*/
class Admin extends Base
{
//账号列表
public function index()
{
//获取信息 渲染到账号列表
$admin = Db::table('admin a')
->select()
->toArray();
//管理员角色
$group = Db::table('admin_group')->field(['gid','title'])->select()->toArray();
foreach ($admin as $key => $av) {
$admin[$key]['gid'] = '';
foreach ($group as $gv) {
if ($av['gid'] == $gv['gid']) {
$admin[$key]['gid'] = $gv['title'];
break;
}
}
}
View::assign([
'admin' => $admin
]);
return View('/admin/index');
}
//管理员编辑
public function edit()
{
$aid =(int)Request::get('aid');
$user = Db::table('admin')->where('id',$aid)->find();
$group = $group = Db::table('admin_group')->field(['gid','title'])->select();
View::assign([
'user'=>$user,
'group'=>$group,
'aid'=>$aid
]);
return View('/admin/edit');
}
//管理员添加页面
public function add()
{
$group = $group = Db::table('admin_group')->field(['gid','title'])->select();
View::assign([
'group'=>$group,
]);
return View('/admin/add');
}
//添加管理员信息
public function add_user()
{
//管理员信息接收
$data['username'] = Request::post('username');
$data['password'] = md5($data['username'].Request::post('password'));
$data['gid'] = Request::post('gid');
$data['truename'] = Request::post('truename');
$data['add_time'] = time();
$data['lastlogin'] = time();
$data['status'] = Request::post('status');
$data['phone'] = Request::post('phone','');
//查询是否有此管理员
if (!empty($data)) {
$find = Db::table('admin')->where('username',$data['username'])->field('username')->find();
}
//添加管理员
if (!empty($find)) {
exit(json_encode(['id'=>0,"msg"=>'该用户名已存在']));
}else {
if (!empty($data)) {
$insert = Db::table('admin')->insert($data);
}
}
if (!empty($insert)) {
exit(json_encode(['id'=>1,"msg"=>'添加成功']));
}else {
exit(json_encode(['id'=>0,"msg"=>'添加失败']));
}
}
//管理员账号修改
public function edit_user()
{
//管理员信息接收
$id = Request::param('aid');
$data['username'] = Request::post('username');
$data['password'] = md5($data['username'].Request::post('password'));
$data['gid'] = Request::post('gid');
$data['truename'] = Request::post('truename');
$data['lastlogin'] = time();
$data['status'] = Request::post('status');
if (!empty($data)) {
//更新用户信息
$update = Db::table("admin")->where('id',$id)->update($data);
}
if (!empty($update)) {
echo json_encode(['id' =>1 ,'msg'=>'修改成功' ]);
}else {
echo json_encode(['id' =>0 ,'msg'=>'修改失败' ]);
}
}
//用户删除
public function dle()
{
$aid = Request::param("aid");
if (!empty($aid)) {
$delete = Db::table("admin")->where('id',$aid)->delete();
}
if (!empty($delete)) {
echo json_encode(['id' => 1 ,'msg'=>'删除成功']);
}else {
echo json_encode(['id' => 0 ,'msg'=>'删除失败']);
}
}
}