Xw.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\model\DataXw;
  4. use app\data\model\DataXwAd;
  5. use app\data\model\DataXwCategory;
  6. use app\data\model\DataXwComment;
  7. use think\admin\Controller;
  8. use hg\apidoc\annotation\Title;
  9. use hg\apidoc\annotation\Returned;
  10. use hg\apidoc\annotation\Param;
  11. use hg\apidoc\annotation\Method;
  12. use think\db\Query;
  13. /**
  14. * @Title("新闻报道")
  15. */
  16. class Xw extends Auth
  17. {
  18. protected $noNeedLogin=['category','index','getLimit','show','comments'];
  19. /**
  20. * @Title("分类")
  21. * @Returned ("id",desc="id")
  22. * @Returned ("name",desc="名称")
  23. */
  24. public function category(){
  25. $list=DataXwCategory::show()->order('sort','desc')->select();
  26. $this->success('',$list);
  27. }
  28. /**
  29. * @Title("列表")
  30. * @Param ("page",desc="第几页")
  31. * @Param ("cid",desc="分类ID")
  32. * @Param ("keyword",desc="搜索关键字")
  33. * @Param ("exad",desc="之前加载的列表中所有的广告ID,用,隔开",default="")
  34. * @Returned ("id",desc="id")
  35. * @Returned ("title",desc="标题")
  36. * @Returned ("images",desc="封面,用的|隔开的")
  37. * @Returned ("has_video",desc="是否含有视频")
  38. * @Returned ("source",desc="来源")
  39. * @Returned ("ctype",desc="1新闻2广告")
  40. * @Returned ("create_time",desc="创建时间")
  41. * @Returned ("content",desc="内容")
  42. * @Returned ("likes_count",desc="点赞数")
  43. * @Returned ("is_liked",desc="是否已点赞")
  44. * @Returned ("user.avatar",desc="发布者用户头像")
  45. * @Returned ("user.username",desc="发布者用户名")
  46. */
  47. public function index(){
  48. $page=input('page/d')?:1;
  49. $exad=input('exad');
  50. $cid=input('cid');
  51. $keyword=input('keyword');
  52. list($limit,$adNum,$splitNum)=$this->getLimit();
  53. $xw=DataXw::show()
  54. ->when($cid,function (Query $query) use ($cid) {
  55. $query->where('category_id',$cid);
  56. })
  57. ->when($keyword,function (Query $query) use ($keyword) {
  58. $query->whereLike('title',"%$keyword%");
  59. })
  60. ->with('category')
  61. ->withCount('comments')
  62. ->hidden(['content'])
  63. ->order('id','desc')
  64. ->paginate($limit);
  65. $xwArr=$xw->chunk($splitNum);
  66. if($xw->isEmpty()){
  67. $this->success('',$xw);
  68. }
  69. $ads=DataXwAd::show()
  70. ->order('sort','desc')
  71. ->when($exad,function (Query $query)use ($exad){
  72. $query->whereNotIn('id',$exad);
  73. })
  74. ->limit($adNum)
  75. ->select();
  76. $newList=[];
  77. foreach ($xwArr as $key=>$xwOne){
  78. $ad=$ads[$key]??null;
  79. foreach ($xwOne as $one){
  80. $one['ctype']=1;
  81. $newList[]=$one;
  82. }
  83. if($ad && count($xwOne)>=$splitNum){
  84. $ad['ctype']=2;
  85. $newList[]=$ad;
  86. }
  87. }
  88. $this->success('成功',[
  89. 'data'=>$newList,
  90. 'total'=>$xw->total(),
  91. 'per_page'=>$limit,
  92. 'current_page'=>$page,
  93. 'last_page'=>$xw->lastPage(),
  94. ]);
  95. }
  96. protected function getLimit(){
  97. $splitNum=sysconf('config_xw.split_num')?:3;
  98. $min=15;
  99. $limit=1;
  100. while ($limit*$splitNum<$min){
  101. $limit++;
  102. }
  103. return [$limit*$splitNum,$limit,$splitNum];
  104. }
  105. /**
  106. * @Title("详情")
  107. * @Param ("id",desc="新闻id")
  108. * @Returned ("见列表")
  109. */
  110. public function show(){
  111. $xw=DataXw::show()
  112. ->withCount(['comments','likes'])
  113. ->where('id',input('id'))->findOrFail();
  114. $xw->append(['user']);
  115. $xw->is_liked=$xw->isLiked($this->getUser());
  116. $this->success('',$xw);
  117. }
  118. /**
  119. * @Title("广告详情")
  120. * @Param ("id",desc="广告id")
  121. * @Returned ("见列表")
  122. */
  123. public function ad_show(){
  124. $xw=DataXwAd::show()
  125. ->where('id',input('id'))->findOrFail();
  126. $this->success('',$xw);
  127. }
  128. /**
  129. * @Title("点赞或取消")
  130. * @Param ("id",desc="新闻id")
  131. */
  132. public function like(){
  133. $xw=DataXw::show()
  134. ->where('id',input('id'))->findOrFail();
  135. $res = $xw->makeLike($this->getUser());
  136. $this->success($res);
  137. }
  138. /**
  139. * @Title("评论列表")
  140. * @Param ("id",desc="新闻id")
  141. * @Param ("page",desc="第几页")
  142. * @Param ("limit",desc="每页数量")
  143. * @Returned ("id",desc="评论ID")
  144. * @Returned ("user.nickname",desc="发布者用户昵称")
  145. * @Returned ("user.headimg",desc="发布者用户头像")
  146. * @Returned ("create_time",desc="发布时间")
  147. * @Returned ("content",desc="发布内容")
  148. */
  149. public function comments(){
  150. $this->_vali([
  151. 'id.require'=>'新闻有误',
  152. 'id.integer'=>'新闻有误',
  153. 'id.gt:0'=>'新闻有误',
  154. ],'get');
  155. $xw=DataXw::show()
  156. ->where('id',input('id'))->findOrFail();
  157. $comments=$xw
  158. ->comments()
  159. ->with(['user'])
  160. ->paginate(input('limit',15));
  161. $this->success('',$comments);
  162. }
  163. /**
  164. * @Title("评论")
  165. * @Method("POST")
  166. * @Param ("id",desc="新闻id")
  167. * @Param ("content",desc="内容")
  168. */
  169. public function comment(){
  170. $data=$this->_vali([
  171. 'id.require'=>'新闻有误',
  172. 'id.integer'=>'新闻有误',
  173. 'id.gt:0'=>'新闻有误',
  174. 'content.require'=>'内容必须',
  175. 'content.max:250'=>'内容过多',
  176. ]);
  177. $xw=DataXw::show()->where('id',$data['id'])->findOrFail();
  178. $user=$this->getUser();
  179. $comment=$xw->comments()->save([
  180. 'content'=>$data['content'],
  181. 'uuid'=>$user['id'],
  182. ]);
  183. $this->success('评论成功',$comment);
  184. }
  185. /**
  186. * @Title("删除评论")
  187. * @Method("POST")
  188. * @Param ("id",desc="评论id")
  189. */
  190. public function del_comment(){
  191. $data=$this->_vali([
  192. 'id.require'=>'评论有误',
  193. 'id.integer'=>'评论有误',
  194. 'id.gt:0'=>'评论有误',
  195. ]);
  196. DataXwComment::where('id',$data['id'])->findOrFail();
  197. // $user=$this->getUser();
  198. DataXwComment::where('id',$data['id'])->delete();
  199. $this->success('删除成功');
  200. }
  201. }