1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-23 20:00:27 +08:00
2020-08-22 19:47:57 +08:00

102 lines
2.5 KiB
PHP

<?php
namespace App\Http\Desktop\Services;
use App\Caches\IndexCarouselList as IndexCarouselListCache;
use App\Caches\IndexFreeCourseList as IndexFreeCourseListCache;
use App\Caches\IndexLiveList as IndexLiveListCache;
use App\Caches\IndexNewCourseList as IndexNewCourseListCache;
use App\Caches\IndexVipCourseList as IndexVipCourseListCache;
use App\Models\Carousel as CarouselModel;
class Index extends Service
{
public function getCarousels()
{
$cache = new IndexCarouselListCache();
/**
* @var array $carousels
*/
$carousels = $cache->get();
if (!$carousels) return [];
foreach ($carousels as $key => $carousel) {
$carousels[$key]['style'] = CarouselModel::htmlStyle($carousel['style']);
switch ($carousel['target']) {
case CarouselModel::TARGET_COURSE:
$carousels[$key]['url'] = $this->url->get([
'for' => 'desktop.course.show',
'id' => $carousel['content'],
]);
break;
case CarouselModel::TARGET_PAGE:
$carousels[$key]['url'] = $this->url->get([
'for' => 'desktop.page.show',
'id' => $carousel['content'],
]);
break;
case CarouselModel::TARGET_LINK:
$carousels[$key]['url'] = $carousel['content'];
break;
default:
break;
}
}
return $carousels;
}
public function getLives()
{
$cache = new IndexLiveListCache();
return $cache->get();
}
public function getNewCourses()
{
$cache = new IndexNewCourseListCache();
$courses = $cache->get();
return $this->handleCategoryCourses($courses);
}
public function getFreeCourses()
{
$cache = new IndexFreeCourseListCache();
$courses = $cache->get();
return $this->handleCategoryCourses($courses);
}
public function getVipCourses()
{
$cache = new IndexVipCourseListCache();
$courses = $cache->get();
return $this->handleCategoryCourses($courses);
}
protected function handleCategoryCourses($items, $limit = 8)
{
if (count($items) == 0) {
return [];
}
foreach ($items as &$item) {
$item['courses'] = array_slice($item['courses'], 0, $limit);
}
return $items;
}
}