|
|
<?php
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
|
use App\Models\Activity;
|
|
|
use App\Models\StudyTour;
|
|
|
use App\Models\Venue;
|
|
|
|
|
|
/**
|
|
|
* H5 活动列表/详情:封面与轮播展示规则(活动缺省则回退关联场馆)。
|
|
|
*/
|
|
|
final class ActivityH5View
|
|
|
{
|
|
|
/**
|
|
|
* 列表/详情头图:优先活动封面,无则场馆封面。
|
|
|
*/
|
|
|
public static function listCover(Activity $a): ?string
|
|
|
{
|
|
|
$c = trim((string) ($a->cover_image ?? ''));
|
|
|
if ($c !== '') {
|
|
|
return $c;
|
|
|
}
|
|
|
|
|
|
$v = trim((string) ($a->venue?->cover_image ?? ''));
|
|
|
|
|
|
return $v !== '' ? $v : null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 轮播素材:gallery_media(可含图片/视频);为空时用封面图兜底。
|
|
|
*
|
|
|
* @return array<int, array{type: string, url: string}>
|
|
|
*/
|
|
|
public static function buildGalleryCarousel(Venue|Activity|StudyTour $model): array
|
|
|
{
|
|
|
$items = [];
|
|
|
$seen = [];
|
|
|
|
|
|
foreach ($model->gallery_media ?? [] as $m) {
|
|
|
if (! is_array($m)) {
|
|
|
continue;
|
|
|
}
|
|
|
$url = trim((string) ($m['url'] ?? ''));
|
|
|
if ($url === '' || isset($seen[$url])) {
|
|
|
continue;
|
|
|
}
|
|
|
$type = $m['type'] ?? 'image';
|
|
|
if (! in_array($type, ['image', 'video'], true)) {
|
|
|
$type = 'image';
|
|
|
}
|
|
|
$seen[$url] = true;
|
|
|
$items[] = ['type' => $type, 'url' => $url];
|
|
|
}
|
|
|
|
|
|
if ($items === [] && $model->cover_image) {
|
|
|
$url = trim((string) $model->cover_image);
|
|
|
if ($url !== '') {
|
|
|
$items[] = ['type' => 'image', 'url' => $url];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return $items;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* H5 轮播:优先活动上传的轮播/封面;皆无则用场馆轮播/封面。
|
|
|
*
|
|
|
* @return array<int, array{type: string, url: string}>
|
|
|
*/
|
|
|
public static function buildActivityH5Carousel(Activity $a): array
|
|
|
{
|
|
|
$fromActivity = self::buildGalleryCarousel($a);
|
|
|
if (count($fromActivity) > 0) {
|
|
|
return $fromActivity;
|
|
|
}
|
|
|
if ($a->venue) {
|
|
|
return self::buildGalleryCarousel($a->venue);
|
|
|
}
|
|
|
|
|
|
return [];
|
|
|
}
|
|
|
}
|