SentResource.php 3.2 KB

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