abstract://Admin.php<?php namespace app\admin\controller; use app\admin\model\UserModel; use app\admin\model\RoleModel; use think\facade\Request; class Admin extends Base{ public function admin(){ //
//Admin.php
<?php
namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\RoleModel;
use think\facade\Request;
class Admin extends Base{
public function admin(){
//获取用户信息$res
$res = UserModel::all();
//用户信息追加角色名称
foreach ($res as $key=>$val) {
$res[$key]['iden'] = RoleModel::where('gid',$val['gid'])->value('title'); }
//渲染用户信息
$this->view->res = $res;
return $this->view->fetch();
}
public function add(){
//获取用户id,判断是编辑用户还是添加用户
$id = (int)input('get.id');
//获取编辑用户
$res = UserModel::where('id',$id)->find();
//获取角色列表
$iden = RoleModel::all();
//渲染角色
$this->view->iden = $iden;
//渲染用户
$this->view->res = $res;
return $this->view->fetch();
}
public function save(){
//获取用户信息
$info = Request::param();
if(!$info['username']){ exit(json_encode(['code'=>1,'msg'=>'用户名不能为空!'])); }
if(!$info['password']){ exit(json_encode(['code'=>1,'msg'=>'密码不能为空!'])); }
if(!$info['gid']){ exit(json_encode(['code'=>1,'msg'=>'角色不能为空!'])); }
if(!$info['truename']){ exit(json_encode(['code'=>1,'msg'=>'姓名不能为空!'])); }
$info['password'] = md5($info['username'].$info['password']);
$info['add_time'] = time();
//如果是添加用户,插入记录
if((int)$info['id']==0){
if(UserModel::where('username',$info['username'])->find()){ exit(json_encode(['code'=>2,'msg'=>'用户已存在!'])); }
$res = UserModel::create($info); }
//如果是编辑用户,更新记录
if((int)$info['id']>0){
$res = UserModel::where('id',$info['id'])->update($info);
}
if($res){
exit(json_encode(['code'=>0,'msg'=>'保存成功!'])); }else{ exit(json_encode(['code'=>1,'msg'=>'保存失败!'])); }
}
public function del(){
//删除用户
$id = Request::get('id');
$res = UserModel::where('id',$id)->delete($id);
if($res){
exit(json_encode(['code'=>0,'msg'=>'删除成功!']));
}else{
exit(json_encode(['code'=>1,'msg'=>'删除失败!']));
}
}
}
//渲染页面admin.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
<style type="text/css">
body{padding:20px;}
.header span{background:#009688;margin-left:30px;color:#ffffff;padding:10px;}
.header button{float:right;margin-top: -4px;}
.header div{border-bottom:2px solid #009688;margin-top:9px;}
</style>
</head>
<body>
<div class="header">
<span>管理员列表</span>
<button class="layui-btn layui-btn-sm" onclick="add()">添加</button>
<div></div>
</div>
<table class="layui-table">
<!-- <colgroup>
<col width="150">
<col width="200">
<col>
</colgroup> -->
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>真实姓名</th>
<th>角色</th>
<th>状态</th>
<th>添加时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name='$res' id='vres'}
<tr>
<td>{$vres.id}</td>
<td>{$vres.username}</td>
<td>{$vres.truename}</td>
<td>{$vres.iden}</td>
<td>{$vres.status==0?'正常':'<span style="color:red">禁用</span>'}</td>
<td>{$vres.add_time | date="Y-m-d H:i:s"}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="add({$vres.id})">编辑</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({$vres.id})">删除</button>
</td>
</tr>
{/volist}
</tbody>
</table>
</body>
<script>
layui.use(['layer'],function(){
var layer = layui.layer; $ = layui.jquery;
})
function add(id){
layer.open(
{ type : 2,
title : id>0 ? '编辑管理员' : '添加管理员',
content : id>0 ? '{:url(\'Admin/add\')}?id='+id : '{:url(\'Admin/add\')}',
area:['480px','420px'] }
)
}
function del(id){
layer.confirm('您确定删除吗?',{'icon':3,'btn':['确定','取消']},function(){ $.get('{:url(\'del\')}?id='+id,function(res){
if(res.code==0){ layer.msg(res.msg); setTimeout(function(){window.location.reload();},3000); } if(res.code>0){ layer.msg(res.msg); setTimeout(function(){window.location.reload();},3000); } },'json') })
}
</script>
</html>
//add.php 添加,编辑用户
<!DOCTYPE html>
<html>
<head>
<title>111</title>
<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css">
<script type="text/javascript" src="/static/layui/layui.js"></script>
</head>
<body style="padding:10px">
<form class="layui-form" action="">
<!-- 如果是编辑用户,渲染ID --> <input type="hidden" name="id" value="{$res.id ? : 0}">
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<!-- 如果是编辑用户,渲染名称 -->
<input type="text" name="username" required lay-verify="required" class="layui-input" value="{$res.username}" {$res.id>0 ? 'readonly' : ''}>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密    码</label>
<div class="layui-input-inline">
<input type="password" name="password" required lay-verify="required" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">角    色</label>
<div class="layui-input-inline">
<select name="gid">
<!-- 渲染角色及GID -->
{volist name='$iden' id ='viden'}
<option value="{$viden.gid}" {$res.gid==$viden.gid?'selected':''}>{$viden.title}</option> {/volist}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">姓    名</label>
<div class="layui-input-inline">
<input type="text" name="truename" required lay-verify="required" class="layui-input" value="{$res.truename}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状    态</label>
<div class="layui-input-inline">
<input type="checkbox" name="status" lay-skin="primary" title="禁用" value="1" {$res.status ? 'checked' : ''}>
</div>
</div>
</form>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="save()">保存</button>
</div>
</div>
</body>
</html>
<script>
layui.use(['layer','form'],function(){ var layer = layui.layer; $ = layui.jquery; var form = layui.form; })
function save(){
var id = parseInt($('input[name=id]').val());
var username = $.trim($('input[name=username]').val());
var password = $.trim($('input[name=password]').val());
var truename = $.trim($('input[name=truename]').val());
var gid = parseInt($('select option:selected').val());
var status = parseInt($('input[type=checkbox]:checked').val()?1:0); //console.log(username+password+truename+gid+status); //console.log($('form').serialize()); if(username==''){
layer.alert('用户名不能为空!',{'icon':2}); return; }
if(password==''){ layer.alert('密码不能为空!',{'icon':2}); return; }
if(truename==''){ layer.alert('请输入真实姓名!',{'icon':2}); return; }
$.post('{:url(\'save\')}',{'id':id,'username':username,'password':password,'truename':truename,'gid':gid,'status':status}, function(res){
if(parseInt(res.code) == 0){
layer.msg(res.msg,{icon:1,time:3000}); setTimeout(function(){parent.window.location.reload()},3000) }
if(res.code>0){ layer.msg(res.msg,{'icon':2}); } }
,'json' )
}
</script>
Correcting teacher:查无此人Correction time:2020-02-27 15:32:38
Teacher's summary:完成的不错。熟悉了框架,常规操作对你来说就很简单了。继续加油。