abstract:<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use think\Controller; use think\facade\Session; use think\faca
<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use think\Controller; use think\facade\Session; use think\facade\Request; class Product extends Common { public function index() { $num = 8; $type = false; $config = [ 'type' => 'bootstrap', 'var_page' => 'page' ]; // 分頁 $data = ProductModel::order('id','asc')->paginate($num,$type,$config); $page = $data->render(); // 分頁數(shù)據(jù) $this->view->assign('pagedata',$data); // 分頁 $this->view->assign('page',$page); // 模版渲染 return $this->view->fetch(); } public function add() { // 模版渲染 return $this->view->fetch(); } public function DoAdd() { // 添加 $data = Request::param(); // 判斷產(chǎn)品標(biāo)題是否重復(fù) $title = $data['title']; $titles = ProductModel::where('title',$title)->find(); if($titles){ return ['res' => 0,'msg' => '產(chǎn)品標(biāo)題重復(fù)']; } // time session $data['time'] = time(); $data['username'] = Session::get('username'); // insert $res = ProductModel::insert($data); // return if($res){ return ['res' => 1,'msg' => '產(chǎn)品添加成功~~~']; }else{ return ['res' => 0,'msg' => '產(chǎn)品添加失敗!']; } } public function edit() { // 根據(jù)id查詢出當(dāng)前編輯頁面數(shù)據(jù) $id = Request::param('id'); // 不能用select $data = ProductModel::where('id',$id)->find(); if($data){ //賦值給編輯頁面 $this->view->assign('data',$data); }else{ return ['res' => 0,'msg' => '不存在該條數(shù)據(jù)!']; } // 模版渲染 return $this->view->fetch(); } public function DoEdit() { // 獲取數(shù)據(jù) $data = Request::param(); $id = $data['id']; // session $username = Session::get('username'); // 更新操作 $res = ProductModel::where('id',$id)->update([ 'title' => $data['title'], 'sort' => $data['sort'], 'desc' => $data['desc'], 'content' => $data['content'], 'once' => $data['once'], 'over_night' => $data['over_night'], 'time' => time(), 'username' => $username, ]); // 結(jié)果返回 if($res){ return ['res' => 1,'msg' => '產(chǎn)品修改成功']; }else{ return ['res' => 0,'msg' => '修改失??!']; } } public function upload() { // 圖片上傳 $img = Request::file('img'); // 驗(yàn)證 $res = $img->validate(['ext' => 'jpg,jpeg,png'])->move('uploads/product'); // 返回name名稱 if($res){ return json(['errno' => 0,'data' => ['/uploads/product/'.$res->getSaveName()]]); }else{ return $res->getError(); } } public function del() { // 獲取del Id $id = Request::param('id'); // 刪除操作 $res = ProductModel::where('id',$id)->delete(); if($res){ return ['res' => 1,'msg' => '刪除成功!']; }else{ return ['res' => 0,'msg' => '刪除失敗']; } } }
所有的CURD操作都測試通過,查詢操作進(jìn)行分頁;編輯操作先根據(jù)主鍵ID查詢數(shù)據(jù)賦值給模版頁面,再進(jìn)行更新;添加操作需要判斷標(biāo)題是否重復(fù),ajax發(fā)送異步請求;刪除操作是直接刪除的,沒有做軟刪除
Correcting teacher:查無此人Correction time:2019-05-23 13:23:15
Teacher's summary:完成的不錯。后臺cms管理系統(tǒng),就是對數(shù)據(jù)進(jìn)行操作。操作越簡單越好。繼續(xù)加油。