摘要:<?php /** * Created by PhpStorm. * User: 叶子 * Date: 2019/3/2 * Time: 18:33 */ namespace app\admin\controller; sessio


<?php
/**
* Created by PhpStorm.
* User: 叶子
* Date: 2019/3/2
* Time: 18:33
*/
namespace app\admin\controller;
session_start();
use app\model\User;
use pig\core\Controller;
class Index extends Controller
{
public function __construct()
{
parent::__construct();
}
// test
public function test($name = 'leaf', $age = 12)
{
return 'I am ' . $name . ' now ' . $age;
}
// testview 演示plates模板引擎的基本使用
public function testView()
{
// 变量
$site = '神奇宝贝';
// 数组
$course = ['皮卡丘', '波加曼', '天蝎王'];
// 对象
$zhi = new \stdClass();
$zhi->name = '小智';
$zhi->age = 10;
$zhi->course = '神奇宝贝大师';
// 模板赋值
$this->assign('site', $site);
$this->
// 模板渲染
$this->fetch(__DIR__ . '/../view/index/testview.html');
}
// 用户信息列表
public function index()
{
$rows = (new User())->select('user', ['id', 'name', 'email', 'dept', 'art', 'create_time'],[
// 搜索功能,如果用户提交了搜索条件,则根据条件搜索,否则显示全部
'dept[~]' => isset($_POST['dept']) ? $_POST['dept'] :null
]);
// 渲染模板
return $this->view->render('admin::index/index', [
'rows' => $rows,
'title' => '武榜',
'loginUrl' => '/admin/index/login', // 登录
'logoutUrl' => '/admin/index/logout', // 登出
'indexUrl' => '/admin/index/index', // 返回首页
'insUrl' => 'admin/index/insert', // 添加操作
'editUrl' => 'admin/index/edit', // 编辑操作
'delUrl' => 'admin/index/delete' // 删除操作
]);
}
// 管理员登录
public function login()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 验证用户
$res = (new User())->get('admin', ['name', 'email', 'password'], [
'AND' => [
'email' => $_POST['email'],
'password' => sha1($_POST['password']),
]
]);
if ($res == false) {
echo "<script>alert('用户名或密码错误');location.href='/';</script>";
} else {
$_SESSION['name'] = $res['name'];
echo "<script>alert('登陆成功');location.href='/';</script>";
}
}
}
// 退出登录
public function logout()
{
session_destroy();
echo "<script>alert('退出成功');location.href='/';</script>";
}
// 添加数据 1. 渲染一个添加数据的表单 2. 添加操作
public function insert()
{
return $this->view->render('admin::index/insert', [
'title' => '添加数据',
'url' => '/admin/index/add',
]);
}
// 添加操作
public function add()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 执行添加操作
(new User())->insert('user', [
'name' => $_POST['name'],
'dept' => $_POST['dept'],
'art' => $_POST['art'],
'email' => $_POST['email'],
'create_time' => time(),
]);
echo "<script>alert('添加成功');location.href='/';</script>";
}
}
// 编辑操作
// 渲染编辑模板
public function edit($id = '')
{
$row = (new User())->get('user', ['id', 'name', 'email', 'dept', 'art'], ['id' => $id]);
return $this->view->render('admin::index/edit',[
'title' => '编辑用户',
'url' => '/admin/index/save',
'row' => $row,
]);
}
public function save($id)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 执行编辑操作
(new User())->update('user', [
'name' => $_POST['name'],
'dept' => $_POST['dept'],
'art' => $_POST['art'],
'email' => $_POST['email'],
],['id'=>$id]);
echo "<script>alert('编辑成功');location.href='/';</script>";
}
}
// 删除操作
public function delete($id)
{
(new User())->delete('user', ['id' => $id]);
echo "<script>alert('删除成功');location.href='/';</script>";
}
}
批改老师:韦小宝批改时间:2019-03-06 09:12:35
老师总结:写的还是很不错的 整体的输入框还是可以改进改进的哦 其他的逻辑什么的都没任何毛病