摘要:<?php namespace app\admin\controller; use Util\SysDb; use think\facade\Request; use app\admin\model\AdminModel; class Admin extends Base { &nbs
<?php
namespace app\admin\controller;
use Util\SysDb;
use think\facade\Request;
use app\admin\model\AdminModel;
class Admin extends Base
{
public function index()
{
// 加载管理员列表
$list = SysDb::getnstance()->table('admins')->order('id desc')->lists();
$this->assign('lists',$list);
return $this->fetch();
}
// 添加管理员
public function add()
{
return $this->fetch();
}
// 添加操作
public function doAdd()
{
// 获取上传数据
$data = Request::param();
// 生成密文
$data['password'] = sha1($data['password']);
// 生成时间
$data['add_time'] = time();
// 查看用户名是否存在
$find = AdminModel::get($data['username']);
if($find) {
return ['code'=>0,'msg'=>'用户名已存在,请重新输入'];
}
// 否则写入数据表
$ins = AdminModel::create($data);
if($ins) {
return ['code'=>1,'msg'=>'用户添加成功'];
}
return ['cdoe'=>0,'msg'=>'用户添加失败'];
}
// 编辑页面
public function edit()
{
// 接收ID
$id = Request::param('id');
$id = (int)$id;
// 查询一条数据
$data = AdminModel::where(['id'=>$id])->find();
// 模板赋值
$this->assign('data',$data);
// 渲染视图
return $this->fetch();
}
// 编辑操作
public function doEdit()
{
// 接收上传数据
$data = Request::param();
// 获取ID
$id = $data['id'];
unset($data['id']);
// 更新数据
if($data['password'])
{
$data['password'] = sha1($data['password']);
} else {
unset($data['password']);
}
$up = AdminModel::where('id',$id)->update($data);
if($up)
{
return ['code'=>1,'msg'=>'数据更新成功'];
}
return ['code'=>0,'msg'=>'数据更新失败'];
}
// 删除操作
public function delete()
{
// 接收删除条件
$id = Request::param();
$id = (int)$id;
// 删除
$res = AdminModel::destroy($id);
if(!$res)
{
return ['code'=>0,'msg'=>'删除失败'];
}
return ['code'=>1,'msg'=>'删除成功'];
}
}
批改老师:查无此人批改时间:2019-05-05 09:24:52
老师总结:完成的不错。tp框架一直占用率比较高。要好好学。继续加油。