摘要:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019-01-31 * Time: 10:18 */namespace app\admin\controller;use app\admin\controller\Common;use think\facade\Request;use think\facade\Ses
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019-01-31
* Time: 10:18
*/
namespace app\admin\controller;
use app\admin\controller\Common;
use think\facade\Request;
use think\facade\Session;
use app\admin\model\ProductModel;
class Product extends Common
{
public function index()
{
$product = new ProductModel();
$products = $product->order('id','desc')->paginate(5);
$this->view->products =$products;
return $this->fetch();
}
public function add()
{
return $this->view->fetch();
}
public function edit()
{
$id =Request::param('id');
$product =ProductModel::get($id);
$this->view->product = $product;
return $this->fetch();
}
public function doadd()
{
$data =Request::param();
$title =$data['title'];
$info = ProductModel::where('title',$title)->find();
if ($info)
{
return ['res'=>0,'msg'=>'产品名称重复'];
}
$data['time']=time();
$data['username']=Session::get('username');
$produst = new ProductModel();
$res =$produst->save($data);
if ($res)
{
return ['res'=>1,'msg'=>'保存成功'];
}
else
{
return ['res'=>0,'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 $file->getError();
}
} //上传图片
public function doedit() //编辑商品
{
$data = Request::param();
$product = new ProductModel();
$data['time']= time();
$data['username']= Session::get('username');
$info = $product-> save([
'title'=>$data['title'],
'desc'=> $data['desc'],
'content'=> $data['content'],
'price1'=>$data['price1'],
'price2'=> $data['price2'],
'time'=> $data['time'],
'username'=> $data['username'],],['id'=>$data['id']] );
if($info)
{
return ['res'=>1, 'msg'=>'更新成功'];
}
else
{
return ['res'=>0,'msg'=>'更新失败'];
}
}
public function del()
{
$id= Request::param('id');
$product = new ProductModel();
$res = $product->destroy($id);
if ($res)
{
return ['res'=>1,'msg'=>' 删除成功'];
}
}
}
批改老师:天蓬老师批改时间:2019-02-03 15:06:10
老师总结:下次不要这样提交了, 一行注释不写, 让人猜吗