Comment.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php namespace App\Http\Controllers\Api\Article;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Article as Model;
  4. use App\Models\ArticleComment as ArticleCommentModel;
  5. /**
  6. * 分享设置
  7. *
  8. * @author huanglei
  9. *
  10. */
  11. class Comment extends Api{
  12. const EVENT_TYPE = [
  13. '1' => '阅读',
  14. '2' => '点赞',
  15. '3' => '分享',
  16. '4' => '推荐',
  17. '5' => '取消推荐',
  18. '6' => '取消点赞'
  19. ];
  20. public function list(Model $Model, ArticleCommentModel $ArticleCommentModel){
  21. // 接受参数
  22. $status = request('status', null);
  23. $startTime = request('start_time','');
  24. $endTime = request('end_time','');
  25. // 查询条件
  26. $map = [];
  27. // 编码ID
  28. if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
  29. if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
  30. if( !is_null($status) ) $map[] = ['status','=',$status];
  31. // 查询文章数据
  32. $list = $Model->query()->where($map);
  33. $list = $list->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  34. // 循环处理数据
  35. foreach ($list as $key => $value) {
  36. // id转编号
  37. $value['article_id'] = $value['id'];
  38. // 重组
  39. $value['poster'] = $value['poster']? path_compat($value['poster']):'';
  40. $value['read_count'] = $ArticleCommentModel->query()
  41. ->where(['article_id'=>$value['id'],'event_type'=>1])
  42. ->count();
  43. $value['like_count'] = $ArticleCommentModel->query()
  44. ->where(['article_id'=>$value['id'],'event_type'=>2])
  45. ->count();
  46. $value['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
  47. $list[$key] = $value;
  48. }
  49. // 分配数据
  50. return json_send(['code'=>'success','msg'=>'列表数据','data'=>$list]);
  51. }
  52. /**
  53. * 数据详情
  54. *
  55. * */
  56. public function detail(Model $Model, ArticleCommentModel $ArticleCommentModel){
  57. // 接受参数
  58. $tid = request('article_id',0);
  59. $uid = $this->checkLogin(); // custom_uid
  60. // 查询条件
  61. $map = [];
  62. // 编码ID
  63. if( $tid ) $map[] = ['id','=',$tid];
  64. $data = $Model->query()->where($map)->find($tid,['id','title','poster','content']);
  65. if ($data){
  66. $data['like_count'] = $ArticleCommentModel->query()
  67. ->where(['article_id'=>$tid,'event_type'=>2])
  68. ->count();
  69. $data['read_count'] = $ArticleCommentModel->query()
  70. ->where(['article_id'=>$tid,'event_type'=>1])
  71. ->count();
  72. $data['share_count'] = $ArticleCommentModel->query()
  73. ->where(['article_id'=>$tid,'event_type'=>3])
  74. ->count();
  75. $data['recommend_count'] = $ArticleCommentModel->query()
  76. ->where(['article_id'=>$tid,'event_type'=>4])
  77. ->count();
  78. if ($uid){
  79. $user_envent = $ArticleCommentModel->query()->where(['uid'=>$uid,'article_id'=>$tid])
  80. ->get();
  81. foreach ($user_envent as $value){
  82. if ($value['event_type'] == 2){
  83. $data['like_type'] = 2;
  84. }else{
  85. $data['like_type'] = 6;
  86. }
  87. if ($value['event_type'] == 4){
  88. $data['recommend_type'] = 4;
  89. }else{
  90. $data['recommend_type'] = 5;
  91. }
  92. }
  93. //写入浏览记录
  94. $read = [
  95. 'article_id' => $tid,
  96. 'event_type' => 1,
  97. 'uid' => $uid,
  98. 'insert_time' => time()
  99. ];
  100. //查询用户是否浏览过
  101. $read_type = $ArticleCommentModel->query()->where(['uid'=>$uid,'article_id'=>$tid])->value('event_type');
  102. if (!$read_type){
  103. $comment_id = $ArticleCommentModel->add($read);
  104. }
  105. }else{
  106. $read['event_type'] = 0;//禁止分享、推荐和点赞
  107. }
  108. }
  109. // 加载模板
  110. if (!empty($data)){
  111. return json_send(['code'=>'success','msg'=>'数据','data'=>$data]);
  112. }
  113. return json_send(['code'=>'success','msg'=>'暂无','data'=>[]]);
  114. }
  115. /**
  116. * 数据详情
  117. *
  118. * */
  119. public function update_event(Model $Model, ArticleCommentModel $ArticleCommentModel){
  120. // '1' => '阅读',
  121. // '2' => '点赞',
  122. // '3' => '分享',
  123. // '4' => '推荐',
  124. // '5' => '取消推荐',
  125. // '6' => '取消点赞'
  126. $event = request('event_type',0);
  127. $tid = request('article_id',0);
  128. $uid = $this->checkLogin(); // custom_uid
  129. // 查询条件
  130. $map = [];
  131. $map[] = ['uid','=',$uid];
  132. $map[] = ['article_id','=',$tid];
  133. $map[] = ['event_type','=',$event];
  134. $user_envent = $ArticleCommentModel->query()->where($map)->get();
  135. switch ($event){
  136. case 2:
  137. if ($user_envent){
  138. $Res = $ArticleCommentModel::query()
  139. ->where(['uid'=>$uid,'article_id'=>$tid])
  140. ->update(['event_type'=>'6']);
  141. return json_send(['code'=>'error','msg'=>'取消点赞','data'=>[]]);
  142. }
  143. break;
  144. case 4:
  145. if ($user_envent){
  146. $Res = $ArticleCommentModel::query()
  147. ->where(['uid'=>$uid,'article_id'=>$tid])
  148. ->update(['event_type'=>'4']);
  149. return json_send(['code'=>'error','msg'=>'取消推荐','data'=>[]]);
  150. }
  151. break;
  152. default:
  153. $map[] = ['uid','=',$uid];
  154. $map[] = ['article_id','=',$tid];
  155. $map[] = ['event_type','=',$event];
  156. }
  157. $user_envent = $ArticleCommentModel->query()->where($map)->get();
  158. if( $tid ) $map[] = ['id','=',$tid];
  159. // $orderRes = $Model::query()->where('article_id','=',$tid)->update(['status'=>'1']);
  160. //用户是否点赞和推荐
  161. return json_send(['code'=>'success','msg'=>'成功',
  162. 'data'=>[
  163. 'event_type'=>$event,'article_id'=>$tid]
  164. ]);
  165. }
  166. }