diff --git a/app/Builders/DanmuList.php b/app/Builders/DanmuList.php new file mode 100644 index 00000000..270514b4 --- /dev/null +++ b/app/Builders/DanmuList.php @@ -0,0 +1,99 @@ +getCourses($danmus); + + foreach ($danmus as $key => $danmu) { + $danmus[$key]['course'] = $courses[$danmu['course_id']] ?? new \stdClass(); + } + + return $danmus; + } + + public function handleChapters(array $danmus) + { + $chapters = $this->getChapters($danmus); + + foreach ($danmus as $key => $danmu) { + $danmus[$key]['chapter'] = $chapters[$danmu['chapter_id']] ?? new \stdClass(); + } + + return $danmus; + } + + public function handleUsers(array $danmus) + { + $users = $this->getUsers($danmus); + + foreach ($danmus as $key => $danmu) { + $danmus[$key]['user'] = $users[$danmu['user_id']] ?? new \stdClass(); + } + + return $danmus; + } + + public function getCourses(array $danmus) + { + $ids = kg_array_column($danmus, 'course_id'); + + $courseRepo = new CourseRepo(); + + $courses = $courseRepo->findByIds($ids, ['id', 'title']); + + $result = []; + + foreach ($courses->toArray() as $course) { + $result[$course['id']] = $course; + } + + return $result; + } + + public function getChapters(array $danmus) + { + $ids = kg_array_column($danmus, 'chapter_id'); + + $chapterRepo = new ChapterRepo(); + + $chapters = $chapterRepo->findByIds($ids, ['id', 'title']); + + $result = []; + + foreach ($chapters->toArray() as $chapter) { + $result[$chapter['id']] = $chapter; + } + + return $result; + } + + public function getUsers(array $danmus) + { + $ids = kg_array_column($danmus, 'user_id'); + + $userRepo = new UserRepo(); + + $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + + $baseUrl = kg_ci_base_url(); + + $result = []; + + foreach ($users->toArray() as $user) { + $user['avatar'] = $baseUrl . $user['avatar']; + $result[$user['id']] = $user; + } + + return $result; + } + +} diff --git a/app/Http/Web/Controllers/ChapterController.php b/app/Http/Web/Controllers/ChapterController.php index 30e405bb..8b3c4393 100644 --- a/app/Http/Web/Controllers/ChapterController.php +++ b/app/Http/Web/Controllers/ChapterController.php @@ -5,6 +5,7 @@ namespace App\Http\Web\Controllers; use App\Services\Frontend\Chapter\AgreeVote as ChapterAgreeVoteService; use App\Services\Frontend\Chapter\ChapterInfo as ChapterInfoService; use App\Services\Frontend\Chapter\CommentList as ChapterCommentListService; +use App\Services\Frontend\Chapter\DanmuList as ChapterDanmuListService; use App\Services\Frontend\Chapter\Learning as ChapterLearningService; use App\Services\Frontend\Chapter\OpposeVote as ChapterOpposeVoteService; use App\Services\Frontend\Course\ChapterList as CourseChapterListService; @@ -53,6 +54,18 @@ class ChapterController extends Controller $this->view->setVar('chapters', $chapters); } + /** + * @Get("/{id:[0-9]+}/danmu", name="web.chapter.danmu") + */ + public function danmuAction($id) + { + $service = new ChapterDanmuListService(); + + $items = $service->handle($id); + + return $this->jsonSuccess(['items' => $items]); + } + /** * @Get("/{id:[0-9]+}/comments", name="web.chapter.comments") */ diff --git a/app/Http/Web/Views/chapter/show_vod.volt b/app/Http/Web/Views/chapter/show_vod.volt index 04454ddd..62de09ae 100644 --- a/app/Http/Web/Views/chapter/show_vod.volt +++ b/app/Http/Web/Views/chapter/show_vod.volt @@ -3,6 +3,7 @@ {% block content %} {% set learning_url = url({'for':'web.chapter.learning','id':chapter.id}) %} + {% set danmu_url = url({'for':'web.chapter.danmu','id':chapter.id}) %}