CommentTransformer.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 16:40
  7. */
  8. namespace App\Transformers\Circle;
  9. use App\Models\InterestCircleMessageComment;
  10. use App\Models\PostComment;
  11. use App\Traits\PostTrait;
  12. use App\Traits\UserTrait;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Redis;
  16. use League\Fractal\TransformerAbstract;
  17. class CommentTransformer extends TransformerAbstract
  18. {
  19. use UserTrait;
  20. use PostTrait;
  21. public function __construct($msgId, $uid)
  22. {
  23. $this->msgId = $msgId;
  24. $this->uid = $uid;
  25. }
  26. public function transform(InterestCircleMessageComment $interestCircleMessageComment)
  27. {
  28. $reply = [];
  29. $replyKey = 'circle_message_new_reply_'.$interestCircleMessageComment['id'];
  30. $replyData = Redis::GET($replyKey);
  31. if($replyData){
  32. $reply = json_decode($replyData);
  33. foreach($reply as &$item){
  34. $item->created_at = Carbon::parse($item->created_at)->diffForHumans();
  35. }
  36. }else{
  37. $replies = InterestCircleMessageComment::where('parent_id', $interestCircleMessageComment['id'])->orderBy('id', 'desc')->limit(2)->get();
  38. $redisReply = [];
  39. foreach($replies as $val){
  40. $userComment = $this->userInfo($val->uid);
  41. $replyUsername = '';
  42. if($val->reply_uid){
  43. $userReply = $this->userInfo($val->reply_uid);
  44. $replyUsername = $userReply['username'];
  45. }
  46. $reply[] = [
  47. 'id' => $val->id,
  48. 'uid' => $val->uid,
  49. 'username' => $userComment['username'],
  50. 'avatar' => $userComment['avatar'],
  51. 'reply_username' => $replyUsername,
  52. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  53. 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
  54. 'is_delete' => $val->is_delete,
  55. ];
  56. $redisReply[] = [
  57. 'id' => $val->id,
  58. 'uid' => $val->uid,
  59. 'username' => $userComment['username'],
  60. 'avatar' => $userComment['avatar'],
  61. 'reply_username' => $replyUsername,
  62. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  63. 'created_at' => $val->created_at,
  64. 'is_delete' => $val->is_delete,
  65. ];
  66. }
  67. Redis::SET($replyKey, json_encode($redisReply));
  68. Redis::EXPIRE($replyKey, 604800);
  69. }
  70. $user = $this->userInfo($interestCircleMessageComment['uid']);
  71. return [
  72. 'id' => $interestCircleMessageComment['id'],
  73. 'uid' => $interestCircleMessageComment['uid'],
  74. 'username' => $user['username'],
  75. 'avatar' => $user['avatar'],
  76. 'content' => $interestCircleMessageComment['is_delete']?'该评论已被删除':$interestCircleMessageComment['content'],
  77. 'created_at' => Carbon::parse($interestCircleMessageComment['created_at'])->diffForHumans(),
  78. 'reply_count' => $interestCircleMessageComment['reply_count'],
  79. 'reply' => $reply,
  80. 'is_delete' => $interestCircleMessageComment['is_delete'],
  81. ];
  82. }
  83. }