StarController.php 10.8 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 app\portal\model\CommentModel;
use cmf\controller\HomeBaseController;
use app\portal\model\CityCategoryModel;
use app\portal\validate\CommentValidate;
use think\Db;
//星球奇境
class StarController 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::xqgs;
        $field = 'id,thumbnail,post_excerpt,more';
        $res_xqgs = $this->getChildArticle($position,$field,1);
        foreach($res_xqgs as &$value){
            $more = json_decode($value['more'],true);
            if(isset($more['audio']) && !empty($more['audio'])){
                $value['audio'] = $more['audio'];
            }
        }
        $this->assign('res_xqgs',$res_xqgs);

        //网红美景
        $position['category_id'] = CityCategoryModel::whmj;
        $field = 'id,thumbnail,post_title,post_hits,post_favorites';
        $res_whmj = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_whmj',$res_whmj);

        //异域珍馐
        $position['category_id'] = CityCategoryModel::yyzx;
        $field = 'id,thumbnail,post_title';
        $res_yyzx = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_yyzx',$res_yyzx);

        //良宿美寝
        $position['category_id'] = CityCategoryModel::lsmq;
        $field = 'id,thumbnail,post_title';
        $res_lsmj = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_lsmj',$res_lsmj);

        //活力生态
        $position['category_id'] = CityCategoryModel::hlst;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res_hlst = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_hlst',$res_hlst);

        //便利出行
        $position['category_id'] = CityCategoryModel::blcx;
        $field = 'id,thumbnail';
        $res_blcx = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_blcx',$res_blcx);
        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;
    }

    //根据子类获取文章分页
    public function getChildArticlePage($position,$field,$page){
        $pre = CityCategoryModel::pre;
        $limit = $this->more_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)
            ->page($page,$limit)
            ->order('weigh desc')
            ->select()
            ->toArray();
        return $res;
    }

    //网红美景更多
    public function getMoreScenery(){
        $page = $this->request->param('page',1,'intval');
        $position['category_id'] = CityCategoryModel::whmj;
        $field = 'id,thumbnail,post_title,post_hits,post_favorites';
        $res = $this->getChildArticlePage($position,$field,$page);
        $this->assign('res',$res);
    }

    //网红美景详情
    public function getSceneryDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['audio'] = isset($more['audio']) && !empty($more['audio'])?$more['audio']:'';
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
    }

    //异域珍馐更多
    public function getMoreFood(){
        $page = $this->request->param('page',1,'intval');
        $position['category_id'] = CityCategoryModel::yyzx;
        $field = 'id,thumbnail,post_title,post_hits,post_favorites';
        $res_top = $this->getChildArticle($position,$field,2);
        $res = $this->getChildArticlePage($position,$field,$page);
        $this->assign('res_top',$res_top);
        $this->assign('res',$res);
    }

    //异域珍馐详情
    public function getFoodDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
    }

    //良宿美寝更多
    public function getMoreHotel(){
        $page = $this->request->param('page',1,'intval');
        $position['category_id'] = CityCategoryModel::lsmq;
        $field = 'id,thumbnail,post_title';
        $res = $this->getChildArticlePage($position,$field,$page);
        $this->assign('res',$res);
    }

    //良宿美寝详情
    public function getHotelDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
    }

    //活力生态更多
    public function getMoreEcology(){
        $page = $this->request->param('page',1,'intval');
        $position['category_id'] = CityCategoryModel::hlst;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res = $this->getChildArticlePage($position,$field,$page);
        $this->assign('res',$res);
    }

    //活力生态详情
    public function getEcologyDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
    }

    //便利出行更多
    public function getMoreTravel(){
        $page = $this->request->param('page',1,'intval');
        $position['category_id'] = CityCategoryModel::blcx;
        $field = 'id,thumbnail,post_title,post_excerpt';
        $res = $this->getChildArticlePage($position,$field,$page);
        $this->assign('res',$res);
    }

    //便利出行详情
    public function getTravelDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
    }

    //获取文章详情(评论列表)
    public function getDetail($id,$field){
        $pre = CityCategoryModel::pre;
        $res = Db::table($pre.'portal_post')
            ->where(['id'=>$id,'delete_time'=>0])
            ->field($field)
            ->find();
        $comment = Db::name('comment')
            ->alias('c')
            ->join('user u','c.user_id = u.id','LEFT')
            ->where(['c.status'=>1,'post_id'=>$id])
            ->field('u.user_nickname nickname,u.avatar,c.content,c.create_time')
            ->select();
        $res['comment'] = $comment;
        return $res;
    }

    //星球故事详情
    public function getStoryDetail(){
        $id = $this->request->param('id',0,'intval');
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more';
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['audio'] = isset($more['audio']) && !empty($more['audio'])?$more['audio']:'';
        }
        $this->assign('res',$res);
    }

    //用户评论
    public function comment(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
        $comment = $this->request->param('comment');//评论内容
        $url = $this->request->param('url');//评论文章链接
        if($login){
            $data['user_id'] = cmf_get_current_user_id();
            $data['post_id'] = $article_id;
            $data['content'] = $comment;
            $data['url'] = $url;
            $data['create_time'] = time();
            $validate = new CommentValidate();
            if(!$validate->check($data)){
                $this->apiResponse(0,$validate->getError());
            }
            $commentModel = new CommentModel();
            $res = $commentModel->allowField(true)->save($data);
            if($res){
                $this->apiResponse(1,'评论成功!');
            }else{
                $this->apiResponse(0,'评论失败!');
            }
        }else{
            $this->apiResponse(0,'请登录后评论!');
        }

    }
}