PostTrait.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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,true);
  85. foreach ($comment as &$item) {
  86. $isLike = 0;
  87. if ($uid) {
  88. $isLike = Redis::ZSCORE($key, $uid . '_' . $item['id']) ? 1 : 0;
  89. }
  90. $item['uid'] = intval($item['uid']);
  91. $item['is_like'] = $isLike;
  92. $item['like_count'] = Redis::ZCOUNT($key, $item['id'], $item['id']);
  93. $item['reply_count'] = 0;
  94. $item['reply'] = [];
  95. }
  96. } else {
  97. $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('like_count', 'desc')->orderBy('id', 'desc')->limit(2)->get();
  98. foreach ($comments as $item) {
  99. $userComment = $this->userInfo($item->uid);
  100. $isLike = 0;
  101. if ($uid) {
  102. $isLike = Redis::ZSCORE($key, $uid . '_' . $item->id) ? 1 : 0;
  103. }
  104. $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
  105. $comment[] = [
  106. 'id' => $item->id,
  107. 'uid' => intval($userComment['uid']),
  108. 'username' => $userComment['username'],
  109. 'content' => $item->is_delete ? '该评论已被删除' : $item->content,
  110. 'is_delete' => $item->is_delete,
  111. 'is_like' => $isLike,
  112. 'like_count' => $likeCount,
  113. 'reply_count' => 0,
  114. 'reply' => [],
  115. ];
  116. }
  117. Redis::SET($commentKey, json_encode($comment));
  118. Redis::EXPIRE($commentKey, 300);
  119. }
  120. sortArrByField($comment, 'like_count', true);
  121. return $comment;
  122. }
  123. //获取评论点赞数、点赞状态
  124. public function getCommentLike($postId, $commentId, $uid = 0)
  125. {
  126. $key = 'comment_like_' . $postId;
  127. $isLike = 0;
  128. if ($uid) {
  129. $isLike = Redis::ZSCORE($key, $uid . '_' . $commentId) ? 1 : 0;
  130. }
  131. $likeCount = Redis::ZCOUNT($key, $commentId, $commentId);
  132. return [
  133. 'is_like' => $isLike,
  134. 'like_count' => $likeCount
  135. ];
  136. }
  137. }