CourseResource.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "teacher"=> UserApi::getByUuid($this->teacher),
  30. "course_count"=>10,
  31. "member_count"=>CourseMember::where('course_id',$this->id)->count(),
  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. "request_exp"=> $this->request_exp,
  43. "created_at"=> $this->created_at,
  44. "updated_at"=> $this->updated_at,
  45. ];
  46. if($this->cover){
  47. $thumb = str_replace('.jpg','_m.jpg',$this->cover);
  48. if (App::environment('local')) {
  49. $data['cover_url'] = [Storage::url($this->cover),Storage::url($thumb)];
  50. }else{
  51. $data['cover_url'] = [
  52. Storage::temporaryUrl($this->cover, now()->addDays(6)),
  53. Storage::temporaryUrl($thumb, now()->addDays(6)),
  54. ];
  55. }
  56. }
  57. if(Str::isUuid($this->cover)){
  58. $coverId = Attachment::find($this->cover);
  59. $cover_url = [];
  60. if($coverId){
  61. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->name);
  62. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_s.jpg');
  63. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_m.jpg');
  64. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_l.jpg');
  65. $data['cover_url'] = $cover_url;
  66. }
  67. }
  68. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  69. if($textbook){
  70. $data['anthology_id'] = $textbook->uid;
  71. $data['anthology_title'] = $textbook->title;
  72. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  73. }
  74. if(!empty($this->channel_id)){
  75. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  76. if($channel){
  77. $data['channel_name'] = $channel->name;
  78. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  79. }
  80. }
  81. if($request->get('view') === "study" || $request->get('view') === "teach"){
  82. $user = AuthApi::current($request);
  83. if($user){
  84. $course_member = CourseMember::where('user_id',$user["user_uid"])
  85. ->where('course_id',$this->id)
  86. ->where('is_current',true)
  87. ->first();
  88. if($course_member){
  89. $data['my_status'] = $course_member->status;
  90. $data['my_status_id'] = $course_member->id;
  91. }
  92. }
  93. }else{
  94. //计算待审核
  95. $data['count_progressing'] = CourseMember::where('course_id',$this->id)
  96. ->where('status',"progressing")
  97. ->count();
  98. }
  99. return $data;
  100. }
  101. }