PostTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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)
  56. {
  57. $data = Redis::HGETALL('post_info_'.$id);
  58. if($data){
  59. $data['praise_count'] = intval($data['praise_count']);
  60. $data['comment_count'] = intval($data['comment_count']);
  61. $data['collect_count'] = intval($data['collect_count']);
  62. $data['available_bean'] = intval($data['available_bean']);
  63. $data['collect_bean'] = intval($data['collect_bean']);
  64. $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
  65. $data['imgs'] = json_decode($data['imgs'], true);
  66. }
  67. return $data;
  68. }
  69. //获取内容最新评论
  70. public function getNewComment($id)
  71. {
  72. $comment = [];
  73. $commentKey = 'post_new_comment_'.$id;
  74. $commentData = Redis::GET($commentKey);
  75. if($commentData){
  76. $comment = json_decode($commentData);
  77. }else{
  78. $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
  79. foreach($comments as $item){
  80. $userComment = $this->userInfo($item->uid);
  81. $comment[] = [
  82. 'id' => $item->id,
  83. 'username' => $userComment['username'],
  84. 'content' => $item->is_delete?'该评论已被删除':$item->content,
  85. 'is_delete' => $item->is_delete,
  86. ];
  87. }
  88. Redis::SET($commentKey, json_encode($comment));
  89. Redis::EXPIRE($commentKey, 300);
  90. }
  91. return $comment;
  92. }
  93. }