CourseResource.php 4.1 KB

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