123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace app\h5\controller;
- use app\hander\HelpHander;
- use think\Controller;
- use think\Db;
- use think\Exception;
- class News extends Controller
- {
- private $userid;
- private $token;
- public function __construct()
- {
- parent::__construct();
- $this->token = input('token');
- if(empty($this->token)){
- $this->lo(url('/h5/index/errorPage'));
- }
- $this->userid = $this->checktoken();
- }
- public function newDetails(){
- $newsid = input('newsid');
- $page = input('page/d',1);
- $size = input('size/d',10);
- $offset = ($page-1)*$size;
- $ret=Db::name('news')
- ->where([
- 'enable'=>1,
- 'del'=>0,
- 'id'=>$newsid
- ])->find();
- //评论数
- $comment_num=Db::name('news_reply')
- ->field('id,news_id,content,user_id,create_time,news_reply_id')
- ->where([
- 'news_id'=>$newsid,
- 'news_reply_id'=>0,
- 'del'=>0,
- 'enable'=>1,
- ])->count();
- $ret['comment_num']=$comment_num?$comment_num:0;
- $ret['imgs'] = empty($ret['imgs'])?'':explode(',',$ret['imgs']);
- $ret['like']=0;
- if($this->userid){
- //点赞
- $like=Db::name('news_like')
- ->where([
- 'news_id'=>$newsid,
- 'user_id'=>$this->userid,
- ])->find();
- $ret['like']=$like?1:0; //1点赞0未点赞
- }
- if($page==1){
- //评论-回复
- $comment=Db::name('news_reply')
- ->where([
- 'news_id'=>$ret['id'],
- 'enable'=>1,
- 'del'=>0,
- 'news_reply_id'=>0
- ])
- ->order('id','asc')
- ->limit($offset,$size)
- ->select();
- foreach ($comment as $k=>$v){
- $user=Db::name('user')->
- field('real_name,head_image')
- ->where('id',$v['user_id'])
- ->find();
- $comment[$k]['user_name']=$user?$user['real_name']:'';
- $comment[$k]['head_image']=$user?$user['head_image']:'';
- $comment[$k]['reply_name']='';
- $subcomment = Db::name('news_reply')
- ->where([
- 'news_id'=>$ret['id'],
- 'enable'=>1,
- 'del'=>0,
- 'news_reply_id'=>$v['id']
- ])
- ->order('id','asc')
- ->select();
- $subcomment = $subcomment?$subcomment:[];
- foreach ($subcomment as $key=>$value){
- $user=Db::name('user')->
- field('real_name,head_image')
- ->where('id',$value['user_id'])
- ->find();
- $reply = Db::name('user')
- ->field('real_name,head_image')
- ->where('id',$value['reply_user_id'])
- ->find();
- $subcomment[$key]['user_name']=$user?$user['real_name']:'';
- $subcomment[$key]['reply_name']=$reply?$reply['real_name']:'';
- }
- $comment[$k]['sub'] = $subcomment;
- }
- }
- //上拉加载更多。。。
- if($page>1){
- $offset = ($page-1)*$size;
- //评论-回复
- $comment=Db::name('news_reply')
- ->where([
- 'news_id'=>$ret['id'],
- 'enable'=>1,
- 'del'=>0,
- 'news_reply_id'=>0
- ])
- ->order('id','desc')
- ->limit($offset,$size)
- ->select();
- foreach ($comment as $k=>$v){
- $user=Db::name('user')
- ->field('real_name,head_image')
- ->where('id',$v['user_id'])
- ->find();
- $comment[$k]['user_name']=$user?$user['real_name']:'';
- $comment[$k]['head_image']=$user?$user['real_name']:'';
- $comment[$k]['reply_name']='';
- $subcomment = Db::name('news_reply')
- ->where([
- 'news_id'=>$ret['id'],
- 'enable'=>1,
- 'del'=>0,
- 'news_reply_id'=>$v['id']
- ])
- ->order('id','asc')
- ->select();
- $subcomment = $subcomment?$subcomment:[];
- foreach ($subcomment as $key=>$value){
- $user=Db::name('user')->
- field('real_name,head_image')
- ->where('id',$value['user_id'])
- ->find();
- $reply = Db::name('user')
- ->field('real_name,head_image')
- ->where('id',$value['reply_user_id'])
- ->find();
- $subcomment[$key]['user_name']=$user?$user['real_name']:'';
- $subcomment[$key]['reply_name']=$reply?$reply['real_name']:'';
- }
- $comment[$k]['sub'] = $subcomment;
- }
- HelpHander::success($comment);
- }
- $data=[
- 'views'=>$ret['views']+1,
- ];
- Db::name('news')
- ->where('id',$newsid)->update($data);
- $this->assign('data',$ret);
- $this->assign('comment',$comment);
- $this->assign('userid',$this->userid);
- $this->assign('token',$this->token);
- return $this->fetch('news/details');
- }
- //回复文章
- public function replytc(){
- $this->userid = $this->checktoken();
- $newsid=input('newsid',0);
- $replyId=input('replyId',0);
- $replyUserId=input('replyUserId',0);
- $data=[
- 'news_id'=>$newsid,
- 'content'=>trim(input('content')),
- 'user_id'=>$this->userid,
- 'enable'=>1,
- 'create_time'=>getTime(),
- 'news_reply_id'=>$replyId,
- 'reply_user_id' => $replyUserId
- ];
- if(!$data['content']){
- HelpHander::error('回复消息不能为空');
- }
- $ret= Db::name('news_reply')
- ->insert($data);
- $ret? HelpHander::success([],'回复成功'): HelpHander::error('回复失败');
- }
- //删除评论
- public function delReplay(){
- $id=input('id');
- $data=['del'=>1];
- $ret=Db::name('news_reply')
- ->where('id',$id)
- ->where('user_id',$this->userid)->update($data);
- if($ret){
- HelpHander::success([],'删除成功');
- }else{
- HelpHander::error('删除失败');
- }
- }
- public function like(){
- $newsId=input('newsid');
- $type=input('type');//1已点赞0取消点赞
- if($type==0){
- $data=[
- 'news_id'=>$newsId,
- 'user_id'=>$this->userid
- ];
- $ret=Db::name('news_like')
- ->insert($data);
- if($ret){
- HelpHander::success([],'点赞成功');
- }else{
- HelpHander::error('点赞失败');
- }
- }
- if($type==1){
- $ret=Db::name('news_like')
- ->where([
- 'news_id'=>$newsId,
- 'user_id'=>$this->userid
- ])->delete();
- if($ret){
- HelpHander::success([],'取消点赞成功');
- }else{
- HelpHander::error('取消点赞失败');
- }
- }
- }
- public function checkToken(){
- $token = input('token');
- if(!$token){
- $this->lo(url('/h5/index/errorPage'));
- }
- $tokenInfo = Db::name('token')
- ->where('token',$token)
- ->find();
- if(empty($tokenInfo)){
- $this->lo(url('/h5/index/errorPage'));
- }
- return $tokenInfo['user_id'];
- }
- public function lo($url){
- header("Location: $url");
- exit();
- }
- }
|