审查视图

app/portal/controller/StarController.php 16.6 KB
景龙 authored
1 2 3 4 5 6 7 8 9 10 11 12
<?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;
13
use app\portal\model\CommentModel;
景龙 authored
14 15
use cmf\controller\HomeBaseController;
use app\portal\model\CityCategoryModel;
16
use app\portal\validate\CommentValidate;
景龙 authored
17
use app\portal\model\PortalPostModel;
景龙 authored
18 19
use app\portal\model\CollectionModel;
use app\portal\model\LikeModel;
景龙 authored
20 21 22 23 24 25 26 27 28
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');
景龙 authored
29 30 31 32
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
        $this->assign('city_id',$city_id);
景龙 authored
33
        $position['city_id'] = $city_id;
34 35 36 37 38

        //城市名称对应banner图
        $city_banner = $this->getCityBanner($city_id);
        $this->assign('city_banner',$city_banner);
景龙 authored
39 40
        //星球故事
        $position['category_id'] = CityCategoryModel::xqgs;
41
        $field = 'id,thumbnail,post_excerpt,more,excerpt';
景龙 authored
42 43
        $res_xqgs = $this->getChildArticle($position,$field,1);
        foreach($res_xqgs as &$value){
44 45 46 47
            $more = json_decode($value['more'],true);
            if(isset($more['audio']) && !empty($more['audio'])){
                $value['audio'] = $more['audio'];
            }
48 49
            $contentModel = new PortalPostModel();
            $value['excerpt'] = $contentModel->getPostContentAttr($value['excerpt']);
景龙 authored
50
        }
景龙 authored
51 52 53
        if($res_xqgs){
            $res_xqgs = $res_xqgs[0];
        }
景龙 authored
54 55
        $this->assign('res_xqgs',$res_xqgs);
景龙 authored
56
景龙 authored
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        //网红美景
        $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';
景龙 authored
72 73
        $res_lsmq = $this->getChildArticle($position,$field,$this->index_limit);
        $this->assign('res_lsmq',$res_lsmq);
景龙 authored
74 75 76 77

        //活力生态
        $position['category_id'] = CityCategoryModel::hlst;
        $field = 'id,thumbnail,post_title,post_excerpt';
景龙 authored
78
        $res_hlst = $this->getChildArticle($position,$field,3);
景龙 authored
79 80 81 82 83
        $this->assign('res_hlst',$res_hlst);

        //便利出行
        $position['category_id'] = CityCategoryModel::blcx;
        $field = 'id,thumbnail';
景龙 authored
84
        $res_blcx = $this->getChildArticle($position,$field,3);
景龙 authored
85 86 87 88
        $this->assign('res_blcx',$res_blcx);
        return $this->fetch();
    }
景龙 authored
89 90
    //获取城市名称
    public function getCityName($city_id){
91
        $res = Db::name('city_category')
景龙 authored
92 93 94
            ->where('id',$city_id)
            ->field('id,name')
            ->find();
95
        $city_name = empty($res['name'])?'':$res['name'];
景龙 authored
96 97 98
        return $city_name;
    }
99 100 101 102 103 104 105 106 107 108
    //获取城市banner图
    public function getCityBanner($city_id){
        $res = Db::name('city_category')
            ->where('id',$city_id)
            ->field('id,image')
            ->find();
        $banner = empty($res['image'])?'':$res['image'];
        return $banner;
    }
景龙 authored
109 110 111 112 113
    //根据子类获取文章
    public function getChildArticle($position,$field,$limit=''){
        $pre = CityCategoryModel::pre;
        $limit = empty($limit)?0:$limit;
        $post_id = Db::table($pre.'portal_category_post')
景龙 authored
114
            ->where('category_id',$position['category_id'])
景龙 authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            ->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;
    }

    //根据子类获取文章分页
