AdminGoodsController.php 5.8 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

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

/**
 * Class AdminGoodsController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'商品管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'商品管理'
 * )
 */
class AdminGoodsController extends AdminBaseController
{
    /**
     * 商品列表
     * @adminMenu(
     *     'name'   => '商品列表',
     *     'parent' => 'portal/AdminGoods/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '商品列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        //接收搜索参数
        $param = $this->request->param();
        //添加搜索条件
        $where = [];
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['number'] = ['like', "%$keyword%"];
        }
        $goods_name = empty($param['goods_name']) ? '' : $param['goods_name'];
        if (!empty($goods_name)) {
            $where['goods_name'] = $goods_name;
        }
        $data = Db::name('goods')
            ->where($where)
            ->order('create_time', 'desc')
            ->paginate('10');
        //向地址传参
        $data->appends($param);

        $this->assign('page', $data->render());
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('goods_name', isset($param['goods_name']) ? $param['goods_name'] : '');
        $this->assign('list', $data);
        return $this->fetch();
    }


    /**
     * 添加商品
     * @adminMenu(
     *     'name'   => '添加商品',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加商品',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }


    /**
     * 添加商品提交
     * @adminMenu(
     *     'name'   => '添加商品提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加商品提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {
        $param = $this->request->param();
        $param['create_time'] = time();
        $param['content']=htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($param['content']),true));
        $re = Db::name('goods')->insert($param);
        if ($re) {
            $this->success('添加成功', 'AdminGoods/index');
        } else {
            $this->error('添加失败', 'AdminGoods/index');
        }
    }


    public function up(){
        $param=$this->request->param();
        Db::name('goods')->where('id',$param['id'])->update(['status'=>1]);
        $this->success('上架成功');
    }
    public function down(){
        $param=$this->request->param();
        Db::name('goods')->where('id',$param['id'])->update(['status'=>0]);
        $this->success('下架成功');
    }

    /**
     * 编辑商品
     * @adminMenu(
     *     'name'   => '编辑商品',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑商品',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $data = Db::name('goods')
            ->where('id', $id)
            ->find();
//        $data['content']=cmf_replace_content_file_url(htmlspecialchars_decode($data['content']));
        $this->assign('list', $data);
        return $this->fetch();
    }

    /**
     * 编辑商品提交
     * @adminMenu(
     *     'name'   => '编辑商品提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑商品提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        $param = $this->request->param();
//        $param['content']=htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($param['content']),true));
        Db::name('goods')->where('id', $param['id'])->update($param);
        $this->success('编辑成功');
    }


    /**
     * 删除商品
     * @adminMenu(
     *     'name'   => '删除商品',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除商品',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $param=$this->request->param();
        if (!empty($param['ids'])){
            Db::name('goods')->where(['id' => ['in', $param['ids']]])->delete();
            $this->success('删除成功');
        }
        Db::name('goods')->where('id', $param['id'])->delete();
        $this->success('删除成功');
    }

}