小型用户管理系统案例

原创 2018-12-18 11:56:10 226
摘要:public function login()     {         if ($_SERVER['REQUEST_METHOD'] == 'POST') {   
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 (false === $res) {
                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>";
    }



    //用户信息列表, 搜索功能也会执行这个操作
    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',[
            'title'=>'武林高手登记表',
            'rows'=> $rows,
            'loginUrl' => '/admin/index/login',
            'logoutUrl' => '/admin/index/logout',
            'indexUrl' => '/admin/index/index',
            'insUrl' => '/admin/index/insert',
            'editUrl' => '/admin/index/edit',
            'delUrl' => '/admin/index/delete',
        ]);
    }

    //添加数据:分二步: 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'],
                    'email'=>$_POST['email'],
                    'dept'=>$_POST['dept'],
                    'art'=>$_POST['art'],
                    'create_time'=>time(),
                ]);
                echo "<script>alert('添加成功');location.href='/';</script>";
       }
    }


    //渲染编辑模板
    public function edit($id='')
    {


       $row = (new User())->get('user',['id','name','dept','art','email'],['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'],
                'email' => $_POST['email'],
                'dept'=>$_POST['dept'],
                'art'=>$_POST['art'],
            ], ['id' => $id]);

            //提示用户信息
            echo "<script>alert('更新成功');location.href='/';</script>";
        }
    }


    //执行删除操作
    public function delete($id)
    {


        //执行删除操作
        (new User())->delete('user',['id'=>$id]);

        //提示用户信息
        echo "<script>alert('删除成功');location.href='/';</script>";
    }

}


批改老师:韦小宝批改时间:2018-12-18 11:57:48
老师总结:写的很不错!课后记得要多练习哦!继续加油吧!

发布手记

热门词条