摘要:<?php namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use think\facade\Request; use think\facade\Session; class P
<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\ProductModel;
use think\facade\Request;
use think\facade\Session;
class Product extends  Common
{
    public function index()
    {
        $product = new ProductModel();
        $products = $product->order('id','desc')->paginate(3);
        $this->view->products=$products;
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function upload()
    {
        $file = Request::file('img');
        if ($info = $file->validate(['ext' => 'jpg,jpeg,png,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();
        $data['time'] = time();
        $data['username'] = Session::get('username');
        if($info == true)
        {
            return ['res' => 0,'msg' => '产品标题重复'];
        }
        $product = new ProductModel();
        if($product->save($data))
        {
            return ['res' => 1,'msg'=>"发布成功"];
        }
        else
        {
            return ['res' => 1,'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' => $data['over'],
                'time' => $data['time'],
                'username' => $data['username'],
            ],['id' => $data['id']]);
        if($info == true)
        {
            return ['res' => 1,'msg' => "修改成功"];
        }
        else
        {
            return ['res' => 0,'msg' => "修改失败"];
        }
    }
    public function del()
    {
        $proId = Request::param('id');
        $product = new ProductModel();
        if($product->destroy($proId))
        {
            return ['res' => 1,'mes' => '删除成功'];
        }
    }
}
						批改老师:韦小宝批改时间:2018-12-26 15:23:54		
						
老师总结:写的很棒,写完这个项目可以自己开发个个人的博客练习练习					
 
                 
 
 
  
            