SentResource.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. "updated_at"=> $this->updated_at,
  39. ];
  40. if($request->has('channels')){
  41. $channels = explode(',',$request->get('channels'));
  42. }else{
  43. $channels = [$this->channel_uid];
  44. }
  45. //TODO 找出channel id = '' 的原因
  46. $mChannels=array();
  47. foreach ($channels as $key => $value) {
  48. if(Str::isUuid($value)){
  49. $mChannels[] = $value;
  50. }
  51. }
  52. if($request->get('html',true)){
  53. $data['html'] = MdRender::render($this->content,
  54. $mChannels,
  55. null,
  56. $mode,
  57. $channel['type'],
  58. $this->content_type,
  59. $request->get('format','react')
  60. );
  61. }
  62. if($request->get('mode') === "edit" || $request->get('mode') === "wbw"){
  63. $data['suggestionCount'] = SuggestionApi::getCountBySent($this->book_id,
  64. $this->paragraph,
  65. $this->word_start,
  66. $this->word_end,
  67. $this->channel_uid
  68. );
  69. }
  70. if(isset($this->acceptor_uid) && !empty($this->acceptor_uid)){
  71. $data["acceptor"]=UserApi::getByUuid($this->acceptor_uid);
  72. $data["pr_edit_at"]=$this->pr_edit_at;
  73. }
  74. return $data;
  75. }
  76. }