MessageListTransformer.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/6
  6. * Time: 14:08
  7. */
  8. namespace App\Transformers\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleMessage;
  11. use App\Models\Post;
  12. use App\Traits\PostTrait;
  13. use App\Traits\UserTrait;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Facades\Redis;
  16. use League\Fractal\TransformerAbstract;
  17. class MessageListTransformer extends TransformerAbstract
  18. {
  19. use UserTrait;
  20. public function transform(InterestCircleMessage $interestCircleMessage)
  21. {
  22. $user = $this->userInfo($interestCircleMessage['uid']);
  23. $imgs = [];
  24. foreach($interestCircleMessage->imgs as $img){
  25. $imgs[] = $img['img'];
  26. }
  27. return [
  28. 'id' => $interestCircleMessage['id'],
  29. 'created_at' => Carbon::parse($interestCircleMessage['created_at'])->diffForHumans(),
  30. 'user' => $user ? $user : new \stdClass(),
  31. 'content' => $interestCircleMessage['content'],
  32. 'imgs' => $imgs,
  33. 'good' => $interestCircleMessage['good'],
  34. 'bad' => $interestCircleMessage['bad'],
  35. 'comment_count' => $interestCircleMessage['comment_count'],
  36. 'comment' => $this->getNewComment($interestCircleMessage['id']),
  37. ];
  38. }
  39. //获取内容最新评论
  40. public function getNewComment($id, $uid = 0)
  41. {
  42. $comment = [];
  43. $key = 'comment_like_' . $id;
  44. $commentKey = 'post_new_comment_' . $id;
  45. $commentData = Redis::GET($commentKey);
  46. if ($commentData) {
  47. $comment = json_decode($commentData, true);
  48. foreach ($comment as &$item) {
  49. $isLike = 0;
  50. if ($uid) {
  51. $isLike = Redis::ZSCORE($key, $uid . '_' . $item['id']) ? 1 : 0;
  52. }
  53. $item['uid'] = intval($item['uid']);
  54. $item['is_like'] = $isLike;
  55. $item['like_count'] = Redis::ZCOUNT($key, $item['id'], $item['id']);
  56. $item['reply_count'] = 0;
  57. $item['reply'] = [];
  58. }
  59. } else {
  60. $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('like_count', 'desc')->orderBy('id', 'desc')->limit(2)->get();
  61. foreach ($comments as $item) {
  62. $userComment = $this->userInfo($item->uid);
  63. $isLike = 0;
  64. if ($uid) {
  65. $isLike = Redis::ZSCORE($key, $uid . '_' . $item->id) ? 1 : 0;
  66. }
  67. $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
  68. $comment[] = [
  69. 'id' => $item->id,
  70. 'uid' => intval($userComment['uid']),
  71. 'username' => $userComment['username'],
  72. 'content' => $item->is_delete ? '该评论已被删除' : $item->content,
  73. 'is_delete' => $item->is_delete,
  74. 'is_like' => $isLike,
  75. 'like_count' => $likeCount,
  76. 'reply_count' => 0,
  77. 'reply' => [],
  78. ];
  79. }
  80. Redis::SET($commentKey, json_encode($comment));
  81. Redis::EXPIRE($commentKey, 300);
  82. }
  83. sortArrByField($comment, 'like_count', true);
  84. return $comment;
  85. }
  86. }