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.

307 lines
11 KiB

6 days ago
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Activity;
6 days ago
use App\Models\ActivityDay;
6 days ago
use App\Models\DictItem;
use App\Models\StudyTour;
use App\Models\Venue;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class H5ContentController extends Controller
{
public function activities(Request $request): JsonResponse
{
$size = max(1, min(30, (int) $request->input('page_size', 10)));
$rows = Activity::query()
->with('venue:id,name,lat,lng')
5 days ago
->visibleOnH5()
6 days ago
->when($request->filled('keyword'), function ($q) use ($request) {
$keyword = trim((string) $request->input('keyword'));
$q->where('title', 'like', "%{$keyword}%");
})
6 days ago
->orderForH5Listing()
6 days ago
->paginate($size);
$rows->getCollection()->transform(function ($a) {
return [
'id' => $a->id,
'title' => $a->title,
'summary' => $a->summary,
'image' => $a->cover_image,
'venue_name' => $a->venue?->name,
'address' => $a->address,
'lat' => $a->lat,
'lng' => $a->lng,
'venue_lat' => $a->venue?->lat,
'venue_lng' => $a->venue?->lng,
'start_at' => optional($a->start_at)?->toIso8601String(),
'end_at' => optional($a->end_at)?->toIso8601String(),
'registered_count' => (int) ($a->registered_count ?? 0),
'tags' => array_values($a->tags ?? []),
];
});
return response()->json($rows);
}
public function activityDetail(int $id): JsonResponse
{
$a = Activity::query()
6 days ago
->with([
5 days ago
'venue:id,name,address,lat,lng,open_time,district,venue_type,venue_types,ticket_type,unit_name,contact_phone',
6 days ago
'activityDays',
])
5 days ago
->visibleOnH5()
6 days ago
->findOrFail($id);
6 days ago
$isBookable = $a->activityDays->contains(
fn (ActivityDay $d) => $d->isCurrentlyBookable()
);
6 days ago
return response()->json([
'id' => $a->id,
'title' => $a->title,
'summary' => $a->summary,
'detail_html' => $a->detail_html,
'image' => $a->cover_image,
'gallery_media' => $a->gallery_media ?? [],
6 days ago
'carousel' => $this->buildGalleryCarousel($a),
6 days ago
'venue' => $a->venue,
'address' => $a->address,
5 days ago
'contact_phone' => $a->contact_phone,
6 days ago
'lat' => $a->lat,
'lng' => $a->lng,
'start_at' => optional($a->start_at)?->toIso8601String(),
'end_at' => optional($a->end_at)?->toIso8601String(),
'registered_count' => (int) ($a->registered_count ?? 0),
'tags' => array_values($a->tags ?? []),
'reservation_notice' => $a->reservation_notice,
6 days ago
'is_bookable' => $isBookable,
5 days ago
'venue_type_color' => $this->resolveVenueTypeColor($a->venue?->venue_type, $a->venue?->venue_types),
6 days ago
]);
}
public function venues(): JsonResponse
{
$rows = Venue::query()
5 days ago
->visibleOnH5()
6 days ago
->orderBy('sort')
->orderByDesc('id')
5 days ago
->get()
->map(function (Venue $v) {
$p = $v->toH5Payload();
if ($p === null) {
return null;
}
return [
'id' => $p['id'],
'name' => $p['name'],
'district' => $p['district'],
'address' => $p['address'],
'lat' => $p['lat'],
'lng' => $p['lng'],
'cover_image' => $p['cover_image'],
'open_time' => $p['open_time'],
'venue_type' => $p['venue_type'],
'venue_types' => is_array($p['venue_types'] ?? null) ? array_values($p['venue_types']) : [],
'ticket_type' => $p['ticket_type'],
'appointment_type' => $p['appointment_type'],
];
})
->filter()
->values();
6 days ago
return response()->json($rows);
}
public function venueDetail(int $id): JsonResponse
{
$v = Venue::query()->find($id);
if ($v === null) {
return response()->json(['message' => '场馆不存在'], 404);
}
5 days ago
$merged = $v->toH5Payload();
if ($merged === null) {
return response()->json(['message' => '场馆不存在'], 404);
6 days ago
}
5 days ago
$display = Venue::make($merged);
$display->id = $v->id;
$display->exists = true;
$payload = $display->toArray();
unset($payload['audit_status'], $payload['audit_remark'], $payload['last_approved_snapshot']);
$payload['carousel'] = $this->buildGalleryCarousel($display);
$payload['live_people_count'] = (int) ($display->live_people_count ?? 0);
$payload['venue_type_color'] = $this->resolveVenueTypeColor($display->venue_type, $display->venue_types);
5 days ago
$payload['activities'] = Activity::query()
->where('venue_id', $v->id)
5 days ago
->visibleOnH5()
5 days ago
->orderForH5Listing()
->limit(50)
->get(['id', 'title', 'summary', 'cover_image', 'start_at', 'end_at', 'registered_count', 'address'])
->map(function (Activity $a) {
return [
'id' => $a->id,
'title' => $a->title,
'summary' => $a->summary,
'cover_image' => $a->cover_image,
'start_at' => optional($a->start_at)?->toIso8601String(),
'end_at' => optional($a->end_at)?->toIso8601String(),
'registered_count' => (int) ($a->registered_count ?? 0),
'address' => $a->address,
];
})
->values()
->all();
6 days ago
return response()->json($payload);
}
/**
* 轮播素材gallery_media可含图片/视频);为空时用封面图兜底。
*
* @return array<int, array{type: string, url: string}>
*/
5 days ago
private function buildGalleryCarousel(Venue|Activity|StudyTour $model): array
6 days ago
{
$items = [];
$seen = [];
6 days ago
foreach ($model->gallery_media ?? [] as $m) {
6 days ago
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];
}
6 days ago
if ($items === [] && $model->cover_image) {
$url = trim((string) $model->cover_image);
6 days ago
if ($url !== '') {
$items[] = ['type' => 'image', 'url' => $url];
}
}
return $items;
}
5 days ago
/**
* 与首页地图场馆一致:字典 venue_type 的 item_remark 为色值。
*
* @param array<int, mixed>|null $venueTypes
*/
private function resolveVenueTypeColor(?string $venueType, $venueTypes = null): string
6 days ago
{
5 days ago
$first = $venueType;
if (is_array($venueTypes) && count($venueTypes)) {
$first = (string) ($venueTypes[0] ?? '');
}
if ($first === null || $first === '') {
6 days ago
return '#05c9ac';
}
$raw = DictItem::query()
->where('dict_type', 'venue_type')
->where('is_active', true)
5 days ago
->where('item_value', $first)
6 days ago
->value('item_remark');
$color = '#05c9ac';
if (is_string($raw) && trim($raw) !== '') {
$t = trim($raw);
if (! str_starts_with($t, '#')) {
$t = '#'.$t;
}
if (preg_match('/^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/', $t)) {
$color = $t;
}
}
return $color;
}
public function studyTourDetail(int $id): JsonResponse
{
$row = StudyTour::query()->where('is_active', true)->findOrFail($id);
$venueIds = collect($row->venue_ids ?? [])->filter()->values();
6 days ago
$venueMap = Venue::query()
6 days ago
->whereIn('id', $venueIds->all())
5 days ago
->get(['id', 'name', 'district', 'address', 'cover_image', 'lat', 'lng', 'ticket_type'])
6 days ago
->keyBy('id');
$venues = $venueIds->map(fn ($id) => $venueMap->get($id))->filter()->values();
6 days ago
return response()->json([
'id' => $row->id,
'name' => $row->name,
'tags' => array_values($row->tags ?? []),
5 days ago
'image' => $row->cover_image,
'gallery_media' => $row->gallery_media ?? [],
'carousel' => $this->buildGalleryCarousel($row),
6 days ago
'intro_html' => $row->intro_html,
'venues' => $venues,
]);
}
public function studyTours(): JsonResponse
{
$rows = StudyTour::query()
->where('is_active', true)
->orderBy('sort')
->orderByDesc('id')
5 days ago
->get(['id', 'name', 'tags', 'venue_ids', 'intro_html', 'cover_image', 'gallery_media']);
6 days ago
$venueIds = $rows->pluck('venue_ids')->flatten()->filter()->values()->all();
$venueMap = Venue::query()
->whereIn('id', $venueIds)
->get(['id', 'name', 'district', 'address', 'cover_image', 'lat', 'lng'])
->keyBy('id');
$payload = $rows->map(function ($row) use ($venueMap) {
$ids = collect($row->venue_ids ?? [])->filter()->values();
$firstVenue = $ids->isNotEmpty() ? $venueMap->get($ids->first()) : null;
6 days ago
$venueNames = $ids->map(fn ($id) => $venueMap->get($id)?->name)->filter()->values();
5 days ago
$fallbackCover = $ids->map(fn ($id) => $venueMap->get($id)?->cover_image)->filter()->first();
$tourCover = trim((string) ($row->cover_image ?? ''));
6 days ago
return [
'id' => $row->id,
'name' => $row->name,
'tags' => array_values($row->tags ?? []),
6 days ago
'venue_names' => $venueNames->all(),
5 days ago
'cover_image' => $tourCover !== '' ? $tourCover : ($fallbackCover ?: $firstVenue?->cover_image),
6 days ago
'first_address' => $firstVenue?->address,
'first_district' => $firstVenue?->district,
'venue_count' => $ids->count(),
];
})->values();
return response()->json($payload);
}
6 days ago
public function venueDicts(): JsonResponse
{
return response()->json([
'district' => DictItem::activeOptions('district'),
'venue_type' => DictItem::activeOptions('venue_type'),
5 days ago
'venue_appointment_type' => DictItem::activeOptions('venue_appointment_type'),
6 days ago
'ticket_type' => DictItem::activeOptions('ticket_type'),
]);
}
6 days ago
}