PostTrait.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/6
  6. * Time: 18:05
  7. */
  8. namespace App\Traits;
  9. use App\Models\Post;
  10. use App\Models\PostComment;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Redis;
  13. trait PostTrait
  14. {
  15. //预计可获得U米数
  16. public function availableBean()
  17. {
  18. $bean = Redis::get('yesterday_post_create_bean');
  19. $count = Redis::get('yesterday_post_count');
  20. $num = 1000;
  21. if($bean && $count){
  22. $num = $bean / $count;
  23. }
  24. $h = date('h');
  25. $H = date('H');
  26. $t = $h * 60 / 720 + 2;
  27. if(in_array(intval($H), [9,10,11,12,17.18,19,20,21])){
  28. $t += 0.5;
  29. }
  30. return intval($num * $t);
  31. }
  32. //详情
  33. public function detail($id)
  34. {
  35. return Post::join('post_data', 'post_data.post_id', '=', 'post.id')
  36. ->select('post.*')
  37. ->find($id);
  38. }
  39. //获取内容话题
  40. public function getTopic($topic_ids)
  41. {
  42. $ids = explode(',', $topic_ids);
  43. $topic = [];
  44. foreach($ids as $id){
  45. $name = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
  46. if($name && isset($name[0])){
  47. $topic[] = [
  48. 'id' => intval($id),
  49. 'name' => $name[0],
  50. ];
  51. }
  52. }
  53. return $topic;
  54. }
  55. //获取内容详情
  56. public function getPostInfo($id, $type = 0)
  57. {
  58. if($type){
  59. $res = Redis::SISMEMBER('delete_post_ids', $id);
  60. if($res){
  61. return [];
  62. }
  63. }
  64. $data = Redis::HGETALL('post_info_'.$id);
  65. if($data){
  66. $data['praise_count'] = intval($data['praise_count']);
  67. $data['comment_count'] = intval($data['comment_count']);
  68. $data['collect_count'] = intval($data['collect_count']);
  69. $data['available_bean'] = intval($data['available_bean']);
  70. $data['collect_bean'] = intval($data['collect_bean']);
  71. $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
  72. $data['imgs'] = json_decode($data['imgs'], true);
  73. }
  74. return $data;
  75. }
  76. //获取内容最新评论
  77. public function getNewComment($id, $uid = 0)
  78. {
  79. $comment = [];
  80. $key = 'comment_like_'.$id;
  81. $commentKey = 'post_new_comment_'.$id;
  82. $commentData = Redis::GET($commentKey);
  83. if($commentData){
  84. $comment = json_decode($commentData);
  85. foreach($comment as &$item){
  86. $isLike = 0;
  87. if($uid){
  88. $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
  89. }
  90. $item->is_like = $isLike;
  91. $item->like_count = Redis::ZCOUNT($key, $item->id, $item->id);
  92. }
  93. }else{
  94. $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('like_count', 'desc')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
  95. foreach($comments as $item){
  96. $userComment = $this->userInfo($item->uid);
  97. $isLike = 0;
  98. if($uid){
  99. $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
  100. }
  101. $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
  102. $comment[] = [
  103. 'id' => $item->id,
  104. 'username' => $userComment['username'],
  105. 'content' => $item->is_delete?'该评论已被删除':$item->content,
  106. 'is_delete' => $item->is_delete,
  107. 'is_like' => $isLike,
  108. 'like_count' => $likeCount,
  109. ];
  110. }
  111. Redis::SET($commentKey, json_encode($comment));
  112. Redis::EXPIRE($commentKey, 3);
  113. }
  114. return $comment;
  115. }
  116. //获取评论点赞数、点赞状态
  117. public function getCommentLike($postId, $commentId, $uid = 0)
  118. {
  119. $key = 'comment_like_'.$postId;
  120. $isLike = 0;
  121. if($uid){
  122. $isLike = Redis::ZSCORE($key, $uid.'_'.$commentId) ? 1 : 0;
  123. }
  124. $likeCount = Redis::ZCOUNT($key, $commentId, $commentId);
  125. return [
  126. 'is_like' => $isLike,
  127. 'like_count' => $likeCount
  128. ];
  129. }
  130. }