CourseResource.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "cover_url"=> [Storage::url($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(Str::isUuid($this->cover)){
  45. $coverId = Attachment::find($this->cover);
  46. $cover_url = [];
  47. if($coverId){
  48. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->name);
  49. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_s.jpg');
  50. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_m.jpg');
  51. $cover_url[] = Storage::disk('public')->url($coverId->bucket.'/'.$coverId->id.'_l.jpg');
  52. $data['cover_url'] = $cover_url;
  53. }
  54. }
  55. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  56. if($textbook){
  57. $data['anthology_id'] = $textbook->uid;
  58. $data['anthology_title'] = $textbook->title;
  59. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  60. }
  61. if(!empty($this->channel_id)){
  62. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  63. if($channel){
  64. $data['channel_name'] = $channel->name;
  65. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  66. }
  67. }
  68. if($request->get('view')==="study"){
  69. $user = AuthApi::current($request);
  70. if(!$user){
  71. return $this->error(__('auth.failed'));
  72. }
  73. $course_member = CourseMember::where('user_id',$user["user_uid"])
  74. ->where('course_id',$this->id)
  75. ->select('status')
  76. ->first();
  77. if($course_member){
  78. $data['my_status'] = $course_member->status;
  79. }
  80. }else{
  81. //计算待审核
  82. $data['count_progressing'] = CourseMember::where('course_id',$this->id)
  83. ->where('status',"progressing")
  84. ->count();
  85. }
  86. return $data;
  87. }
  88. }