abstract:<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019-02-19 * Time: 16:15 */namespace app\admin\controller;use app\admin\controller\Common;use app\admin\model\
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019-02-19
* Time: 16:15
*/
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(4);
//赋值模板变量
$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'=>'删除成功!'];
}
}
}
Correcting teacher:韦小宝Correction time:2019-03-02 13:26:15
Teacher's summary:整体的看并不是很复杂 实际上这里的上传可以放到公共文件中 所有的上传都公用一个