<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/27 * Time: 13:59 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use app\admin\model\ProductPicModel; use think\facade\Request; use think\facade\Session; class ProductPic extends Common { // 渲染产品缩略图列表 public function index() { // 获取缩略图信息 $prodcuts = ProductPicModel::order('id','desc')->paginate(6); // 模板赋值 $this->assign('products',$prodcuts); // 渲染模板 return $this->fetch(); } // 添加产品缩略图 public function add() { // 查询所有产品数据 $products = ProductModel::field('id,title')->all(); // 模板赋值 $this->assign('products',$products); // 渲染模板 return $this->fetch(); } // 图片上传操作 public function upload() { // 获取图片信息 $file = Request::file('file'); // 验证并移动图片 if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')) { $filePath = '/upload/'.$info->getSaveName(); return json([1,'上传成功','data'=>$filePath]); } return $info->getError(); } // 产品缩略图添加操作 public function DoAdd() { // 获取提交数据 $data = Request::param(); $data['username'] = Session::get('username'); $data['time'] = time(); $ins = ProductPicModel::create($data); if($ins) { return ['code'=>1,'msg'=>'产品缩略图添加成功']; } return ['code'=>0,'msg'=>'产品缩略图添加失败']; } // 删除操作 public function DoDel() { // 获取要删除的ID $pid = Request::param('id'); // 进行软删除操作 $del = ProductPicModel::destroy($pid); // 删除状态 if($del) { return ['code'=>1,'msg'=>'数据删除成功']; } return ['code'=>0,'msg'=>'数据删除失败']; } }