abstract:产品模块和新闻模块基本一样,就是多了个价格。<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/28 * Time: 9:42 */ namespace&
产品模块和新闻模块基本一样,就是多了个价格。
<?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(); //查询数据并分页 $products = $product->order('id','desc')->paginate(5); //给模版赋值 $this->view->products=$products; //渲染产品列表首页 return $this->fetch(); } public function add() { //渲染产品添加界面 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() { //获取提交过来的数据 $data = Request::param(); //取出产品名称,用于查询是否重复 $title = $data['title']; //查重 $info = ProductModel::where('title',$title)->find(); //判断返回 if ($info == true){ return ['res'=>0,'msg'=>'标题重复']; } //获取时间和管理员数据 $data['time']=time(); $data['username']=Session::get('username'); //实例化模型 $product = new ProductModel(); //数据入库 if($product->save($data)){ return ['res'=>1,'msg'=>'发布成功']; }else{ return ['res'=>0,'msg'=>'发布失败']; } } public function edit() { //接收传过来的ID $proId = Request::param('id'); //查询对应的数据 $product = ProductModel::get($proId); //将数据赋值给模版 $this->view->product=$product; //渲染产品修改界面 return $this->fetch(); } public function DoEdit() { //获取提交过来的数据 $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() { //获取产品ID $proId = Request::param('id'); $product = new ProductModel(); if($product->destroy($proId)){ return ['res'=>1,'msg'=>'删除成功']; }else{ return ['res'=>0,'msg'=>'删除失败']; } } }
Correcting teacher:天蓬老师Correction time:2019-04-29 09:13:39
Teacher's summary:数据库的许多操作是类似的, 完全可以进行代码复用.....因为很多业务逻辑, 只需要更换参数就可以实现了