PostTrait.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Redis;
  12. trait PostTrait
  13. {
  14. //预计可获得U米数
  15. public function availableBean()
  16. {
  17. $bean = Redis::get('yesterday_post_create_bean');
  18. $count = Redis::get('yesterday_post_count');
  19. $num = 1000;
  20. if($bean && $count){
  21. $num = $bean / $count;
  22. }
  23. $h = date('h');
  24. $H = date('H');
  25. $t = $h * 60 / 720 + 2;
  26. if(in_array(intval($H), [9,10,11,12,17.18,19,20,21])){
  27. $t += 0.5;
  28. }
  29. return intval($num * $t);
  30. }
  31. //详情
  32. public function detail($id)
  33. {
  34. return Post::join('post_data', 'post_data.post_id', '=', 'post.id')
  35. ->select('post.*')
  36. ->find($id);
  37. }
  38. //获取内容话题
  39. public function getTopic($topic_ids)
  40. {
  41. $ids = explode(',', $topic_ids);
  42. $topic = [];
  43. foreach($ids as $id){
  44. $name = $topicNameArray = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
  45. if($name && isset($name[0])){
  46. $topic[] = [
  47. 'id' => intval($id),
  48. 'name' => $name[0],
  49. ];
  50. }
  51. }
  52. return $topic;
  53. }
  54. //获取内容详情
  55. public function getPostInfo($id, $type = 0)
  56. {
  57. if($type){
  58. $res = Redis::SISMEMBER('delete_post_ids', $id);
  59. if($res){
  60. return [];
  61. }
  62. }
  63. $data = Redis::HGETALL('post_info_'.$id);
  64. if($data){
  65. $data['praise_count'] = intval($data['praise_count']);
  66. $data['comment_count'] = intval($data['comment_count']);
  67. $data['collect_count'] = intval($data['collect_count']);
  68. $data['available_bean'] = intval($data['available_bean']);
  69. $data['collect_bean'] = intval($data['collect_bean']);
  70. $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
  71. $data['imgs'] = json_decode($data['imgs'], true);
  72. }
  73. return $data;
  74. }
  75. //获取内容最新评论
  76. public function getNewComment($id)
  77. {
  78. $comment = [];
  79. $commentKey = 'post_new_comment_'.$id;
  80. $commentData = Redis::GET($commentKey);
  81. if($commentData){
  82. $comment = json_decode($commentData);
  83. }else{
  84. $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
  85. foreach($comments as $item){
  86. $userComment = $this->userInfo($item->uid);
  87. $comment[] = [
  88. 'id' => $item->id,
  89. 'username' => $userComment['username'],
  90. 'content' => $item->is_delete?'该评论已被删除':$item->content,
  91. 'is_delete' => $item->is_delete,
  92. ];
  93. }
  94. Redis::SET($commentKey, json_encode($comment));
  95. Redis::EXPIRE($commentKey, 300);
  96. }
  97. return $comment;
  98. }
  99. }