Comment.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\ArticleEvent as ArticleEvent;
  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 get_list(Model $Model){
  21. // 查询条件
  22. $map = [['status','=','0']];
  23. // 查询文章数据
  24. $Paginator = $Model->query()->where($map)->orderByDesc('id')->paginate(request('limit',config('page_num',10)));
  25. // 循环处理数据
  26. foreach ($Paginator as $key => $value) {
  27. // 海报图
  28. $value['poster'] = path_compat($value['poster']);
  29. // 如果没有缩略图,使用海报图
  30. $value['thumb'] = path_compat($value['thumb']);
  31. // 获取数据
  32. $value['insert_time'] = date('Y-m-d',$value['insert_time']);
  33. // 重组
  34. $Paginator[$key] = $value;
  35. }
  36. // 获取数据
  37. $data['total'] = $Paginator->total();
  38. $data['current_page'] = $Paginator->currentPage();
  39. $data['per_page'] = $Paginator->perPage();
  40. $data['last_page'] = $Paginator->lastPage();
  41. $data['data'] = $Paginator->items();
  42. // 分配数据
  43. return json_send(['code'=>'success','msg'=>'列表数据','data'=>$data]);
  44. }
  45. /**
  46. * 数据详情
  47. *
  48. * */
  49. public function get_detail(Model $Model, ArticleEvent $ArticleEvent){
  50. // 检查登录
  51. $uid = $this->checkLogin();
  52. // 接受参数
  53. $id = request('id',0);
  54. // 获取旧数据
  55. $articleData = $Model->query()->where([['status','=',0]])->find($id,['id','title','poster','content','path','appid','read_count','hand_count','like_count','share_count','insert_time']);
  56. // 用户是否点赞
  57. if( !$articleData ) return json_send(['code'=>'error','msg'=>'文章不存在或者已下架']);
  58. // 转数组
  59. $articleData = $articleData->toArray();
  60. // 阅读数 + 1
  61. $Model->query()->where([['id','=',$id]])->increment('read_count');
  62. // 如果事件不存在,新增此次事件
  63. $ArticleEvent->add(['custom_uid'=>$uid,'article_id'=>$id,'type_id'=>1,'status'=>0]);
  64. // 用户是否点赞和推荐
  65. $articleData['is_hand'] = $uid ? (int) $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$id],['type_id','=',2],['status','=',0]])->value('id') : 0;
  66. $articleData['is_like'] = $uid ? (int) $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$id],['type_id','=',4],['status','=',0]])->value('id') : 0;
  67. $articleData['read_count'] += 1;
  68. $articleData['insert_time'] = date('Y年m月d日 H:s',$articleData['insert_time']);
  69. $articleData['poster'] = $articleData['poster'] ? path_compat($articleData['poster']) : '';
  70. // 返回结果
  71. return json_send(['code'=>'success','msg'=>'暂无','data'=>$articleData]);
  72. }
  73. /**
  74. * 数据详情
  75. *
  76. * */
  77. public function update_event(Model $Model, ArticleEvent $ArticleEvent){
  78. // 检查登录
  79. $uid = $this->checkLogin();
  80. // 接收参数
  81. $typeId = request('type_id',0);
  82. $articleId = request('article_id',0);
  83. // 如果用户不存在
  84. $oldData = $uid ? $ArticleEvent->query()->where([['custom_uid','=',$uid],['article_id','=',$articleId],['type_id','=',$typeId]])->first(['id','status']) : [];
  85. // 判断事件。
  86. $oldData = $oldData ? $oldData->toArray() : [];
  87. // 事件已存在,并且是点赞或者喜欢
  88. if( $oldData && in_array($typeId,[2,4]) ){
  89. // 更新事件状态
  90. $ArticleEvent->edit($oldData['id'],['status'=>$oldData['status']?0:1]);
  91. }else{
  92. // 新增事件
  93. $ArticleEvent->add(['custom_uid'=>$uid,'article_id'=>$articleId,'type_id'=>$typeId,'status'=>0]);
  94. }
  95. // 判断数据类型,点赞
  96. if( $typeId == 2 ) {
  97. // 如果旧数据是未点赞或者取消点赞,继续点赞
  98. ( !$oldData || !empty($oldData['status']) ) ? $Model->query()->where([['id','=',$articleId]])->increment('hand_count') : $Model->query()->where([['id','=',$articleId],['hand_count','>=',1]])->decrement('hand_count');
  99. }
  100. // 判断数据类型,分享
  101. if( $typeId == 3 ) $Model->query()->where([['id','=',$articleId]])->increment('share_count');
  102. // 判断数据类型,喜欢
  103. if( $typeId == 4 ) {
  104. // 如果旧数据是未喜欢或者取消喜欢或,继续喜欢
  105. ( !$oldData || !empty($oldData['status']) ) ? $Model->query()->where([['id','=',$articleId]])->increment('like_count') : $Model->query()->where([['id','=',$articleId],['like_count','>=',1]])->decrement('like_count') ;
  106. }
  107. // 返回结果
  108. return json_send(['code'=>'success','msg'=>'操作成功','data'=>'']);
  109. }
  110. }