AdminPrizeController.php 4.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 function Qiniu\thumbnail;
use think\Db;


class AdminPrizeController extends AdminBaseController
{

    public function index()
    {
        //接收搜索参数
        $param = $this->request->param();
        //添加搜索条件
        $where=[];
        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['p.name'] = ['like', "%$keyword%"];
        }
        $category=empty($param['category']) ? '' : $param['category'];
        if (!empty($category)) {
            $where['p.category_id'] = $category;
        }
        $data = Db::name('prize')
            ->alias('p')
            ->join('prize_category pc','p.category_id=pc.id')
            ->where($where)
            ->field('p.*,pc.name as pcname')
            ->order('p.create_time','desc')
            ->paginate('15')
            ->each(function ($item) {
                $item['order'] = Db::name('prize_order') ->where('prize_id',$item['id'])->count();
                return $item;
            });

        $data->appends($param);

        $ca=Db::name('prize_category')->select();
        $this->assign('page',$data->render());
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('ca', isset($param['category']) ? $param['category'] : '');
        $this->assign('category',$ca);
        $this->assign('list', $data);
        return $this->fetch();
    }

    public function add()
    {
        $ca=Db::name('prize_category')->select();
        $this->assign('category',$ca);
        return $this->fetch();
    }

    public function addPost()
    {
        $param = $this->request->param();
        $param['create_time'] = time();
        if (!empty($param['photo_names']) && !empty($param['photo_urls'])) {
            $param['more']['photos'] = [];
            foreach ($param['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($param['more']['photos'], ["url" => $photoUrl, "name" => $param['photo_names'][$key]]);
            }
            unset($param['photo_urls']);
            unset($param['photo_names']);
            $param['more']=json_encode($param['more']);
        }
        $re = Db::name('prize')->insert($param);
        if ($re) {
            $this->success('添加成功', 'AdminPrize/index');
        } else {
            $this->error('添加失败', 'AdminPrize/index');
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $data = Db::name('prize')->where('id', $id)->find();
        $data['content']=cmf_replace_content_file_url(htmlspecialchars_decode( $data['content']));
        $data['more']=json_decode($data['more'],true);
        $ca=Db::name('prize_category')->select();
        $this->assign('category',$ca);
        $this->assign('list', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        $param = $this->request->param();
//        print_r($param);
        if (!empty($param['photo_names']) && !empty($param['photo_urls'])) {
            $param['more']['photos'] = [];
            foreach ($param['photo_urls'] as $key => $url) {
                $photoUrl = cmf_asset_relative_url($url);
                array_push($param['more']['photos'], ["url" => $photoUrl, "name" => $param['photo_names'][$key]]);
            }
            unset($param['photo_urls']);
            unset($param['photo_names']);
            $param['more']=json_encode($param['more']);
        }
        Db::name('prize')->where('id', $param['id'])->update($param);
        $this->success('编辑成功');
    }

    public function delete()
    {
        $id = $this->request->param('id');
        Db::name('prize')->where('id', $id)->delete();
        $this->success('删除成功');
    }

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

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