景龙 authored
133
    public function getChildArticlePage($position,$field,$limit = ''){
景龙 authored
134
        $pre = CityCategoryModel::pre;
景龙 authored
135
        $limit = empty($limit)?$this->more_limit:$limit;
景龙 authored
136 137 138 139 140 141 142
        $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');
        //查询文章
景龙 authored
143 144 145 146 147 148
        if($position['city_id'] == 0){
            //所有
            $where = ['delete_time'=>0];
        }else{
            $where = ['city_id'=>$position['city_id'],'delete_time'=>0];
        }
景龙 authored
149 150
        $res = Db::table($pre.'portal_post')
            ->whereIn('id',$post_id)
景龙 authored
151
            ->where($where)
景龙 authored
152 153
            ->field($field)
            ->order('weigh desc')
景龙 authored
154
            ->paginate($limit,false,['query'=>request()->param()]);
景龙 authored
155 156 157
        $arr['data'] = $res->toArray();
        $arr['page'] = $res->render();
        return $arr;
景龙 authored
158 159 160 161
    }

    //网红美景更多
    public function getMoreScenery(){
jinglong authored
162 163 164 165 166
        $city_id = $this->request->param('city_id',0,'intval');
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
景龙 authored
167
        $position['category_id'] = CityCategoryModel::whmj;
jinglong authored
168
        $position['city_id'] = $city_id;
景龙 authored
169
        $field = 'id,thumbnail,post_title,post_hits,post_favorites';
jinglong authored
170 171 172 173
        $res = $this->getChildArticlePage($position,$field);
        $this->assign('res',$res['data']);
        $this->assign('page',$res['page']);
        return $this->fetch();
景龙 authored
174 175
    }
176 177 178 179 180 181 182 183 184 185
    //网红美景详情
    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['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
        }
        $this->assign('res',$res);
jinglong authored
186
        return $this->fetch();
187 188
    }
景龙 authored
189 190
    //异域珍馐更多
    public function getMoreFood(){
jinglong authored
191 192 193 194 195
        $city_id = $this->request->param('city_id',0,'intval');
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
景龙 authored
196
        $position['category_id'] = CityCategoryModel::yyzx;
jinglong authored
197
        $position['city_id'] = $city_id;
景龙 authored
198
        $field = 'id,thumbnail,post_title,post_hits,post_favorites';
jinglong authored
199 200 201 202
        $res = $this->getChildArticlePage($position,$field,10);
        $this->assign('res',$res['data']);
        $this->assign('page',$res['page']);
        return $this->fetch();
景龙 authored
203 204
    }
205 206 207 208 209 210 211 212 213 214
    //异域珍馐详情
    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);
jinglong authored
215
        return $this->fetch();
216 217
    }
景龙 authored
218 219
    //良宿美寝更多
    public function getMoreHotel(){
景龙 authored
220 221 222 223 224
        $city_id = $this->request->param('city_id',0,'intval');
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
景龙 authored
225
        $position['category_id'] = CityCategoryModel::lsmq;
景龙 authored
226
        $position['city_id'] = $city_id;
jinglong authored
227
        $field = 'id,thumbnail,post_title,post_excerpt';
景龙 authored
228
        $res = $this->getChildArticlePage($position,$field);
jinglong authored
229 230
        $this->assign('res',$res['data']);
        $this->assign('page',$res['page']);
景龙 authored
231
        return $this->fetch();
景龙 authored
232 233
    }
234 235 236
    //良宿美寝详情
    public function getHotelDetail(){
        $id = $this->request->param('id',0,'intval');
jinglong authored
237
        $field = 'id,thumbnail,post_title,tel,url,address,post_excerpt,create_time,post_content,more';
238 239 240
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
jinglong authored
241 242
            $res['image_one'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:'';
            $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos']:'';
243 244
        }
        $this->assign('res',$res);
jinglong authored
245
        return $this->fetch();
246 247
    }
景龙 authored
248 249
    //活力生态更多
    public function getMoreEcology(){
jinglong authored
250 251 252 253 254
        $city_id = $this->request->param('city_id',0,'intval');
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
景龙 authored
255
        $position['category_id'] = CityCategoryModel::hlst;
jinglong authored
256
        $position['city_id'] = $city_id;
景龙 authored
257
        $field = 'id,thumbnail,post_title,post_excerpt';
jinglong authored
258 259 260 261
        $res = $this->getChildArticlePage($position,$field);
        $this->assign('res',$res['data']);
        $this->assign('page',$res['page']);
        return $this->fetch();
景龙 authored
262 263
    }
264 265 266 267 268 269 270 271 272 273
    //活力生态详情
    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);
jinglong authored
274
        return $this->fetch();
275 276
    }
