MessageListTransformer.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\InterestCircleMessageComment;
  12. use App\Models\InterestCircleMessageRecord;
  13. use App\Models\Post;
  14. use App\Traits\PostTrait;
  15. use App\Traits\UserTrait;
  16. use Illuminate\Support\Carbon;
  17. use Illuminate\Support\Facades\Redis;
  18. use League\Fractal\TransformerAbstract;
  19. class MessageListTransformer extends TransformerAbstract
  20. {
  21. use UserTrait;
  22. public function __construct($uid)
  23. {
  24. $this->uid = $uid;
  25. }
  26. public function transform(InterestCircleMessage $interestCircleMessage)
  27. {
  28. $user = $this->formatUser($this->userInfo($interestCircleMessage['uid']));
  29. $imgs = [];
  30. foreach ($interestCircleMessage->imgs as $img) {
  31. $imgs[] = $img['image'];
  32. }
  33. return [
  34. 'id' => $interestCircleMessage['id'],
  35. 'created_at' => Carbon::parse($interestCircleMessage['created_at'])->diffForHumans(),
  36. 'user' => $user ? $user : new \stdClass(),
  37. 'content' => $interestCircleMessage['content'],
  38. 'imgs' => $imgs,
  39. 'good' => $interestCircleMessage['good'],
  40. 'bad' => $interestCircleMessage['bad'],
  41. 'action' => $this->getAction($this->uid,$interestCircleMessage['id']),
  42. 'comment_count' => $interestCircleMessage['comment_count'],
  43. 'comment' => $this->getNewComment($interestCircleMessage['id']),
  44. ];
  45. }
  46. public function getAction($uid, $msgId)
  47. {
  48. $info = InterestCircleMessageRecord::where([['uid', $uid], ['msg_id', $msgId]])->first();
  49. $status = 0;
  50. if ($info && $info->action == 1) {
  51. $status = 1;
  52. } elseif ($info && $info->action == -1) {
  53. $status = -1;
  54. }
  55. return $status;
  56. }
  57. //获取内容最新评论
  58. public function getNewComment($id, $uid = 0)
  59. {
  60. $comment = [];
  61. $commentKey = 'circle_message_new_comment_' . $id;
  62. $commentData = Redis::GET($commentKey);
  63. if ($commentData) {
  64. $comment = json_decode($commentData, true);
  65. foreach ($comment as &$item) {
  66. $item['uid'] = intval($item['uid']);
  67. $item['reply_count'] = 0;
  68. $item['reply'] = [];
  69. }
  70. } else {
  71. $comments = InterestCircleMessageComment::where('msg_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('id', 'desc')->limit(2)->get();
  72. foreach ($comments as $item) {
  73. $userComment = $this->userInfo($item->uid);
  74. $comment[] = [
  75. 'id' => $item->id,
  76. 'uid' => intval($userComment['uid']),
  77. 'username' => $userComment['username'],
  78. 'content' => $item->is_delete ? '该评论已被删除' : $item->content,
  79. 'is_delete' => $item->is_delete,
  80. 'reply_count' => 0,
  81. 'reply' => [],
  82. ];
  83. }
  84. Redis::SET($commentKey, json_encode($comment));
  85. Redis::EXPIRE($commentKey, 300);
  86. }
  87. return $comment;
  88. }
  89. }