摘要:<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\facade\Session;
use think\facade\Request;
use app\admin\model\product as Product<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\facade\Session;
use think\facade\Request;
use app\admin\model\product as ProductModel;
class Product extends Common
{
public function index()
{
$Products = ProductModel::paginate(2);
$this->assign('products',$Products);
return $this->fetch();
}
public function add()
{
return $this->fetch();
}
public function DoAdd()
{
$data = Request::param();
$data['add_time'] = time();
$data['username'] = Session::get('username');
$product = new ProductModel;
if($product->save($data)){
return ['res' => true,'msg' => '产品添加成功'];
}else{
return ['res' => false,'msg' => '添加失败'];
}
}
public function upload()
{
$file = Request::file('img');
$info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload');
if($info){
return json(['errno' => 0,'data' => ['/upload/'.$info->getSaveName()]]);
}else{
return $info->getError();
}
}
public function edit()
{
$id = Request::param();
$product = ProductModel::get($id);
$this->assign('product',$product);
return $this->fetch();
}
public function DoEdit()
{
$data = Request::param();
// $data['username'] = Session::get('username');
// $data['edit_time'] = time();
$productData = new ProductModel;
$res = $productData->save([
'title' => $data['title'],
'content' => $data['content'],
'sort' => $data['sort'],
'desc' => $data['desc'],
'once' => $data['once'],
'more_once' => $data['more_once'],
'edit_time' => time()
],['id' => $data['id']]);
if($res){
return ['res' => true,'msg' => '修改产品成功'];
}else{
return ['res' => false,'msg' => '修改失败'];
}
}
public function del()
{
$id = Request::param();
if($product = ProductModel::get($id)){
if($product->delete()){
return ['res' => true,'msg' => '删除产品成功'];
}else{
return ['res' => false,'msg' => '删除失败'];
}
}
return ['res' => false,'msg' => '删除失败,请联系管理员'];
}
}
产品缩略图<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\productPic as ProductPicModel;
use app\admin\model\product as ProductModel;
use think\facade\Request;
use think\facade\Session;
class ProductPic extends Common
{
public function index()
{
$infos = ProductPicModel::paginate(2);
$this->assign('infos',$infos);
return $this->view->fetch();
}
public function add()
{
// ProductPicModel::get(1);
$infos = ProductModel::field('id,title')->all();
// dump($infos);
$this->assign('infos',$infos);
return $this->fetch();
}
public function DoAdd()
{
$data = Request::param();
$data['add_time'] = time();
$data['username'] = Session::get('username');
$addPro = new ProductPicModel;
if($addPro->save($data)){
return ['res' => true,'msg' => '上传产品图片成功'];
}else{
return ['res' => false,'msg' => '上传失败'];
}
}
public function upload()
{
$file = Request::file('file');
$info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload');
if($info){
$fileName = '/upload/'.$info->getSaveName();
return json(['errno' => 0,'data' => $fileName]);
}else{
return $file->getError;
}
}
public function del()
{
$id = Request::param();
if($delData = ProductPicModel::get($id)){
if($delData->delete()){
return ['res' => true,'msg' => '删除产品图片成功'];
}else{
return ['res' => false,'msg' => '删除失败'];
}
}
return ['res' => false, 'msg' => '要删除的数据不存在,请联系管理员'];
}
}
批改老师:韦小宝批改时间:2019-01-11 13:48:05
老师总结:写的很不错 这里的数据返回还可以改用json的形式哦