SentResource.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  60. if($request->get('mode') === "edit" || $request->get('mode') === "wbw"){
  61. $data['suggestionCount'] = SuggestionApi::getCountBySent($this->book_id,
  62. $this->paragraph,
  63. $this->word_start,
  64. $this->word_end,
  65. $this->channel_uid
  66. );
  67. }
  68. if(isset($this->acceptor_uid) && !empty($this->acceptor_uid)){
  69. $data["acceptor"]=UserApi::getByUuid($this->acceptor_uid);
  70. $data["pr_edit_at"]=$this->pr_edit_at;
  71. }
  72. return $data;
  73. }
  74. }