SentResource.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\MdRender;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\UserApi;
  7. use App\Http\Api\ChannelApi;
  8. use App\Http\Api\SuggestionApi;
  9. use Illuminate\Support\Str;
  10. class SentResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. $channel = ChannelApi::getById($this->channel_uid);
  21. if($request->get('mode','read')==="read"){
  22. $mode = 'read';
  23. }else{
  24. $mode = 'edit';
  25. }
  26. $data = [
  27. "id" => $this->uid,
  28. "content"=>$this->content,
  29. "content_type"=>$this->content_type,
  30. "html"=> "",
  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. "studio" => StudioApi::getById($channel["studio_id"]),
  38. 'fork_at' => $this->fork_at,
  39. "updated_at"=> $this->updated_at,
  40. ];
  41. if($request->has('channels')){
  42. $channels = explode(',',$request->get('channels'));
  43. }else{
  44. $channels = [$this->channel_uid];
  45. }
  46. //TODO 找出channel id = '' 的原因
  47. $mChannels=array();
  48. foreach ($channels as $key => $value) {
  49. if(Str::isUuid($value)){
  50. $mChannels[] = $value;
  51. }
  52. }
  53. if($request->get('html',true)){
  54. $data['html'] = MdRender::render($this->content,
  55. $mChannels,
  56. null,
  57. $mode,
  58. $channel['type'],
  59. $this->content_type,
  60. $request->get('format','react')
  61. );
  62. }
  63. if($request->get('mode') === "edit" || $request->get('mode') === "wbw"){
  64. $data['suggestionCount'] = SuggestionApi::getCountBySent($this->book_id,
  65. $this->paragraph,
  66. $this->word_start,
  67. $this->word_end,
  68. $this->channel_uid
  69. );
  70. }
  71. if(isset($this->acceptor_uid) && !empty($this->acceptor_uid)){
  72. $data["acceptor"]=UserApi::getByUuid($this->acceptor_uid);
  73. $data["pr_edit_at"]=$this->pr_edit_at;
  74. }
  75. return $data;
  76. }
  77. }