CourseResource.php 4.8 KB

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