abstract:<?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/5/26 * Time: 20:45 */namespace app\admin\controller;use app\admin\controller\Common;use think\facade\Request;use app\model\Product
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/26
* Time: 20:45
*/
namespace app\admin\controller;
use app\admin\controller\Common;
use think\facade\Request;
use app\model\Product as ProductModel;
use think\facade\Session;
class Product extends Common
{
public function index()
{
$products = ProductModel::order('id','desc')->paginate(5);
$this->assign('products',$products);
//渲染產(chǎn)品列表
return $this->fetch();
}
public function add()
{
//渲染產(chǎn)品添加界面
return $this->fetch();
}
//上傳操作
public function upload()
{
//獲取上傳圖片信息
$file = Request::file('img');
//驗(yàn)證圖片信息并移動(dòng)到指定目錄
$info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload');
if ($info) {
//返回成功的提示信息
return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
} else {
return $file->getError();
}
}
//添加操作
public function doAdd()
{
//獲取提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
$title = $data['title'];
$info = ProductModel::where('title',$title)->find();
if ($info == true) {
return ['res'=>0,'msg'=>'產(chǎn)品標(biāo)題重復(fù)'];
}
$data['time'] = time();
//添加發(fā)布管理員
$data['username'] = Session::get('username');
//添加產(chǎn)品信息到數(shù)據(jù)庫(kù)表
$product = ProductModel::create($data);
if ($product) {
return ['res'=>1,'msg'=>'產(chǎn)品發(fā)布成功'];
} else {
return ['res'=>0,'msg'=>'產(chǎn)品發(fā)布失敗'];
}
}
public function edit()
{
$proId = Request::param('id');
$product = ProductModel::get($proId);
$this->assign('product',$product);
//渲染產(chǎn)品編輯界面
return $this->fetch();
}
public function doEdit()
{
//獲取提交過(guò)來(lái)的數(shù)據(jù)
$data = Request::param();
$data['time'] = time();
$data['username'] = Session::get('username');
$product = ProductModel::where('id',$data['id'])->update($data);
if ($product) {
return ['res'=>1,'msg'=>'更新成功'];
}else {
return ['res'=>0,'msg'=>'更新失敗'];
}
}
//刪除
public function del()
{
//獲取需要?jiǎng)h除的產(chǎn)品id
$proId = Request::param('id');
$result = ProductModel::destroy($proId);
if ($result) {
return ['res'=>1,'msg','刪除成功'];
}else {
return ['res'=>0,'msg','刪除失敗'];
}
}
}
Correcting teacher:天蓬老師Correction time:2019-05-27 09:06:33
Teacher's summary:public function del()
{
//獲取需要?jiǎng)h除的產(chǎn)品id
$proId = Request::param('id');
$result = ProductModel::destroy($proId);
if ($result) {
return ['res'=>1,'msg','刪除成功']