GoodsController.php 3.9 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;


class GoodsController extends AdminBaseController
{
    public $table_name_tag = 'goods';

    //列表页
    public function data_list()
    {
        //获取筛选参数
        $param = $this->request->param();
        //1
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['name'] = ['like', "%$keyword%"];
        }
        //2
        $type = empty($param['type']) ? '' : $param['type'];
        if (!empty($type)) {
            $where['type'] = ['eq', $type];
        }

        $where['delete_time'] = 0;
        $list                 = DB::name($this->table_name_tag)->where($where)->select()->toArray();
        $this->assign('list', $list);

        return $this->fetch();
    }

    //列表编辑
    public function data_edit()
    {
        $param = $this->request->param();
        //提交数据
        if ($this->request->isPost()) {
            //添加提交
            $datain['name']        = $param['name'];
            $datain['price']       = $param['price'];
            $datain['des']         = $param['des'];
            $datain['hetong']      = $param['hetong'];
            $datain['create_time'] = time();

            //提交修改
            if (!empty($param['id'])) {
                $datain['id'] = $param['id'];

                $result       = DB::name($this->table_name_tag)->update($datain);
                if ($result) {
                    $this->success('修改成功!', url('data_list'));
                } else {
                    $this->error('添加失败!');
                }
            }else{
                $this->error('暂时无法添加数据!');
                $result                = DB::name($this->table_name_tag)->insert($datain);
                if ($result) {
                    $this->success('添加成功!', url('data_list'));
                } else {
                    $this->error('添加失败!');
                }
            }
        } else {
            //展示表单
            if (!empty($param['id'])) {
                $info = DB::name($this->table_name_tag)->where(['id' => $param['id']])->find();

            }else{
                $info=[];
            }
            $this->assign('info', $info);
            return $this->fetch();
        }

    }

    //列表状态修改
    public function data_status()
    {
        $param = $this->request->param();
        $id    = $param['id'];
        $type  = $param['type'];
        if ($type == 'ban') {
            $field       = 'status';
            $field_value = '0';
            $field_msg   = '停用';
        } elseif ($type == 'cancelban') {
            $field       = 'status';
            $field_value = '1';
            $field_msg   = '启用';
        } elseif ($type == 'del') {
            $field       = 'delete_time';
            $field_value = time();
            $field_msg   = '删除';
        }
        if($field){
            $result = Db::name('goods')->where(["id" => $id])->setField($field, $field_value);
            if ($result != false) {
                $this->success("{$field_msg}成功!");
            } else {
                $this->error("{$field_msg}失败!");
            }
        }else{
            $this->error("数据错误!");
        }

    }


}