GoodController.php 3.0 KB
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2020/10/14
 * Time: 9:30
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\Controller;

class GoodController extends AdminBaseController
{
    /**
     * 商品首页
     * @return mixed
     */
    public function index(){
        $param=$this->request->param();
        $map=[];
        $map['delete_time']=null;
        $data= db('goods')->where($map)->order('id desc')->paginate();
        $data->appends($param);
        $list=$data->items();
        $this->assign([
            'data'=>$list,
            'page'=>$data->render(),
        ]);
        return $this->fetch();
    }

    /**
     * 编辑商品
     */
    public function edit(){
        $id=input('id');
        $data=db('goods')->where('id',$id)->find();
        $data['banner']=json_decode($data['banner']);
        $data['banner_name']=json_decode($data['banner_name']);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 编辑商品
     */
    public function editPost(){
        $id=input('id');
        $data=input();
        $banner=input('banner/a');
        $banner_name=input('banner_name/a');
        if (empty($banner)){

        }else{
            unset($data['banner']);
            unset($data['banner_name']);
            $data['banner']=json_encode($banner,true);
            $data['banner_name']=json_encode($banner_name,true);
        }
        $info=db('goods')->where('id',$id)->update($data);

        if (!empty($info)){
            $this->success('编辑成功!');
        }else{
            $this->error('编辑失败!');
        }
    }

    /**
     * 添加页面
     */
    public function add(){
        return $this->fetch();
    }

    /**
     * 添加提交
     */
    public function addPost(){
        $data=input();
        $banner=input('banner/a');
        $banner_name=input('banner_name/a');
        if (empty($banner)){

        }else{
            unset($data['banner']);
            unset($data['banner_name']);
            $data['banner']=json_encode($banner,true);
            $data['banner_name']=json_encode($banner_name,true);
        }
        $info=db('goods')->insert($data);
        if (!empty($info)){
            $this->success('编辑成功!');
        }else{
            $this->error('编辑失败!');
        }
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function   delete(){
        $id=input('id');
        $update['delete_time']=time();
        $result=db('goods')->where('id',$id)->update($update);
        if ($result){
            $this->success('操作成功!');
        }else{
            $this->error('操作失败!');
        }
    }


    public function online(){
        $id=input('id');
        $update['is_online']=input('status');
        $result=db('goods')->where('id',$id)->update($update);
        if ($result){
            $this->success('操作成功!');
        }else{
            $this->error('操作失败!');
        }
    }

}