class product extends Common{
public funtion index(){
//渲染首页模版
return $this->fetch();
}
public function add(){
//渲染产品添加界面
return $this->fetch();
}
public function upload(){
//获取上传图片信息
$file = Request::file('img');
//验证图片格式并上传至指定目录
if($info = $file->validate(['ext']=>'jpg,jpeg,gif')->move('upload')){
return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
}else{
return $file->getError();
}
}
public function DoAdd(){
//获取提交数据
$data= Request::param();
$title=$data['title'];
$info =ProductModel::where('title',$title)->find();
if($info == true){
return ['res'=>1,'msg'=>'产品标题重复'];
$data['time']=time();
$data['username']=Session::get('username');
$product= new ProductModel();
if($product->save($data)){
return['res'=>1,'msg'=>'添加信息成功!'];
}else{
return ['res'=>0,'msg'=>'添加信息失败!'];
}
}
}
public function edit(){
$proId= Request::param('id');
$product =ProductModel::get($proId);
//赋值给模版
$this->view->product=$product;
//渲染产品编辑页面
return $this->fetch();
}
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'],
'once'=>$data['once'],
'over_night'=>$data['over_night'],
'time'=>$data['time'],
'username'=>$data['username'],
],['id'=>$data['id']]
);
if($info){
return ['res'=>1,'msg'=>'更新成功!'];
}else{
return ['res'=>0,'msg'=>'更新失败!'];
}
}
pupublic funtion del(){
//获取产品ID
$proId = Request::param('id');
$product = new ProductModel();
if($product->destroy($proId)){
return ['res'=>1,'msg'=>'删除成功!'];
}
}
}