审查视图

app/portal/controller/AdminGoodsController.php 5.8 KB
1  
潘浩文 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
<?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('删除成功');
    }

}