CourseResource.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class CourseResource extends JsonResource
  14. {
  15. /**
  16. * Transform the resource into an array.
  17. *
  18. * @param \Illuminate\Http\Request $request
  19. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  20. */
  21. public function toArray($request)
  22. {
  23. $data = [
  24. "id"=>$this->id,
  25. "title"=> $this->title,
  26. "subtitle"=> $this->subtitle,
  27. "summary"=> $this->summary,
  28. "teacher"=> UserApi::getByUuid($this->teacher),
  29. "course_count"=>10,
  30. "member_count"=>CourseMember::where('course_id',$this->id)->count(),
  31. "publicity"=> $this->publicity,
  32. "start_at"=> $this->start_at,
  33. "end_at"=> $this->end_at,
  34. "content"=> $this->content,
  35. "content_type"=> $this->content_type,
  36. "cover"=> $this->cover,
  37. "channel_id"=>$this->channel_id,
  38. "join"=> $this->join,
  39. "request_exp"=> $this->request_exp,
  40. "created_at"=> $this->created_at,
  41. "updated_at"=> $this->updated_at,
  42. ];
  43. if(Str::isUuid($this->cover)){
  44. $coverId = Attachment::find($this->cover);
  45. $cover_url = [];
  46. if($coverId){
  47. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->name);
  48. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_s.jpg');
  49. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_m.jpg');
  50. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_l.jpg');
  51. $data['cover_url'] = $cover_url;
  52. }
  53. }
  54. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  55. if($textbook){
  56. $data['anthology_id'] = $textbook->uid;
  57. $data['anthology_title'] = $textbook->title;
  58. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  59. }
  60. if(!empty($this->channel_id)){
  61. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  62. if($channel){
  63. $data['channel_name'] = $channel->name;
  64. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  65. }
  66. }
  67. if($request->get('view')==="study"){
  68. $user = AuthApi::current($request);
  69. if(!$user){
  70. return $this->error(__('auth.failed'));
  71. }
  72. $course_member = CourseMember::where('user_id',$user["user_uid"])
  73. ->where('course_id',$this->id)
  74. ->select('status')
  75. ->first();
  76. if($course_member){
  77. $data['my_status'] = $course_member->status;
  78. }
  79. }else{
  80. //计算待审核
  81. $data['count_progressing'] = CourseMember::where('course_id',$this->id)
  82. ->where('status',"progressing")
  83. ->count();
  84. }
  85. return $data;
  86. }
  87. }