????:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/5/14 * Time: 11:22 */ namespace app\admin\control
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/5/14 * Time: 11:22 */ 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(); $products = $product->order('id','desc')->paginate(5); $this->view->products = $products; return $this->fetch(); } public function add(){ return $this->fetch(); } public function edit(){ $proId = Request::param('id'); $product = ProductModel::get($proId); $this->view->product = $product; // 渲染產(chǎn)品修改界面 return $this->fetch(); } public function DoAdd(){ $data = Request::param(); $title = $data['title']; $info = ProductModel::where('title',$title)->find(); if($info == true){ return ['res'=>0,'msg'=>'產(chǎn)品標題重復~']; } $data['time']=time(); $data['username']=Session::get('username'); $product = new ProductModel(); if($product->save($data)){ return ['res'=>1,'msg'=>'產(chǎn)品添加成功~']; }else{ return ['res'=>0,'msg'=>'發(fā)布失敗~']; } } 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 $file->getError(); } } 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'], 'desc' => $data['desc'], 'content' => $data['content'], 'price' => $data['price'], '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'=>'刪除成功!']; } } }
?? ???:天蓬老師?? ??:2019-05-14 17:48:48
???? ??:凡是在方法中使用: $product = new ProductModel();, 其實都是不太友好的編程, 可以考慮放在參數(shù)中傳入, 豈不更妙