2
0

SentPrResource.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\MdRender;
  5. use App\Http\Api\UserApi;
  6. use App\Services\AuthService;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Resources\Json\JsonResource;
  10. class SentPrResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param Request $request
  16. * @return array|Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. // 获取用户信息
  21. $user = AuthService::current($request);
  22. $role = 'reader';
  23. if ($user && $user['user_uid'] === $this->editor_uid) {
  24. $role = 'owner';
  25. }
  26. $channel = ChannelApi::getById($this->channel_uid);
  27. $mode = $request->input('mode', 'read');
  28. return [
  29. 'id' => $this->id,
  30. 'uid' => $this->uid,
  31. 'book' => $this->book_id,
  32. 'paragraph' => $this->paragraph,
  33. 'word_start' => $this->word_start,
  34. 'word_end' => $this->word_end,
  35. 'editor' => UserApi::getByUuid($this->editor_uid),
  36. 'channel' => $channel,
  37. 'content' => $this->content,
  38. 'html' => MdRender::render($this->content, [$this->channel_uid], null, $mode, $channel['type']),
  39. 'role' => $role,
  40. 'created_at' => $this->created_at,
  41. 'updated_at' => $this->updated_at,
  42. ];
  43. }
  44. }