<?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; // 产品控制器,继承公共控制器,Common class Product extends Common { // 产品列表 public function index() { // 获取产品信息 $product = ProductModel::order('id','desc')->paginate(8); // 模板赋值 $this->assign('product',$product); // 渲染产品列表 return $this->fetch(); } // 添加产品页面 public function add() { // 渲染添加页面 return $this->fetch(); } // 添加产品操作 public function DoAdd() { // 接收表单上传的数据 $data = Request::param(); // 独立产品标题 $title = $data['title']; // 查询标题是否重复 $pro = ProductModel::where('title',$title)->find(); if($pro == true) { return ['code'=>0,'msg'=>'产品标题重复!']; } $data['time'] = time(); $data['username'] = Session::get('username'); $ins = ProductModel::create($data); if($ins == true) { return ['code'=>1,'msg'=>'产品添加成功']; } return ['code'=>0,'msg'=>'产品添加失败']; } // 编辑页面 public function edit() { // 获取产品ID $proid = Request::param('id'); // 根据产品ID获取数据 $product = ProductModel::where('id',$proid)->find(); // 模板赋值 $this->assign('product',$product); // 渲染模板 return $this->fetch(); } // 编辑操作 public function DoEdit() { // 获取提交数据 $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'=>'产品信息编辑成功']; } // 返回更新失败 return ['code'=>0,'msg'=>'产品信息编辑失败']; } // 图片上传 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'=>'删除失败']; } }