产品缩略图添加查询以及删除操作

Original 2018-12-06 11:19:09 274
abstract:/******************* ProductPic.php 产品缩略图控制器 *******************************/
/******************* ProductPic.php 产品缩略图控制器 *******************************/

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()
{
//实例化模型
$proPic = new ProductPicModel();
$productPic= $proPic->order('id','desc')->paginate(2);
$this->view->productPic = $productPic;

//渲染产品缩略图列表
return $this->fetch();
}
public function add()
{

//查询所有的数据
$product = ProductModel::all();
//模板赋值
$this->view->product = $product;
//渲染产品缩略图添加
return $this->fetch();
}

public function upload()

{
//获取上传图片的信息
$file = Request::file('file');
//验证图片并移动到指定目录
if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')){
//拼接图片路径
$fileName = '/upload/'.$info->getSaveName();
//返回上传成功的提示信息
return json([1,'上传成功!','data'=>$fileName]);
}else{
//返回上传成功的提示信息
return $file->getError();
}
}

public function DoAdd()
{
//获取提交的数据
$data = Request::param();
//添加时间
$data['time'] = time();
//添加管理员
$data['username'] = Session::get('username');
//实例化模型
$proPic = new ProductPicModel();
//存储并验证
if($proPic->save($data)){
//返回对应的数据
return ['res'=>1,'msg'=>'发布成功!'];
}else{
return ['res'=>0,'msg'=>'发布失败!'];
}
}

public function del()
{
//获取要产出产品的id
$productId = Request::param('id');
//实例化模型
$productPic = new ProductPicModel();
//删除并验证
if($productPic->destroy($productId)){
return ['res'=>1,'msg'=>'删除成功!'];
}
}
}

/***************** ProductPicModel.php 产品缩略图模型***********************/

namespace app\admin\model;
use \think\Model;

class ProductPicModel extends Model
{
protected $table = 'product_pic';

protected $pk = 'id';
}

/************************ index.html 产品缩略图列表 *******************************************/

{include file="public/head" /}






共有数据:88 条














{volist name="productPic" id="product"}









{/volist}



图片ID所属产品图片发布管理员发布日期操作


{$product.id}{:GetProTitle($product.product_id)}{$product.username}{$product.time|date="Y-m-d"}
















/*************************** add.html 产品缩略图添加页面 ****************************/

{include file="public/head" /}


























Correcting teacher:天蓬老师Correction time:2018-12-06 11:47:05
Teacher's summary:功能很完整,以后推荐尽可能自己写, 尽可能不要照抄教学源码, 还有,代码应该放在代码块中提交

Release Notes

Popular Entries