1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 04:21:27 +08:00
2020-06-14 19:56:14 +08:00

137 lines
3.7 KiB
PHP

<?php
namespace App\Services\Frontend\Chapter;
use App\Models\Chapter as ChapterModel;
use App\Models\Course as CourseModel;
use App\Repos\Chapter as ChapterRepo;
use App\Services\ChapterVod as ChapterVodService;
use App\Services\Frontend\ChapterTrait;
use App\Services\Frontend\Service as FrontendService;
use App\Services\Live as LiveService;
use WhichBrowser\Parser as BrowserParser;
class ChapterBasic extends FrontendService
{
use ChapterTrait;
public function handle($id)
{
$chapter = $this->checkChapterCache($id);
return $this->handleChapter($chapter);
}
protected function handleChapter(ChapterModel $chapter)
{
/**
* @var array $attrs
*/
$attrs = $chapter->attrs;
$result = [];
switch ($attrs['model']) {
case CourseModel::MODEL_VOD:
$result = $this->formatChapterVod($chapter);
break;
case CourseModel::MODEL_LIVE:
$result = $this->formatChapterLive($chapter);
break;
case CourseModel::MODEL_READ:
$result = $this->formatChapterRead($chapter);
break;
}
return $result;
}
protected function formatChapterVod(ChapterModel $chapter)
{
$chapterVodService = new ChapterVodService();
$playUrls = $chapterVodService->getPlayUrls($chapter->id);
/**
* @var array $attrs
*/
$attrs = $chapter->attrs;
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
'model' => $attrs['model'],
'play_urls' => $playUrls,
'user_count' => $chapter->user_count,
'agree_count' => $chapter->agree_count,
'oppose_count' => $chapter->oppose_count,
'comment_count' => $chapter->comment_count,
];
}
protected function formatChapterLive(ChapterModel $chapter)
{
$headers = getallheaders();
$browserParser = new BrowserParser($headers);
$liveService = new LiveService();
$stream = "chapter-{$chapter->id}";
$format = $browserParser->isType('desktop') ? 'flv' : 'hls';
$playUrls = $liveService->getPullUrls($stream, $format);
$chapterRepo = new ChapterRepo();
$live = $chapterRepo->findChapterLive($chapter->id);
/**
* @var array $attrs
*/
$attrs = $chapter->attrs;
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
'model' => $attrs['model'],
'play_urls' => $playUrls,
'start_time' => $live->start_time,
'end_time' => $live->end_time,
'user_count' => $chapter->user_count,
'agree_count' => $chapter->agree_count,
'oppose_count' => $chapter->oppose_count,
'comment_count' => $chapter->comment_count,
];
}
protected function formatChapterRead(ChapterModel $chapter)
{
$chapterRepo = new ChapterRepo();
$read = $chapterRepo->findChapterRead($chapter->id);
/**
* @var array $attrs
*/
$attrs = $chapter->attrs;
return [
'id' => $chapter->id,
'title' => $chapter->title,
'summary' => $chapter->summary,
'model' => $attrs['model'],
'content' => $read->content,
'user_count' => $chapter->user_count,
'agree_count' => $chapter->agree_count,
'oppose_count' => $chapter->oppose_count,
'comment_count' => $chapter->comment_count,
];
}
}