Comment.php 4.4 KB

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