0
0

News.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\h5\controller;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. class News extends Controller
  8. {
  9. private $userid;
  10. private $token;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->token = input('token');
  15. if(empty($this->token)){
  16. $this->lo(url('/h5/index/errorPage'));
  17. }
  18. $this->userid = $this->checktoken();
  19. }
  20. public function newDetails(){
  21. $newsid = input('newsid');
  22. $page = input('page/d',1);
  23. $size = input('size/d',10);
  24. $offset = ($page-1)*$size;
  25. $ret=Db::name('news')
  26. ->where([
  27. 'enable'=>1,
  28. 'del'=>0,
  29. 'id'=>$newsid
  30. ])->find();
  31. //评论数
  32. $comment_num=Db::name('news_reply')
  33. ->field('id,news_id,content,user_id,create_time,news_reply_id')
  34. ->where([
  35. 'news_id'=>$newsid,
  36. 'news_reply_id'=>0,
  37. 'del'=>0,
  38. 'enable'=>1,
  39. ])->count();
  40. $ret['comment_num']=$comment_num?$comment_num:0;
  41. $ret['imgs'] = empty($ret['imgs'])?'':explode(',',$ret['imgs']);
  42. $ret['like']=0;
  43. if($this->userid){
  44. //点赞
  45. $like=Db::name('news_like')
  46. ->where([
  47. 'news_id'=>$newsid,
  48. 'user_id'=>$this->userid,
  49. ])->find();
  50. $ret['like']=$like?1:0; //1点赞0未点赞
  51. }
  52. if($page==1){
  53. //评论-回复
  54. $comment=Db::name('news_reply')
  55. ->where([
  56. 'news_id'=>$ret['id'],
  57. 'enable'=>1,
  58. 'del'=>0,
  59. 'news_reply_id'=>0
  60. ])
  61. ->order('id','asc')
  62. ->limit($offset,$size)
  63. ->select();
  64. foreach ($comment as $k=>$v){
  65. $user=Db::name('user')->
  66. field('real_name,head_image')
  67. ->where('id',$v['user_id'])
  68. ->find();
  69. $comment[$k]['user_name']=$user?$user['real_name']:'';
  70. $comment[$k]['head_image']=$user?$user['head_image']:'';
  71. $comment[$k]['reply_name']='';
  72. $subcomment = Db::name('news_reply')
  73. ->where([
  74. 'news_id'=>$ret['id'],
  75. 'enable'=>1,
  76. 'del'=>0,
  77. 'news_reply_id'=>$v['id']
  78. ])
  79. ->order('id','asc')
  80. ->select();
  81. $subcomment = $subcomment?$subcomment:[];
  82. foreach ($subcomment as $key=>$value){
  83. $user=Db::name('user')->
  84. field('real_name,head_image')
  85. ->where('id',$value['user_id'])
  86. ->find();
  87. $reply = Db::name('user')
  88. ->field('real_name,head_image')
  89. ->where('id',$value['reply_user_id'])
  90. ->find();
  91. $subcomment[$key]['user_name']=$user?$user['real_name']:'';
  92. $subcomment[$key]['reply_name']=$reply?$reply['real_name']:'';
  93. }
  94. $comment[$k]['sub'] = $subcomment;
  95. }
  96. }
  97. //上拉加载更多。。。
  98. if($page>1){
  99. $offset = ($page-1)*$size;
  100. //评论-回复
  101. $comment=Db::name('news_reply')
  102. ->where([
  103. 'news_id'=>$ret['id'],
  104. 'enable'=>1,
  105. 'del'=>0,
  106. 'news_reply_id'=>0
  107. ])
  108. ->order('id','desc')
  109. ->limit($offset,$size)
  110. ->select();
  111. foreach ($comment as $k=>$v){
  112. $user=Db::name('user')
  113. ->field('real_name,head_image')
  114. ->where('id',$v['user_id'])
  115. ->find();
  116. $comment[$k]['user_name']=$user?$user['real_name']:'';
  117. $comment[$k]['head_image']=$user?$user['real_name']:'';
  118. $comment[$k]['reply_name']='';
  119. $subcomment = Db::name('news_reply')
  120. ->where([
  121. 'news_id'=>$ret['id'],
  122. 'enable'=>1,
  123. 'del'=>0,
  124. 'news_reply_id'=>$v['id']
  125. ])
  126. ->order('id','asc')
  127. ->select();
  128. $subcomment = $subcomment?$subcomment:[];
  129. foreach ($subcomment as $key=>$value){
  130. $user=Db::name('user')->
  131. field('real_name,head_image')
  132. ->where('id',$value['user_id'])
  133. ->find();
  134. $reply = Db::name('user')
  135. ->field('real_name,head_image')
  136. ->where('id',$value['reply_user_id'])
  137. ->find();
  138. $subcomment[$key]['user_name']=$user?$user['real_name']:'';
  139. $subcomment[$key]['reply_name']=$reply?$reply['real_name']:'';
  140. }
  141. $comment[$k]['sub'] = $subcomment;
  142. }
  143. HelpHander::success($comment);
  144. }
  145. $data=[
  146. 'views'=>$ret['views']+1,
  147. ];
  148. Db::name('news')
  149. ->where('id',$newsid)->update($data);
  150. $this->assign('data',$ret);
  151. $this->assign('comment',$comment);
  152. $this->assign('userid',$this->userid);
  153. $this->assign('token',$this->token);
  154. return $this->fetch('news/details');
  155. }
  156. //回复文章
  157. public function replytc(){
  158. $this->userid = $this->checktoken();
  159. $newsid=input('newsid',0);
  160. $replyId=input('replyId',0);
  161. $replyUserId=input('replyUserId',0);
  162. $data=[
  163. 'news_id'=>$newsid,
  164. 'content'=>trim(input('content')),
  165. 'user_id'=>$this->userid,
  166. 'enable'=>1,
  167. 'create_time'=>getTime(),
  168. 'news_reply_id'=>$replyId,
  169. 'reply_user_id' => $replyUserId
  170. ];
  171. if(!$data['content']){
  172. HelpHander::error('回复消息不能为空');
  173. }
  174. $ret= Db::name('news_reply')
  175. ->insert($data);
  176. $ret? HelpHander::success([],'回复成功'): HelpHander::error('回复失败');
  177. }
  178. //删除评论
  179. public function delReplay(){
  180. $id=input('id');
  181. $data=['del'=>1];
  182. $ret=Db::name('news_reply')
  183. ->where('id',$id)
  184. ->where('user_id',$this->userid)->update($data);
  185. if($ret){
  186. HelpHander::success([],'删除成功');
  187. }else{
  188. HelpHander::error('删除失败');
  189. }
  190. }
  191. public function like(){
  192. $newsId=input('newsid');
  193. $type=input('type');//1已点赞0取消点赞
  194. if($type==0){
  195. $data=[
  196. 'news_id'=>$newsId,
  197. 'user_id'=>$this->userid
  198. ];
  199. $ret=Db::name('news_like')
  200. ->insert($data);
  201. if($ret){
  202. HelpHander::success([],'点赞成功');
  203. }else{
  204. HelpHander::error('点赞失败');
  205. }
  206. }
  207. if($type==1){
  208. $ret=Db::name('news_like')
  209. ->where([
  210. 'news_id'=>$newsId,
  211. 'user_id'=>$this->userid
  212. ])->delete();
  213. if($ret){
  214. HelpHander::success([],'取消点赞成功');
  215. }else{
  216. HelpHander::error('取消点赞失败');
  217. }
  218. }
  219. }
  220. public function checkToken(){
  221. $token = input('token');
  222. if(!$token){
  223. $this->lo(url('/h5/index/errorPage'));
  224. }
  225. $tokenInfo = Db::name('token')
  226. ->where('token',$token)
  227. ->find();
  228. if(empty($tokenInfo)){
  229. $this->lo(url('/h5/index/errorPage'));
  230. }
  231. return $tokenInfo['user_id'];
  232. }
  233. public function lo($url){
  234. header("Location: $url");
  235. exit();
  236. }
  237. }