景龙 authored
277 278
    //便利出行更多
    public function getMoreTravel(){
景龙 authored
279 280 281 282 283
        $city_id = $this->request->param('city_id',0,'intval');
        //查询城市名称
        $city_name = $this->getCityName($city_id);
        $this->assign('city_name',$city_name);
景龙 authored
284
        $position['category_id'] = CityCategoryModel::blcx;
景龙 authored
285
        $position['city_id'] = $city_id;
景龙 authored
286
        $field = 'id,thumbnail,post_title,post_excerpt';
景龙 authored
287 288 289 290
        $res = $this->getChildArticlePage($position,$field);
        $this->assign('res',$res['data']);
        $this->assign('page',$res['page']);
        return $this->fetch();
景龙 authored
291
    }
292 293 294 295 296 297 298 299 300 301 302

    //便利出行详情
    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);
景龙 authored
303
        return $this->fetch();
304 305
    }
306
    //获取文章详情(评论列表)
307 308 309 310 311 312
    public function getDetail($id,$field){
        $pre = CityCategoryModel::pre;
        $res = Db::table($pre.'portal_post')
            ->where(['id'=>$id,'delete_time'=>0])
            ->field($field)
            ->find();
景龙 authored
313 314
        $contentModel = new PortalPostModel();
        $res['post_content'] = $contentModel->getPostContentAttr($res['post_content']);
景龙 authored
315 316 317
        //查看数+1
        $postModel = new PortalPostModel();
        $postModel->where('id',$id)->setInc('post_hits', 1);
318 319 320 321 322 323
        return $res;
    }

    //星球故事详情
    public function getStoryDetail(){
        $id = $this->request->param('id',0,'intval');
324
        $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more,excerpt';
325 326 327 328
        $res = $this->getDetail($id,$field);
        if($res){
            $more = json_decode($res['more'],true);
            $res['audio'] = isset($more['audio']) && !empty($more['audio'])?$more['audio']:'';
329 330
            $contentModel = new PortalPostModel();
            $res['excerpt'] = $contentModel->getPostContentAttr($res['excerpt']);
331 332
        }
        $this->assign('res',$res);
景龙 authored
333
        return $this->fetch();
334
    }
335 336 337 338 339 340

    //用户评论
    public function comment(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
景龙 authored
341
        $content = $this->request->param('content');//评论内容
342 343 344 345
        $url = $this->request->param('url');//评论文章链接
        if($login){
            $data['user_id'] = cmf_get_current_user_id();
            $data['post_id'] = $article_id;
景龙 authored
346
            $data['content'] = $content;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
            $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,'请登录后评论!');
        }

    }
景龙 authored
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    //收藏
    public function collection(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
        if($login){
            $data['uid'] = cmf_get_current_user_id();
            $data['post_id'] = $article_id;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $collectionModel = new CollectionModel();
            $res = $collectionModel->allowField(true)->save($data);
            $contentModel = new PortalPostModel();
            $contentModel->where('id',$article_id)->setInc('post_favorites',1);
            if($res){
                $this->apiResponse(1,'收藏成功!');
            }else{
                $this->apiResponse(0,'收藏失败!');
            }
        }else{
            $this->apiResponse(0,'请登录后操作!');
        }
    }

    //取消收藏
    public function cancelCollection(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
        if($login){
            $uid = cmf_get_current_user_id();
            $collectionModel = new CollectionModel();
            $res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
            $contentModel = new PortalPostModel();
            $contentModel->where('id',$article_id)->setDec('post_favorites',1);
            if($res){
                $this->apiResponse(1,'已取消!');
            }else{
                $this->apiResponse(0,'取消失败!');
            }
        }else{
            $this->apiResponse(0,'请登录后操作!');
        }
    }

    //点赞
    public function like(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
        if($login){
            $data['uid'] = cmf_get_current_user_id();
            $data['post_id'] = $article_id;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $likeModel = new LikeModel();
            $res = $likeModel->allowField(true)->save($data);
            if($res){
                $this->apiResponse(1,'点赞成功!');
            }else{
                $this->apiResponse(0,'点赞失败!');
            }
        }else{
            $this->apiResponse(0,'请登录后操作!');
        }
    }

    //取消点赞
    public function cancelLike(){
        //判断是否登录
        $login = cmf_is_user_login();
        $article_id = $this->request->param('article_id');//文章id
        if($login){
            $uid = cmf_get_current_user_id();
            $likeModel = new LikeModel();
            $res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
            if($res){
                $this->apiResponse(1,'已取消!');
            }else{
                $this->apiResponse(0,'取消失败!');
            }
        }else{
            $this->apiResponse(0,'请登录后操作!');
        }
    }
景龙 authored
451
}