亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

產(chǎn)品模塊相關(guān)代碼

原創(chuàng) 2019-05-14 12:20:18 228
摘要:<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019/5/14  * Time: 11:22  */ namespace app\admin\control
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14
 * Time: 11:22
 */
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(5);
        $this->view->products = $products;

        return $this->fetch();
    }

    public function add(){

        return $this->fetch();
    }

    public function edit(){
        $proId = Request::param('id');
        $product = ProductModel::get($proId);
        $this->view->product = $product;
        // 渲染產(chǎn)品修改界面
        return $this->fetch();
    }

    public function DoAdd(){
        $data = Request::param();
        $title = $data['title'];
        $info = ProductModel::where('title',$title)->find();
        if($info == true){
            return ['res'=>0,'msg'=>'產(chǎn)品標(biāo)題重復(fù)~'];
        }

        $data['time']=time();
        $data['username']=Session::get('username');
        $product = new ProductModel();
        if($product->save($data)){
            return ['res'=>1,'msg'=>'產(chǎn)品添加成功~'];
        }else{
            return ['res'=>0,'msg'=>'發(fā)布失敗~'];
        }
    }

    public function upload(){
        // 獲取上傳圖片信息
        $file = Request::file('img');
        // 驗(yàn)證圖片信息并移動(dòng)到指定目錄
        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 DoEdit()
    {
        // 獲取提交過(guò)來(lái)的數(shù)據(jù)
        $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'],
            'price' => $data['price'],
            'time' => $data['time'],
            'username' => $data['username'],
        ], ['id' => $data['id']]);
        if ($info) {
            return ['res' => 1, 'msg' => '更新成功!'];
        } else {
            return ['res' => 0, 'msg' => '更新失??!'];
        }
    }

    public function del(){

        // 獲取需要?jiǎng)h除的產(chǎn)品id
        $proId = Request::param('id');
        $product = new ProductModel();
        if ($product->destroy($proId)) {
            return ['res'=>1,'msg'=>'刪除成功!'];
        }
    }


}


批改老師:天蓬老師批改時(shí)間:2019-05-14 17:48:48
老師總結(jié):凡是在方法中使用: $product = new ProductModel();, 其實(shí)都是不太友好的編程, 可以考慮放在參數(shù)中傳入, 豈不更妙

發(fā)佈手記

熱門詞條