Comment.php 4.5 KB

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