IndexController.php 8.2 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 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 cmf\controller\HomeBaseController;
use app\portal\model\CityCategoryModel;
use app\portal\model\PortalPostModel;
use think\Db;
class IndexController extends HomeBaseController
{
    public function index(){

        $serverModel = new ServerController();
        //统计访问量
        $serverModel->statistics();

        //banner轮播图
        $coverImg = $this->getCoverImg(CityCategoryModel::indexImg,3);
        $this->assign('coverImg',$coverImg);

        //星球奇境,城市分类
        $res['asia'] = $this->getCity(CityCategoryModel::asia);
        $res['europe'] = $this->getCity(CityCategoryModel::europe);
        $res['africa'] = $this->getCity(CityCategoryModel::africa);
        $res['oceania'] = $this->getCity(CityCategoryModel::oceania);
        $res['north'] = $this->getCity(CityCategoryModel::north);
        $res['south'] = $this->getCity(CityCategoryModel::south);
        $res['antarctica'] = $this->getCity(CityCategoryModel::antarctica);
        $res['arctic'] = $this->getCity(CityCategoryModel::arctic);
        $this->assign('res_city',$res);

        //星享体验
        $res_month['January'] = $this->getMonthArticle(CityCategoryModel::January);
        $res_month['February'] = $this->getMonthArticle(CityCategoryModel::February);
        $res_month['March'] = $this->getMonthArticle(CityCategoryModel::March);
        $res_month['April'] = $this->getMonthArticle(CityCategoryModel::April);
        $res_month['May'] = $this->getMonthArticle(CityCategoryModel::May);
        $res_month['June'] = $this->getMonthArticle(CityCategoryModel::June);
        $res_month['July'] = $this->getMonthArticle(CityCategoryModel::July);
        $res_month['August'] = $this->getMonthArticle(CityCategoryModel::August);
        $res_month['September'] = $this->getMonthArticle(CityCategoryModel::September);
        $res_month['October'] = $this->getMonthArticle(CityCategoryModel::October);
        $res_month['November'] = $this->getMonthArticle(CityCategoryModel::November);
        $res_month['December'] = $this->getMonthArticle(CityCategoryModel::December);
        $this->assign('res_month',$res_month);

        //星域秀场->星球影院
        $position = CityCategoryModel::xqyy;
        $field = 'id,more,thumbnail';
        $res_xqyy = $this->getChildArticle($position,$field,1);
        foreach($res_xqyy as &$value){
            $video = json_decode($value['more'],true);
            $value['video'] = $video['video'];
        }
        if($res_xqyy){
            $res_xqyy = $res_xqyy[0];
        }
        $this->assign('res_xqyy',$res_xqyy);

        //星域秀场->明星访谈
        $position = CityCategoryModel::mxft;
        $field = 'id,full_name,position,trade,post_excerpt,thumbnail avatar';
        $res_mxft = $this->getChildArticle($position,$field,1);
        if($res_mxft){
            $res_mxft = $res_mxft[0];
        }
        $this->assign('res_mxft',$res_mxft);


        //星域秀场->星域画廊
        $position = CityCategoryModel::xyhl;
        $field = 'id,thumbnail';
        $res_xyhl = $this->getChildArticle($position,$field,16);
        $this->assign('res_xyhl',$res_xyhl);


        //星探推荐
        $position = CityCategoryModel::xttj;
        $field = 'id,post_title,price,index_thumbnail,place';
        $res_xttj = $this->getParentArticle($position,$field,16);
        $this->assign('res_xttj',$res_xttj);


        //星际活动
        $position = CityCategoryModel::xjhd;
        $field = 'id,post_title,thumbnail';
        $res_xjhd = $this->getParentArticle($position,$field,2);
        $this->assign('res_xjhd',$res_xjhd);
        return $this->fetch();
    }

    //根据父类查询文章
    public function getParentArticle($position,$field,$limit = ''){
        $pre = CityCategoryModel::pre;
        $limit = empty($limit)?0:$limit;
        $category_id = Db::table($pre.'portal_category')
            ->where('parent_id',$position)
            ->field('id')
            ->select()
            ->toArray();
        $c_id = array_column($category_id,'id');
        //查询文章id
        $post_id = Db::table($pre.'portal_category_post')
            ->whereIn('category_id',$c_id)
            ->field('post_id')
            ->select()
            ->toArray();
        $post_id = array_column($post_id,'post_id');
        //查询文章
        $res = Db::table($pre.'portal_post')
            ->whereIn('id',$post_id)
            ->where('delete_time', 0)
            ->field($field)
            ->limit($limit)
            ->order('weigh desc')
            ->select()
            ->toArray();
        return $res;
    }

    //根据子类获取文章
    public function getChildArticle($position,$field,$limit=''){
        $pre = CityCategoryModel::pre;
        $limit = empty($limit)?0:$limit;
        $post_id = Db::table($pre.'portal_category_post')
            ->whereIn('category_id',$position)
            ->field('post_id')
            ->select()
            ->toArray();
        $post_id = array_column($post_id,'post_id');
        //查询文章
        $res = Db::table($pre.'portal_post')
            ->whereIn('id',$post_id)
            ->where('delete_time', 0)
            ->field($field)
            ->limit($limit)
            ->order('weigh desc')
            ->select()
            ->toArray();
        return $res;
    }

    //获取各洲对应的城市
    private function getCity($pid){
        $pre = CityCategoryModel::pre;
        $res = Db::table($pre.'city_category')
            ->where('pid',$pid)
            ->where('delete_time', 0)
            ->field('id,pid,name')
            ->select()
            ->toArray();
        return $res;
    }

    //获取封面图
    public function getCoverImg($type,$limit){
        $res = Db::name('image')
            ->where(['type'=>$type])
            ->field('id,image')
            ->limit($limit)
            ->order('weigh desc')
            ->select()
            ->toArray();
        return $res;
    }

    //根据月份查询文章(星享体验)
    private function getMonthArticle($month){
        $position = CityCategoryModel::xxty;
        $pre = CityCategoryModel::pre;
        $category_id = Db::table($pre.'portal_category')
            ->where('parent_id',$position)
            ->field('id')
            ->select()
            ->toArray();
        $c_id = array_column($category_id,'id');
        //查询文章id
        $post_id = Db::table($pre.'portal_category_post')
            ->whereIn('category_id',$c_id)
            ->field('post_id')
            ->select()
            ->toArray();
        $post_id = array_column($post_id,'post_id');
        //查询文章
        $res = Db::table($pre.'portal_post')
            ->whereIn('id',$post_id)
            ->where('month', $month)
            ->where('delete_time', 0)
            ->field('id,month,post_title,index_thumbnail,thumbnail,post_excerpt')
            ->limit(1)
            ->order('weigh desc')
            ->find();
        if($res){
            $category_id = Db::table($pre.'portal_category_post')
                ->where('post_id', $res['id'])
                ->field('category_id')
                ->find();
            $category_name = Db::table($pre.'portal_category')
                ->where('id', $category_id['category_id'])
                ->field('name')
                ->find();
            $res['category_name'] = $category_name['name'];
        }
        return $res;
    }

    //关于我们
    public function aboutUs(){
        $res = Db::name('about_us')->where('id',1)->find();
        $contentModel = new PortalPostModel();
        $res['content'] = $contentModel->getPostContentAttr($res['content']);
        $this->assign('res',$res);
        return $this->fetch();
    }
}