摘要:<?php namespace app\admin\controller; use app\admin\controller\Common; use think\Controller; use think\facade\Request; use app\admin\model\NewsModel; use think\facade\
<?php namespace app\admin\controller; use app\admin\controller\Common; use think\Controller; use think\facade\Request; use app\admin\model\NewsModel; use think\facade\Session; class News extends Common { public function index() { $num = 10; $type = false; $config = [ 'type' => 'bootstrap', 'var_page' => 'page', ]; $pagedata = NewsModel::order('id','asc')->paginate($num,$type,$config); $page = $pagedata->render(); // $this->view->assign('pagedata',$pagedata); $this->view->assign('page',$page); // 渲染 return $this->view->fetch(); } // 圖片上傳 public function upload() { $res = Request::file('img'); $file = $res->validate(['ext' => 'jpg,jpeg,png'])->move('uploads/news'); if($file){ return json(['errno' => 0,'data' => ['/uploads/news/'.$file->getSaveName()]]); }else{ return $res->getError(); } } public function add() { // 渲染 return $this->view->fetch(); } // 新聞添加 public function DoAdd() { // param $data = Request::param(); // 判斷標(biāo)題是否重復(fù) $title = NewsModel::where('title','=',$data['title'])->find(); if($title){ return ['res' => 0,'msg' => '新聞標(biāo)題重復(fù)']; } // time $data['time'] = time(); // session 管理員姓名 $data['username'] = Session::get('username'); $info = NewsModel::insert($data); if($info){ return ['res' => 1,'msg' => '新聞添加成功!']; }else{ return ['res' => 0,'msg' => '新聞添加失敗']; } } public function edit() { $newsId = Request::param('id'); $res = NewsModel::where('id','=',$newsId)->find(); if($res){ $this->view->assign('news',$res); }else{ return ['res' => 0,'msg' => 'id不存在!']; } // 渲染 return $this->view->fetch(); } public function DoEdit() { // 獲取param $data = Request::param(); // 獲取主鍵id $newsId = $data['id']; // 獲取session $data['username'] = Session::get('username'); // 執(zhí)行更新操作 $res = NewsModel::update([ 'title' => $data['title'], 'desc' => $data['desc'], 'content' => $data['content'], 'username' => $data['username'], 'time' => time(), ],function($query) use ($newsId){ $query->where('id',$newsId); }); // 結(jié)果返回 if($res){ return ['res' => 1,'msg' => '修改成功!']; }else{ return ['res' => 0,'msg' => '修改失??!']; } } public function del() { // del $newsId = Request::param('id'); $res = NewsModel::where('id',$newsId)->delete(); if($res){ return ['res' => 1,'msg' => '刪除成功']; }else{ return ['res' => 0,'msg' => '刪除失敗']; } } }
批改老師:查無此人批改時(shí)間:2019-05-13 09:25:20
老師總結(jié):完成的不錯(cuò)。后臺(tái)cms系統(tǒng),就是管理數(shù)據(jù)庫的,增刪查改是最常用的,繼續(xù)加油。