摘要:<?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()
{
//查詢數(shù)據(jù)獲得列表
$proPic = new ProductPicModel();
$productPic = $proPic -> order('id','desc') -> paginate(4);
//賦值模板變量
$this->view->productPic = $productPic;
//渲染模板
return $this->fetch();
}
public function add()
{
//查詢所有產(chǎn)品數(shù)據(jù)
$product = ProductModel::all();
//將數(shù)據(jù)賦值給模板
$this->view->product=$product;
//渲染產(chǎn)品縮略圖添加界面
return $this->fetch();
}
public function upload()
{
//獲取圖片信息
$file = Request::file('file');
//驗(yàn)證圖片信息并且移動(dòng)到指定目錄
if($info=$file->validate(['ext'=>'jpg,jpeg,png,gif'])->move('upload')){
//拼接路徑
$fileName = '/upload/'.$info->getSaveName();
//返回提示信息
return json([1,'上傳成功!','data'=>$fileName]);
} else {
//返回錯(cuò)誤信息
return $file->getError();
}
}
public function DoAdd()
{
//獲取提交來的數(shù)據(jù)
$data = Request::param();
//添加時(shí)間
$data['time'] = time();
//添加管理員
$data['username'] = Session::get('username');
//實(shí)例化產(chǎn)品縮略圖模型
$proPic = new ProductPicModel();
//存儲(chǔ)并驗(yàn)證
if($proPic->save($data)){
//返回?cái)?shù)據(jù)
return ['res'=>1,'msg'=>'發(fā)布成功!'];
}else {
//返回?cái)?shù)據(jù)
return ['res'=>0,'msg'=>'發(fā)布失敗!'];
}
}
public function del()
{
//獲取要?jiǎng)h除的產(chǎn)品id
$productId = Request::param('id');
//實(shí)例化模型
$productPic = new ProductPicModel();
//刪除并驗(yàn)證
if($productPic->destroy($productId)){
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}
批改老師:韋小寶批改時(shí)間:2019-03-02 13:26:15
老師總結(jié):整體的看并不是很復(fù)雜 實(shí)際上這里的上傳可以放到公共文件中 所有的上傳都公用一個(gè)