feat: match detail live room matching

This commit is contained in:
hajimi
2026-08-03 02:47:57 +08:00
parent 369ee15044
commit 56b34414ac
5 changed files with 259 additions and 16 deletions
@@ -281,7 +281,7 @@ class MatchController extends BaseApiController
return $this->fail('赛事不存在');
}
$data = $match->toArray();
$data['live_list'] = MatchLiveService::getLiveListForApi((int) $data['id']);
$data['live_list'] = MatchLiveService::getMatchedLiveListForApi($data);
if (empty($data['live_url']) && !empty($data['live_list'][0]['source_url'])) {
$data['live_url'] = (string) $data['live_list'][0]['source_url'];
}
@@ -28,6 +28,47 @@ class MatchLiveService
return $liveMap[$matchId] ?? [];
}
/**
* 获取赛事可用的直播间:优先返回已绑定赛事,未绑定的房间则按标题中的主客队名称补充匹配。
*/
public static function getMatchedLiveListForApi(array $match): array
{
$matchId = (int) ($match['id'] ?? 0);
$liveList = $matchId > 0 ? self::getLiveListForApi($matchId) : [];
$knownLiveIds = array_flip(array_filter(array_map(
static fn(array $item): int => (int) ($item['id'] ?? 0),
$liveList
)));
$homeTeam = trim((string) ($match['home_team'] ?? ''));
$awayTeam = trim((string) ($match['away_team'] ?? ''));
if ($homeTeam === '' || $awayTeam === '') {
return $liveList;
}
$homeKeyword = '%' . addcslashes($homeTeam, '\\%_') . '%';
$awayKeyword = '%' . addcslashes($awayTeam, '\\%_') . '%';
$matchedRows = MatchLive::where('fetch_status', 1)
->whereNull('delete_time')
->where('title', 'like', $homeKeyword)
->where('title', 'like', $awayKeyword)
->order('create_time', 'asc')
->order('id', 'asc')
->select()
->toArray();
foreach ($matchedRows as $row) {
$liveId = (int) ($row['id'] ?? 0);
if ($liveId <= 0 || isset($knownLiveIds[$liveId])) {
continue;
}
$liveList[] = self::decorateLiveRow($row);
$knownLiveIds[$liveId] = true;
}
return $liveList;
}
public static function getLiveMapForApi(array $matchIds): array
{
$matchIds = array_values(array_unique(array_filter(array_map('intval', $matchIds))));
@@ -44,8 +85,7 @@ class MatchLiveService
$result = [];
foreach ($rows as &$row) {
$row['stream_options'] = self::buildStreamOptions($row);
$row['default_play_url'] = self::resolveDefaultPlayUrl($row);
$row = self::decorateLiveRow($row);
$matchId = (int) ($row['match_id'] ?? 0);
if ($matchId <= 0) {
continue;
@@ -57,6 +97,14 @@ class MatchLiveService
return $result;
}
protected static function decorateLiveRow(array $row): array
{
$row['stream_options'] = self::buildStreamOptions($row);
$row['default_play_url'] = self::resolveDefaultPlayUrl($row);
return $row;
}
public static function buildStreamOptions(array $row): array
{
$definitions = [