????:產(chǎn)品模塊和新聞模塊基本一樣,就是多了個價格。<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/28 * Time: 9:42 */ namespace&
產(chǎn)品模塊和新聞模塊基本一樣,就是多了個價格。
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/28 * Time: 9:42 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use think\facade\Request; use think\facade\Session; class Product extends Common { public function index() { //實例化模型 $product = new ProductModel(); //查詢數(shù)據(jù)并分頁 $products = $product->order('id','desc')->paginate(5); //給模版賦值 $this->view->products=$products; //渲染產(chǎn)品列表首頁 return $this->fetch(); } public function add() { //渲染產(chǎn)品添加界面 return $this->fetch(); } //上傳圖片操作 public function upload() { //獲取上傳圖片信息 $file = Request::file('img'); //判斷圖片是否可用 if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif']) //驗證后綴 ->move('upload')){ //移動圖片到upload return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]); }else{ return $file->getError(); } } //添加方法 public function DoAdd() { //獲取提交過來的數(shù)據(jù) $data = Request::param(); //取出產(chǎn)品名稱,用于查詢是否重復(fù) $title = $data['title']; //查重 $info = ProductModel::where('title',$title)->find(); //判斷返回 if ($info == true){ return ['res'=>0,'msg'=>'標(biāo)題重復(fù)']; } //獲取時間和管理員數(shù)據(jù) $data['time']=time(); $data['username']=Session::get('username'); //實例化模型 $product = new ProductModel(); //數(shù)據(jù)入庫 if($product->save($data)){ return ['res'=>1,'msg'=>'發(fā)布成功']; }else{ return ['res'=>0,'msg'=>'發(fā)布失敗']; } } public function edit() { //接收傳過來的ID $proId = Request::param('id'); //查詢對應(yīng)的數(shù)據(jù) $product = ProductModel::get($proId); //將數(shù)據(jù)賦值給模版 $this->view->product=$product; //渲染產(chǎn)品修改界面 return $this->fetch(); } public function DoEdit() { //獲取提交過來的數(shù)據(jù) $data = Request::param(); $product = new ProductModel(); $data['time'] = time(); $data['username'] = Session::get('username'); $info =$product->save([ 'title'=>$data['title'], 'sort'=>$data['sort'], 'desc'=>$data['desc'], 'content'=>$data['content'], 'once'=>$data['once'], 'over_night'=>$data['over_night'], 'time'=>$data['time'], 'username'=>$data['username'], ],['id'=>$data['id']]); if($info){ return ['res'=>1,'msg'=>'更新成功']; }else{ return ['res'=>0,'msg'=>'更新失敗']; } } public function del() { //獲取產(chǎn)品ID $proId = Request::param('id'); $product = new ProductModel(); if($product->destroy($proId)){ return ['res'=>1,'msg'=>'刪除成功']; }else{ return ['res'=>0,'msg'=>'刪除失敗']; } } }
?? ???:天蓬老師?? ??:2019-04-29 09:13:39
???? ??:數(shù)據(jù)庫的許多操作是類似的, 完全可以進行代碼復(fù)用.....因為很多業(yè)務(wù)邏輯, 只需要更換參數(shù)就可以實現(xiàn)了