CourseResource.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Models\Collection;
  7. use App\Models\Channel;
  8. use App\Models\CourseMember;
  9. use App\Http\Api\AuthApi;
  10. use App\Models\Attachment;
  11. use Illuminate\Support\Facades\Storage;
  12. use Illuminate\Support\Str;
  13. use Illuminate\Support\Facades\App;
  14. class CourseResource extends JsonResource
  15. {
  16. /**
  17. * Transform the resource into an array.
  18. *
  19. * @param \Illuminate\Http\Request $request
  20. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  21. */
  22. public function toArray($request)
  23. {
  24. $data = [
  25. "id"=>$this->id,
  26. "title"=> $this->title,
  27. "subtitle"=> $this->subtitle,
  28. "summary"=> $this->summary,
  29. "sign_up_message"=> $this->sign_up_message,
  30. "teacher"=> UserApi::getByUuid($this->teacher),
  31. "course_count"=>10,
  32. "publicity"=> $this->publicity,
  33. "start_at"=> $this->start_at,
  34. "end_at"=> $this->end_at,
  35. "sign_up_start_at"=> $this->sign_up_start_at,
  36. "sign_up_end_at"=> $this->sign_up_end_at,
  37. "content"=> $this->content,
  38. "content_type"=> $this->content_type,
  39. "cover"=> $this->cover,
  40. "channel_id"=>$this->channel_id,
  41. "join"=> $this->join,
  42. "number"=> $this->number,
  43. "request_exp"=> $this->request_exp,
  44. "studio" => StudioApi::getById($this->studio_id),
  45. "created_at"=> $this->created_at,
  46. "updated_at"=> $this->updated_at,
  47. ];
  48. $data['member_count'] = CourseMember::where('course_id',$this->id)
  49. ->where('is_current',true)->count();
  50. $data['members'] = CourseMember::where('course_id',$this->id)
  51. ->where('is_current',true)
  52. ->select(['role','status'])
  53. ->get();
  54. $user = AuthApi::current($request);
  55. if($user){
  56. $data['my_role'] = CourseMember::where('course_id',$this->id)
  57. ->where('is_current',true)
  58. ->where('user_id',$user['user_uid'])
  59. ->value('role');
  60. }
  61. if($this->cover){
  62. $thumb = str_replace('.jpg','_m.jpg',$this->cover);
  63. if (App::environment('local')) {
  64. $data['cover_url'] = [Storage::url($this->cover),Storage::url($thumb)];
  65. }else{
  66. $data['cover_url'] = [
  67. Storage::temporaryUrl($this->cover, now()->addDays(6)),
  68. Storage::temporaryUrl($thumb, now()->addDays(6)),
  69. ];
  70. }
  71. }
  72. if(Str::isUuid($this->cover)){
  73. $coverId = Attachment::find($this->cover);
  74. $cover_url = [];
  75. if($coverId){
  76. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->name);
  77. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_s.jpg');
  78. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_m.jpg');
  79. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_l.jpg');
  80. $data['cover_url'] = $cover_url;
  81. }
  82. }
  83. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  84. if($textbook){
  85. $data['anthology_id'] = $textbook->uid;
  86. $data['anthology_title'] = $textbook->title;
  87. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  88. }
  89. if(!empty($this->channel_id)){
  90. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  91. if($channel){
  92. $data['channel_name'] = $channel->name;
  93. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  94. }
  95. }
  96. if($request->get('view') === "study" || $request->get('view') === "teach"){
  97. $user = AuthApi::current($request);
  98. if($user){
  99. $course_member = CourseMember::where('user_id',$user["user_uid"])
  100. ->where('course_id',$this->id)
  101. ->where('is_current',true)
  102. ->first();
  103. if($course_member){
  104. $data['my_status'] = $course_member->status;
  105. $data['my_status_id'] = $course_member->id;
  106. }
  107. }
  108. }else{
  109. //计算待审核
  110. $data['count_progressing'] = CourseMember::where('course_id',$this->id)
  111. ->where('status',"invited")
  112. ->where('is_current',true)
  113. ->count();
  114. }
  115. return $data;
  116. }
  117. }