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

產(chǎn)品模塊和新聞模塊基本一樣

asal 2019-04-28 16:42:04 294
abstrak:產(chǎn)品模塊和新聞模塊基本一樣,就是多了個(gè)價(jià)格。<?php /**  * Created by PhpStorm.  * User: Administrator  * Date: 2019/4/28  * Time: 9:42  */ namespace&

產(chǎn)品模塊和新聞模塊基本一樣,就是多了個(gè)價(jià)格。

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/28
 * Time: 9:42
 */

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()
    {
        //實(shí)例化模型
        $product = new ProductModel();
        //查詢數(shù)據(jù)并分頁(yè)
        $products = $product->order('id','desc')->paginate(5);

        //給模版賦值
        $this->view->products=$products;
        //渲染產(chǎn)品列表首頁(yè)
        return $this->fetch();
    }

    public function add()
    {
        //渲染產(chǎn)品添加界面
        return $this->fetch();
    }

    //上傳圖片操作
    public function upload()
    {
        //獲取上傳圖片信息
        $file = Request::file('img');

        //判斷圖片是否可用
        if($info = $file->validate(['ext'=>'jpg,jpeg,png,gif'])   //驗(yàn)證后綴
            ->move('upload')){                                    //移動(dòng)圖片到upload
            return json(['errno'=>0,'data'=>['/upload/'.$info->getSaveName()]]);
        }else{
            return $file->getError();
        }
    }

    //添加方法
    public function DoAdd()
    {
        //獲取提交過(guò)來(lái)的數(shù)據(jù)
        $data = Request::param();
        //取出產(chǎn)品名稱,用于查詢是否重復(fù)
        $title = $data['title'];

        //查重
        $info = ProductModel::where('title',$title)->find();
        //判斷返回
        if ($info == true){
            return ['res'=>0,'msg'=>'標(biāo)題重復(fù)'];
        }

        //獲取時(shí)間和管理員數(shù)據(jù)
        $data['time']=time();
        $data['username']=Session::get('username');

        //實(shí)例化模型
        $product = new ProductModel();

        //數(shù)據(jù)入庫(kù)
        if($product->save($data)){
            return ['res'=>1,'msg'=>'發(fā)布成功'];
        }else{
            return ['res'=>0,'msg'=>'發(fā)布失敗'];
        }

    }

    public function edit()
    {
        //接收傳過(guò)來(lái)的ID
        $proId = Request::param('id');

        //查詢對(duì)應(yīng)的數(shù)據(jù)
        $product = ProductModel::get($proId);

        //將數(shù)據(jù)賦值給模版
        $this->view->product=$product;
        //渲染產(chǎn)品修改界面
        return $this->fetch();
    }

    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'],
            'sort'=>$data['sort'],
            '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'=>'更新失敗'];
        }
    }

    public function del()
    {
        //獲取產(chǎn)品ID
        $proId = Request::param('id');

        $product = new ProductModel();
        if($product->destroy($proId)){
            return ['res'=>1,'msg'=>'刪除成功'];
        }else{
            return ['res'=>0,'msg'=>'刪除失敗'];
        }
    }
}


Guru membetulkan:天蓬老師Masa pembetulan:2019-04-29 09:13:39
Rumusan guru:數(shù)據(jù)庫(kù)的許多操作是類似的, 完全可以進(jìn)行代碼復(fù)用.....因?yàn)楹芏鄻I(yè)務(wù)邏輯, 只需要更換參數(shù)就可以實(shí)現(xiàn)了

Nota Keluaran

Penyertaan Popular