You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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 [];
}
}