摘要:产品缩略图和新闻缩略图模块几乎一样,需要通过自身表中uid,pid来查询对应的user表中username,和product表中title字段的值.一.缩略图列表页 model/ProductPic.php<?php namespace app\admin\model; use think\Model; use th
产品缩略图和新闻缩略图模块几乎一样,需要通过自身表中uid,pid来查询对应的user表中username,和product表中title字段的值.
一.缩略图列表页
model/ProductPic.php
<?php
namespace app\admin\model;
use think\Model;
use think\model\concern\SoftDelete;
class ProductPic extends Model
{
//使用trait软删除
use SoftDelete;
//表名
protected $table = 'product_pic';
//主键
protected $pk = 'id';
//开启自动时间戳
protected $autoWriteTimestamp = true;
//设置一下用户自定义的新增和更新时间的字段名
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
//设置软删除字段
protected $defaultSoftDelete = 'delete_time';
} 控制器ProductPic.php
<?php
namespace app\admin\controller;
use app\admin\common\Common;
use think\facade\Request;
use app\admin\model\ProductPic as ProductPicModel;
use app\admin\model\Product;
use think\facade\Session;
class ProductPic extends Common
{
//列表模板
public function index()
{
//获取全部数据
$productPic = ProductPicModel::order('id')->paginate(5);
//获取分页
$page = $productPic->render();
//模板赋值
$this->view->assign('productPic',$productPic);
$this->view->assign('page',$page);
return $this->view->fetch();
} 公共函数
//获取产品名称
function getProductTitle($pid)
{
return Db::connect('qiye')->table('product')
->where('id',$pid)->value('title');
}
二.新增操作
新增操作需要注意的是需要将产品表中title/id值传过去,在页面中显示, 需要传到数据表中的数据uid字段为session中的id的值,
//添加页面
public function add()
{
$product = Product::field(['title','id'])->all();
$this->view->assign('title',$product);
return $this->view->fetch();
}
//上传图片操作
public function upload()
{
$img = Request::file('file');
if($img == ''){
return ['errno'=>0,'msg'=>'错误!'];
}
$info = $img->validate(['ext'=>'jpg,png,gif,jpeg','size'=>2000000])->move('upload');
if($info){
return json([1,'上传成功','data'=>['/upload/'.$info->getSaveName()]]);
}else{
return ['erron'=>2,'msg'=>'错误!'];
}
}
//执行添加操作
public function doAdd()
{
//获取提交数据
$data = Request::param();
if($data == ''){
return ['res'=>'0','msg'=>'没有文件被上传'];
}
//uid 为session中的id值
$data['uid'] = Session::get('id');
//执行新增
$res = ProductPicModel::create($data);
if($res){
return ['res'=>1,'msg'=>'上传成功'];
}else{
return ['res'=>0,'msg'=>'上传失败'];
}
}
三.删除操作
依旧软删除,必须有条件
//删除数据
public function del()
{
//获取要删除的id
$id = Request::param('id');
if(!$id){
return ['res'=>0,'msg'=>'非法操作!'];
}
if(ProductPicModel::destroy($id)){
return ['res'=>1,'msg'=>'删除成功'];
}else{
return ['res'=>0,'msg'=>'删除失败!'];
}
}
批改老师:天蓬老师批改时间:2018-12-24 15:22:31
老师总结:看上去,你完成的错, 有点项目的样子了, 不过,还是要细心