IndexController.php 5.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 think\Db;
class IndexController extends HomeBaseController
{
    public function index(){
        $pre = CityCategoryModel::pre;

        $serverModel = new ServerController();
        //统计访问量
        $serverModel->statistics();
        //星球奇境,城市分类
        $res_child = Db::table($pre.'city_category')
            ->where('pid','<>',0)
            ->where('delete_time', 0)
            ->field('id,pid,name')
            ->select()
            ->toArray();
        $res_parent = Db::table($pre.'city_category')
            ->where('pid',0)
            ->where('delete_time', 0)
            ->field('id,pid,name')
            ->select()
            ->toArray();
        foreach($res_parent as &$r_value){
            foreach($res_child as $c_value){
                if($c_value['pid'] == $r_value['id']){
                    $r_value['children'][] = $c_value;
                }
            }
        }
        $this->assign('res_parent',$res_parent);

        //星享体验
        $position = CityCategoryModel::xxty;
        $field = 'id,month,post_title,thumbnail';
        $post = $this->getParentArticle($position,$field);
        $res_xxty = [];
        $arr = [];
        for($i=1;$i<13;$i++){
            $arr['month'] = $i;
            $arr['list'] = [];
            foreach($post as $value){
                if($value['month'] == $i){
                    $arr['list'][] = $value;
                }
            }
            array_push($res_xxty,$arr);
        }
        $this->assign('res_xxty',$res_xxty);

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


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


        //星探推荐
        $position = CityCategoryModel::xttj;
        $field = 'id,post_title,price,thumbnail';
        $res_xttj = $this->getParentArticle($position,$field,4);
        $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;
    }
}