产品模块对应控制器代码

Original 2019-02-26 21:22:28 227
abstract:<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019/2/16  * Time: 16:52  */ namespace app\admin\contro
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/16
 * Time: 16:52
 */

namespace app\admin\controller;

use app\admin\controller\Common;
use think\facade\Request;
use app\admin\model\ProductModel;
use think\facade\Session;

class Product extends Common
{
    public function index()
    {
        $data = ProductModel::order('product_id','desc')->paginate();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function add()
    {
        return $this->fetch();
    }
    public function edit()
    {
        $proId = Request::param('product_id');
        $proInfo = ProductModel::get($proId);
        $this->assign('proInfo',$proInfo);
        return $this->fetch();
    }
    public function doAdd()
    {
        $data = Request::param();
        $title = $data['title'];

        $db_title = ProductModel::where('title',$title)->find();

        if($db_title){
            return ['status'=>1,'msg'=>'产品已存在'];
        }
        $data['author'] = Session::get('username');
        $data['pubtime'] = time();
        $productModel = new ProductModel();
        if($productModel->save($data)){
            return ['status'=>0,'msg'=>'添加成功'];
        }else{
            return ['status'=>1,'msg'=>'添加失败'];
        }
    }
    public function doEdit()
    {
        $data = Request::param();

        $data['author'] = Session::get('username');
        $data['pubtime'] = time();
        //echo json_encode($data);exit;
        $productModel = new ProductModel();

        if($productModel->isUpdate(true)->save($data)){
            return ['status'=>0,'msg'=>'更新成功'];
        }else{
            return ['status'=>1,'msg'=>'更新失败'];
        }
    }
    public function del()
    {
        $id = Request::param('product_id');
        $productModel = new ProductModel();
        if($productModel->destroy($id)){
            return ['status'=>0,'msg'=>'删除成功'];
        }else{
            return ['status'=>1,'msg'=>'删除失败'];
        }
    }
    public function upload()
    {
        $file = Request::file('img');
        if($info = $file->validate(['ext'=>'jpg,jpeg,gif,png'])->move('upload')){
            $fileName = str_replace('\\','/',$info->getSaveName());
            return json(['errno'=>0,'data'=>['/upload/'.$fileName]]);
        }else{
            return $file->getError();
        }
    }
}


Correcting teacher:查无此人Correction time:2019-02-27 09:09:20
Teacher's summary:完成的不错。每个方法最好有注释,代码块如果复杂,也要注释。继续加油

Release Notes

Popular Entries