管理员登录的一些操作

Original 2019-06-06 15:59:28 190
abstract:<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/lay
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css">
	<script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
	<style type="text/css">
		.header span{background: #009688;margin-left: 30px;padding: 10px;color: #ffffff;}
		.header button{float: right;margin-top: -5px;}
		.header div{border-bottom: solid 2px #009688;margin-top: 8px;}
	</style>
</head>
<body style="padding: 10px;">
	<div class="header">
		<span>管理员列表</span>
		<button class="layui-btn layui-btn-sm" onclick="add()">添加</button>
		<div></div>
	</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>
			{volist name='$lists' id="vo"}
			<tr>
				<td>{$vo.id}</td>
				<td>{$vo.username}</td>
				<td>{$vo.truename}</td>
				<td>{$vo.gid}</td>
				<td>{$vo.status==0?'正常':'<span style="color: red;">禁用</span>'}</td>
				<td>{:date('Y-m-d H:i:s',$vo.add_time)}</td>
				<td>
					<button class="layui-btn layui-btn-xs" onclick="add({$vo.id})">编辑</button>
					<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del({$vo.id})">删除</button>
				</td>
			</tr>
			{/volist}
		</tbody>
	</table>
</body>
</html>
<script type="text/javascript">
	layui.use(['layer'],function(){
		layer = layui.layer;
		$ = layui.jquery;
	});

	// 添加
	function add(id){
		layer.open({
			type:2,
			//点击添加就是添加,编辑就是编辑;只是省去一个编辑文件;
			title:id>0?'编辑管理员':'添加管理员',
			shade:0.3,
			area:['480px','420px'],
			content:'/index.php/admins/admin/add?id='+id
		});
	}

	// 删除
	function del(id){
		layer.confirm('确定要删除吗?',{
			icon:3,
			btn:['确定','取消']
		},function(){
			$.post('/index.php/admins/admin/delete',{'id':id},function(res){
				if(res.code>0){
					layer.alert(res.msg,{'icon':2});
				}else{
					layer.msg(res.msg,{'icon':1});
					setTimeout(function(){window.location.reload();},1000);
				}
			},'json');
		});
	}
</script>
<?php
namespace app\admins\controller;
use think\Controller;
use Util\SysDb;		//引入自己定义的操作数据库类;

//继承魔术方法类;
class Admin extends Base{
	public function index(){
		// 加载管理员列表
		$data['lists'] = $this->db->table('admins')->order('id desc')->lists();
		return $this->fetch('',$data);
	}

	// 添加管理员页面显示;	//省一步,没有编辑文件;
	public function add(){
		//点击添加,传过来的id为,所以在数据库中找到的信息就为空,然后将这个信息显示在页面就没有数据;
		$id = (int)input('get.id');	//强制转换成整数类型;
		$data['item'] = $this->db->table('admins')->where(array('id'=>$id))->item();

		return $this->fetch('',$data);
	}


	// 修改和添加管理员
	public function save(){
		//强制转换成整数类型;
		$id = (int)input('post.id');
		$data['username'] = trim(input('post.username'));
		$data['gid'] = (int)input('post.gid');
		$data['truename'] = trim(input('post.truename'));
		$data['status'] = (int)input('post.status');
		$password = input('post.password');

		if(!$data['username']){
			exit(json_encode(array('code'=>1,'msg'=>'用户名不能为空')));
		}
		if(!$data['gid']){
			exit(json_encode(array('code'=>1,'msg'=>'角色不能为空')));
		}
		if(!$data['truename']){
			exit(json_encode(array('code'=>1,'msg'=>'姓名不能为空')));
		}

		//接收的id为0时就是添加,
		if($id==0 && !$password){
			exit(json_encode(array('code'=>1,'msg'=>'请输入密码')));
		}
		//将密码用md5进行加密;
		if($password){
			$data['password'] = md5($data['username'].$password);
		}
		
		$res = true;
		//添加操作;
		if($id == 0){
			$item = $this->db->table('admins')->where(array('username'=>$data['username']))->item();
			if($item){
				exit(json_encode(array('code'=>1,'msg'=>'该用户已经存在')));
			}
			$data['add_time'] = time();
			$res = $this->db->table('admins')->insert($data);
		}else{
			$res = $this->db->table('admins')->where(array('id'=>$id))->update($data);
		}
		if($res){
			exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
		}else{
			exit(json_encode(array('code'=>1,'msg'=>'保存失败')));
		}
		
	}


	// 删除操作
	public function delete(){
		$id = (int)input('post.id');
		$res = $this->db->table('admins')->where(array('id'=>$id))->delete();
		if(!$res){
			exit(json_encode(array('code'=>1,'msg'=>'删除失败')));
		}
		exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
	}
}


Correcting teacher:查无此人Correction time:2019-06-10 09:14:10
Teacher's summary:完成的不错。php的框架多学几款,对以后工作有帮助。继续加油

Release Notes

Popular Entries