<?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 app\portal\model\PortalPostModel; use app\portal\model\CollectionModel; use app\portal\model\LikeModel; 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'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $this->assign('city_id',$city_id); $position['city_id'] = $city_id; //城市名称对应banner图 $city_banner = $this->getCityBanner($city_id); $this->assign('city_banner',$city_banner); //星球故事 $position['category_id'] = CityCategoryModel::xqgs; $field = 'id,thumbnail,post_excerpt,more,excerpt'; $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']; } $contentModel = new PortalPostModel(); $value['excerpt'] = $contentModel->getPostContentAttr($value['excerpt']); } if($res_xqgs){ $res_xqgs = $res_xqgs[0]; } $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_lsmq = $this->getChildArticle($position,$field,$this->index_limit); $this->assign('res_lsmq',$res_lsmq); //活力生态 $position['category_id'] = CityCategoryModel::hlst; $field = 'id,thumbnail,post_title,post_excerpt'; $res_hlst = $this->getChildArticle($position,$field,3); $this->assign('res_hlst',$res_hlst); //便利出行 $position['category_id'] = CityCategoryModel::blcx; $field = 'id,thumbnail'; $res_blcx = $this->getChildArticle($position,$field,3); $this->assign('res_blcx',$res_blcx); return $this->fetch(); } //获取城市名称 public function getCityName($city_id){ $res = Db::name('city_category') ->where('id',$city_id) ->field('id,name') ->find(); $city_name = empty($res['name'])?'':$res['name']; return $city_name; } //获取城市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; } //根据子类获取文章 public function getChildArticle($position,$field,$limit=''){ $pre = CityCategoryModel::pre; $limit = empty($limit)?0:$limit; $post_id = Db::table($pre.'portal_category_post') ->where('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,$limit = ''){ $pre = CityCategoryModel::pre; $limit = empty($limit)?$this->more_limit:$limit; if($limit == -1){ $limit = 1500; } $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'); //查询文章 if($position['city_id'] == 0){ //所有 $where = ['delete_time'=>0]; }else{ $where = ['city_id'=>$position['city_id'],'delete_time'=>0]; } $res = Db::table($pre.'portal_post') ->whereIn('id',$post_id) ->where($where) ->field($field) ->order('weigh desc') ->paginate($limit,false,['query'=>request()->param()]); $arr['data'] = $res->toArray(); $arr['page'] = $res->render(); return $arr; } //网红美景更多 public function getMoreScenery(){ $city_id = $this->request->param('city_id',0,'intval'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $position['category_id'] = CityCategoryModel::whmj; $position['city_id'] = $city_id; $field = 'id,thumbnail,post_title,post_hits,post_favorites'; $res = $this->getChildArticlePage($position,$field); $position['is_show'] = 1; $is_show_img = $this->getShowImg($position,$field,5); $this->assign('image',$is_show_img); $this->assign('res',$res['data']); $this->assign('page',$res['page']); return $this->fetch(); } //获取网红美景展示图片 public function getShowImg($position,$field,$limit = ''){ $pre = CityCategoryModel::pre; $limit = empty($limit)?$this->more_limit:$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'); //查询文章 if($position['city_id'] == 0){ //所有 $where = ['delete_time'=>0,'is_show'=>$position['is_show']]; }else{ $where = ['city_id'=>$position['city_id'],'delete_time'=>0,'is_show'=>$position['is_show']]; } $res = Db::table($pre.'portal_post') ->whereIn('id',$post_id) ->where($where) ->field($field) ->order('weigh desc') ->limit($limit) ->select() ->toArray(); return $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['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:''; } $this->assign('res',$res); return $this->fetch(); } //异域珍馐更多 public function getMoreFood(){ $city_id = $this->request->param('city_id',0,'intval'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $position['category_id'] = CityCategoryModel::yyzx; $position['city_id'] = $city_id; $field = 'id,thumbnail,post_title,post_hits,post_favorites'; $res = $this->getChildArticlePage($position,$field,10); $this->assign('res',$res['data']); $this->assign('page',$res['page']); return $this->fetch(); } //异域珍馐详情 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); return $this->fetch(); } //良宿美寝更多 public function getMoreHotel(){ $city_id = $this->request->param('city_id',0,'intval'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $position['category_id'] = CityCategoryModel::lsmq; $position['city_id'] = $city_id; $field = 'id,thumbnail,post_title,post_excerpt'; $res = $this->getChildArticlePage($position,$field); $this->assign('res',$res['data']); $this->assign('page',$res['page']); return $this->fetch(); } //良宿美寝详情 public function getHotelDetail(){ $id = $this->request->param('id',0,'intval'); $field = 'id,thumbnail,post_title,tel,url,address,post_excerpt,create_time,post_content,more'; $res = $this->getDetail($id,$field); if($res){ $more = json_decode($res['more'],true); $res['image_one'] = isset($more['photos']) && !empty($more['photos'])?$more['photos'][0]['url']:''; $res['image_url'] = isset($more['photos']) && !empty($more['photos'])?$more['photos']:''; } $this->assign('res',$res); return $this->fetch(); } //活力生态更多 public function getMoreEcology(){ $city_id = $this->request->param('city_id',0,'intval'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $position['category_id'] = CityCategoryModel::hlst; $position['city_id'] = $city_id; $field = 'id,thumbnail,post_title,post_excerpt'; $res = $this->getChildArticlePage($position,$field); $this->assign('res',$res['data']); $this->assign('page',$res['page']); return $this->fetch(); } //活力生态详情 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); return $this->fetch(); } //便利出行更多 public function getMoreTravel(){ $city_id = $this->request->param('city_id',0,'intval'); //查询城市名称 $city_name = $this->getCityName($city_id); $this->assign('city_name',$city_name); $position['category_id'] = CityCategoryModel::blcx; $position['city_id'] = $city_id; $field = 'id,thumbnail,post_title,post_excerpt'; $res = $this->getChildArticlePage($position,$field); $this->assign('res',$res['data']); $this->assign('page',$res['page']); return $this->fetch(); } //便利出行详情 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); return $this->fetch(); } //获取文章详情(评论列表) public function getDetail($id,$field){ $pre = CityCategoryModel::pre; $res = Db::table($pre.'portal_post') ->where(['id'=>$id,'delete_time'=>0]) ->field($field) ->find(); $contentModel = new PortalPostModel(); $res['post_content'] = $contentModel->getPostContentAttr($res['post_content']); //查看数+1 $postModel = new PortalPostModel(); $postModel->where('id',$id)->setInc('post_hits', 1); return $res; } //星球故事详情 public function getStoryDetail(){ $id = $this->request->param('id',0,'intval'); $field = 'id,thumbnail,post_title,post_excerpt,create_time,post_content,more,excerpt'; $res = $this->getDetail($id,$field); if($res){ $more = json_decode($res['more'],true); $res['audio'] = isset($more['audio']) && !empty($more['audio'])?$more['audio']:''; $contentModel = new PortalPostModel(); $res['excerpt'] = $contentModel->getPostContentAttr($res['excerpt']); } $this->assign('res',$res); return $this->fetch(); } //用户评论 public function comment(){ //判断是否登录 $login = cmf_is_user_login(); $article_id = $this->request->param('article_id');//文章id $content = $this->request->param('content');//评论内容 $url = $this->request->param('url');//评论文章链接 if($login){ $data['user_id'] = cmf_get_current_user_id(); $data['post_id'] = $article_id; $data['content'] = $content; $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,'请登录后评论!'); } } //收藏 public function collection(){ //判断是否登录 $login = cmf_is_user_login(); $article_id = $this->request->param('article_id');//文章id if(empty($article_id)){ $this->apiResponse(0,'收藏失败!'); } $post_url = $this->request->param('post_url');//文章链接 if($login){ $data['uid'] = cmf_get_current_user_id(); $data['post_id'] = $article_id;//文章id $data['post_url'] = $post_url; //查找分类名称 $category = Db::name('portal_category_post') ->where('post_id',$article_id) ->field('category_id') ->find(); $category_name = Db::name('portal_category') ->where('id',$category['category_id']) ->field('name') ->find(); //查找城市名称 $city = Db::name('portal_post') ->where('id',$article_id) ->where('delete_time',0) ->field('city_id') ->find(); $city_name = Db::name('city_category') ->where('id',$city['city_id']) ->field('name') ->find(); $data['category_name'] = $category_name['name'];//分类名称 $data['city_name'] = isset($city_name['name']) && !empty($city_name['name'])?$city_name['name']:'';//城市名称 $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(empty($article_id)){ $this->apiResponse(0,'点赞失败!'); } 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,'请登录后操作!'); } } //更多视频显示是否收藏与点赞 //是否收藏 public function is_collections(){ //判断是否登录 $login = cmf_is_user_login(); $article_id = $this->request->param('id');//文章id if($login){ $uid = cmf_get_current_user_id(); $collectionModel = new CollectionModel(); $res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->find(); if($res){ $this->apiResponse(1); }else{ $this->apiResponse(0); } }else{ $this->apiResponse(0); } } //是否点赞 public function is_likes(){ //判断是否登录 $login = cmf_is_user_login(); $article_id = $this->request->param('id');//文章id if($login){ $uid = cmf_get_current_user_id(); $likeModel = new LikeModel(); $res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->find(); if($res){ $this->apiResponse(1); }else{ $this->apiResponse(0); } }else{ $this->apiResponse(0); } } }