Comment.php 4.7 KB

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