Comment.php 5.7 KB

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