返回user控制器...登陆

user控制器

会上树的猪2019-06-14 13:21:53306

<?php
namespace app\admin\controller;
//use think\facade\View;  //view创建静态代理

use app\admin\model\UserModel; //导入模型
use think\facade\Request;//系统控制器
use think\Controller;

class User extends Controller
{
    public function index()
    {
        //实例化模型
        $user = new UserModel();
        //halt($user);
        //按照id降序排列 并且每页展示8条数据
        $users = $user->order('id', 'desc')->paginate(8);
        //将数据赋值给模板
        $this->view->users = $users;
     //渲染管理员界面
        return $this->fetch();
    }

    public function add()
    {
     //渲染管理员添加界面
        return $this->fetch();
    }

    public function DoAdd()
    {
        // 获取前台提交过来的数据
        $data = Request::param();
        // 获取添加的时间
        $data['time'] = time();
        $username = $data['username'];
        // 使用用户名来查询数据库是否有对应的数据
        $res = UserModel::where('username', $username)->find();
        // 判断数据是否存在
        if ($res == true) {
            return ['res' => 0, 'msg' => '用户名已存在!'];
        }
        // 实例化模型
        $user = new UserModel();
        // 验证数据是否存入数据库
        if ($user->save($data)) {
            return ['res' => 1, 'msg' => '添加成功!'];
        } else {
            return ['res' => 0, 'msg' => '添加失败!'];
        }

    }

    public function edit()
    {
        // 获取提交过来的id
        $userId = Request::param('id');
        //通过用户id查询需要更改的用户数据
        $user = UserModel::get($userId);
        //将数据赋值给模板
        $this->view->user = $user;
        //渲染管理员编辑界面
        return $this->fetch();       
    }
   
    public function DoEdit()
    {
        // 获取前台提交过来的数据
        $data = Request::param(); 
        //实例化模型
        $user = new UserModel();
        //对数库中的数据进行修改更新
        $res = $user->save([
            'username' => $data['username'],
            'password' => $data['password'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'time' => time(),
        ],['id' => $data['id']]);
        if($res) {
            return['res'=>1,'msg'=>'修改成功!'];
        }
    }

   public function del()
   {
        // 获取提交过来的id
        $userId = Request::param('id');
        //实例化模型
        $user = new UserModel(); 
        //删除并验证操作
        if($user->destroy($userId)){
            //返回提示信息
            return['res'=>1,'msg'=>'删除成功!'];
        }
   }
}


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • php.cn