EnjoyController.php 3.1 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 EnjoyController extends HomeBaseController
{
    private $index_limit = 16;//首页分页
    private $more_limit = 8;//更多列表分页
    public function index(){
        //按照城市查询
        $city_id = $this->request->param('city_id');
        $position['city_id'] = $city_id;

        //亲临现场
        $position['category_id'] = CityCategoryModel::qlxc;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res_qlxc = $this->getChildArticle($position,$field);
        $this->assign('res_qlxc',$res_qlxc);

        //市井漫游
        $position['category_id'] = CityCategoryModel::yyzx;
        $field = 'id,thumbnail,post_title,more';
        $res_yyzx = $this->getChildArticle($position,$field);
        foreach($res_yyzx as &$value){
            $more = json_decode($value['more'],true);
            if(isset($more['photos']) && !empty($more['photos'])){
                $value['photos'] = $more['photos'][0]['url'];
            }else{
                $value['photos'] = '';
            }
        }
        $this->assign('res_yyzx',$res_yyzx);

        //视听盛宴
        $position['category_id'] = CityCategoryModel::stsy;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res_stsy = $this->getChildArticle($position,$field);
        $this->assign('res_stsy',$res_stsy);

        //户外天堂
        $position['category_id'] = CityCategoryModel::hwtt;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res_hwtt = $this->getChildArticle($position,$field);
        $this->assign('res_hwtt',$res_hwtt);

        return $this->fetch();
    }

    //根据子类获取文章
    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['category_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('city_id',$position['city_id'])
            ->where('delete_time', 0)
            ->field($field)
            ->limit($limit)
            ->order('weigh desc')
            ->select()
            ->toArray();
        return $res;
    }
}