CooperationController.php 7.6 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: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\model\ThemeModel;
use app\portal\model\CooperationModel;
use app\portal\model\PortalPostModel;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalTagModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;

class CooperationController extends AdminBaseController
{
    /**
     * 列表
     * @adminMenu(
     *     'name'   => '文章管理',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章列表',
     *     'param'  => ''
     * )
     */
    public function index(Request $request)
    {
        $where_coo['status'] = 1;
        if($request->post()){
            $search = $request->post();
            if($search['start_time']){
                $start = strtotime($search['start_time']);
                if($search['end_time']){
                    $end = strtotime($search['end_time']);
                }else{
                    $end = time();
                }
                $where_coo['update_time'] = array('between',"$start,$end");
                $this->assign('start_time',date('Y-m-d H:i',$start));
                $this->assign('end_time',date('Y-m-d H:i',$end));
            }
            if($search['keyword']){
                $keyword = $search['keyword'];
                $where_coo['title'] = array('like',"%$keyword%");
                $this->assign('keyword',$keyword);
            }
        }
        $coo_list = Db::name('Cooperation')->where($where_coo)->order('score desc , create_time desc')
            ->paginate(10);
        $page = $coo_list->render();
//            ->select()->toArray();
        $this->assign('list',$coo_list);
        $this->assign('page',$page);
        return $this->fetch();
    }


    /**
     * 添加、修改
     * @adminMenu(
     *     'name'   => '添加文章',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加文章',
     *     'param'  => ''
     * )
     */
    public function add(Request $request)
    {
        if($request->post()){
//            添加数据
            $data = $request->post();
            if($request->Post('id')){
//                修改
                $change_add['title'] = $data['title'];
                $change_add['pic'] = $data['pic'];
                $change_add['score'] = $data['score'];
                $change_add['jump'] = $data['jump'];
                $validate = Loader::validate('Cooperation');
                if(!$validate->scene('edit')->check($change_add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $change_add['id'] = $request->Post('id');
                $model = new CooperationModel();
                $final = $model->isUpdate(true)->allowField(true)->save($change_add);
            }else{
//                添加
                $add['title'] = $data['title'];
                $add['pic'] = $data['pic'];
                $add['score'] = $data['score'];
                $add['jump'] = $data['jump'];
                $validate = Loader::validate('Cooperation');
                if(!$validate->scene('add')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $model = new CooperationModel();
                $final = $model->save($add);
            }
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $where_find['id'] = $request->param('id');
            $list = Db::name('Cooperation')->where($where_find)->find();
            $this->assign('list',$list);
            return $this->fetch('add');
        }
    }

//  删除
    public function del(Request $request){
        $ids = $this->request->post();
        $id = $request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new CooperationModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }


//    两分钟视频
    public function video(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    $first = str_split($fileUrl,4);
                    if($first[0] == 'http'){
                        $add['path'] = $fileUrl;
                    }else{
                        $add['path'] = cmf_get_asset_url($fileUrl);
                    }
                    $add['update_time'] = time();
                    $where_change['id'] = 1;
                    $res = Db::name('Video')->where($where_change)->update($add);
                    if($res){
                        $this->success('修改成功',url('Cooperation/video'));
                    }else{
                        $this->error('添加失败');
                    }
                }
            }else{
                $this->error('视频不能为空');
            }
        }else{
            $where_status['status'] = 1;
            $video = Db::name('Video')->where($where_status)->find();
            $this->assign('video',$video);
            return $this->fetch();
        }

    }


//    网站配置
    public function conf(){
        $data = $this->request->param();
        if($this->request->post()){

        }
        $this->assign('list',array());
        return $this->fetch();
    }

    //    推广内容
    public function push(){
        $data = $this->request->param();
        if($this->request->post()){
            $add['title'] = $data['title'];
            $add['content'] = $data['content'];
            $add['update_time'] = time();
            if(empty($data['id'])){
                $add['create_time'] = time();
                $res = Db::name('Push')->insertGetId($add);
            }else{
                $where_id['id'] = $data['id'];
                $res = Db::name('Push')->where($where_id)->update($add);
            }
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }


        }else{
            $where_push['status'] = 1;
            $list = Db::name('Push')->where($where_push)->find();
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

}