abstrait:<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/26 * Time: 22:10 */ namespace app\admin\controller; u
<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/26 * Time: 22:10 */ namespace app\admin\controller; use app\admin\controller\Common; use think\facade\Request; use think\facade\Session; use app\admin\model\ProductModel; // 產(chǎn)品控制器,繼承公共控制器,Common class Product extends Common { // 產(chǎn)品列表 public function index() { // 獲取產(chǎn)品信息 $product = ProductModel::order('id','desc')->paginate(8); // 模板賦值 $this->assign('product',$product); // 渲染產(chǎn)品列表 return $this->fetch(); } // 添加產(chǎn)品頁(yè)面 public function add() { // 渲染添加頁(yè)面 return $this->fetch(); } // 添加產(chǎn)品操作 public function DoAdd() { // 接收表單上傳的數(shù)據(jù) $data = Request::param(); // 獨(dú)立產(chǎn)品標(biāo)題 $title = $data['title']; // 查詢標(biāo)題是否重復(fù) $pro = ProductModel::where('title',$title)->find(); if($pro == true) { return ['code'=>0,'msg'=>'產(chǎn)品標(biāo)題重復(fù)!']; } $data['time'] = time(); $data['username'] = Session::get('username'); $ins = ProductModel::create($data); if($ins == true) { return ['code'=>1,'msg'=>'產(chǎn)品添加成功']; } return ['code'=>0,'msg'=>'產(chǎn)品添加失敗']; } // 編輯頁(yè)面 public function edit() { // 獲取產(chǎn)品ID $proid = Request::param('id'); // 根據(jù)產(chǎn)品ID獲取數(shù)據(jù) $product = ProductModel::where('id',$proid)->find(); // 模板賦值 $this->assign('product',$product); // 渲染模板 return $this->fetch(); } // 編輯操作 public function DoEdit() { // 獲取提交數(shù)據(jù) $data = Request::param(); // 更新條件 $pid = $data['pid']; // 更新操作 $ins = ProductModel::where('id',$pid)->update([ 'title'=>$data['title'], 'desc'=>$data['desc'], 'content'=>$data['content'], 'once'=>$data['once'], 'over_night'=>$data['over_night'], 'time'=>time(), 'username'=>Session::get('username'), ]); if($ins) { // 返回更新成功 return ['code'=>1,'msg'=>'產(chǎn)品信息編輯成功']; } // 返回更新失敗 return ['code'=>0,'msg'=>'產(chǎn)品信息編輯失敗']; } // 圖片上傳 public function upload() { // 獲取上傳圖片信息 $file = Request::file('img'); if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')) { return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); } else { return $info->getError(); } } // 使用軟刪除 public function DoDel() { // 獲取刪除條件 $pid = Request::param('pid'); // 刪除操作 $del = ProductModel::destroy($pid); // 是否刪除成功 if($del) { return ['code'=>1,'msg'=>'刪除成功']; } return ['code'=>0,'msg'=>'刪除失敗']; } }
Professeur correcteur:查無(wú)此人Temps de correction:2019-04-27 17:27:38
Résumé du professeur:完成的不錯(cuò),cms后臺(tái)就是對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行操作。繼續(xù)加油