Merge branch 'codex/match-live-streams' into dev.1.0.0

This commit is contained in:
hajimi
2026-06-21 16:50:16 +08:00
23 changed files with 2516 additions and 2 deletions
@@ -0,0 +1,53 @@
<?php
namespace app\adminapi\controller\match;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\match\MatchLiveLists;
use app\adminapi\logic\match\MatchLiveLogic;
use app\adminapi\validate\match\MatchLiveValidate;
class MatchLiveController extends BaseAdminController
{
public function lists()
{
return $this->dataLists(new MatchLiveLists());
}
public function detail()
{
$params = (new MatchLiveValidate())->goCheck('detail');
$result = MatchLiveLogic::detail($params);
return $this->data($result);
}
public function add()
{
$params = (new MatchLiveValidate())->post()->goCheck('add');
$result = MatchLiveLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(MatchLiveLogic::getError());
}
public function edit()
{
$params = (new MatchLiveValidate())->post()->goCheck('edit');
$result = MatchLiveLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(MatchLiveLogic::getError());
}
public function delete()
{
$params = (new MatchLiveValidate())->post()->goCheck('delete');
$result = MatchLiveLogic::delete($params);
if (true === $result) {
return $this->success('删除成功', [], 1, 1);
}
return $this->fail(MatchLiveLogic::getError());
}
}
@@ -0,0 +1,63 @@
<?php
namespace app\adminapi\lists\match;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\match\MatchLive;
class MatchLiveLists extends BaseAdminDataLists implements ListsSearchInterface
{
public function setSearch(): array
{
return [
'=' => ['match_id', 'fetch_status'],
];
}
public function lists(): array
{
$lists = $this->buildQuery()
->field('id,match_id,title,source_url,play_url_m3u8,play_url_hd_m3u8,play_url_flv,play_url_hd_flv,fetch_status,fetch_error,last_fetch_at,next_fetch_at,create_time,update_time')
->limit($this->limitOffset, $this->limitLength)
->order('create_time', 'asc')
->order('id', 'asc')
->select()
->toArray();
foreach ($lists as &$item) {
$item['fetch_status_text'] = self::formatFetchStatus((int) ($item['fetch_status'] ?? 0));
}
unset($item);
return $lists;
}
public function count(): int
{
return $this->buildQuery()->count();
}
public static function formatFetchStatus(int $status): string
{
return match ($status) {
0 => '待抓取',
1 => '抓取成功',
2 => '抓取失败',
default => '未知',
};
}
protected function buildQuery()
{
$query = MatchLive::whereNull('delete_time')
->where($this->searchWhere);
$matchId = (int) ($this->params['match_id'] ?? 0);
if ($matchId <= 0) {
$query->where('match_id', 0);
}
return $query;
}
}
@@ -0,0 +1,104 @@
<?php
namespace app\adminapi\logic\match;
use app\common\logic\BaseLogic;
use app\common\model\match\MatchLive;
use app\common\service\match\MatchLiveService;
class MatchLiveLogic extends BaseLogic
{
public static function detail(array $params): array
{
return MatchLive::findOrEmpty($params['id'])->toArray();
}
public static function add(array $params): bool
{
try {
$now = time();
MatchLive::create([
'match_id' => (int) $params['match_id'],
'title' => trim((string) $params['title']),
'source_url' => trim((string) $params['source_url']),
'fetch_status' => 0,
'fetch_error' => '',
'last_fetch_at' => 0,
'next_fetch_at' => $now,
'create_time' => $now,
'update_time' => $now,
]);
MatchLiveService::syncLegacyLiveUrl((int) $params['match_id']);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function edit(array $params): bool
{
try {
$live = MatchLive::findOrEmpty($params['id']);
if ($live->isEmpty()) {
self::setError('直播线路不存在');
return false;
}
$oldMatchId = (int) $live->match_id;
$newMatchId = (int) $params['match_id'];
$now = time();
$sourceUrl = trim((string) $params['source_url']);
$sourceUrlChanged = $sourceUrl !== trim((string) $live->source_url);
$saveData = [
'match_id' => $newMatchId,
'title' => trim((string) $params['title']),
'source_url' => $sourceUrl,
'update_time' => $now,
];
if ($sourceUrlChanged) {
$saveData['play_url_m3u8'] = '';
$saveData['play_url_hd_m3u8'] = '';
$saveData['play_url_flv'] = '';
$saveData['play_url_hd_flv'] = '';
$saveData['fetch_status'] = 0;
$saveData['fetch_error'] = '';
$saveData['last_fetch_at'] = 0;
$saveData['next_fetch_at'] = $now;
}
$live->save($saveData);
if ($oldMatchId > 0 && $oldMatchId !== $newMatchId) {
MatchLiveService::syncLegacyLiveUrl($oldMatchId);
}
MatchLiveService::syncLegacyLiveUrl($newMatchId);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function delete(array $params): bool
{
try {
$live = MatchLive::findOrEmpty($params['id']);
if ($live->isEmpty()) {
self::setError('直播线路不存在');
return false;
}
$matchId = (int) $live->match_id;
$live->delete();
MatchLiveService::syncLegacyLiveUrl($matchId);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}
@@ -24,7 +24,6 @@ class MatchLogic extends BaseLogic
'league_name',
'round_name',
'stage',
'live_url',
'home_team',
'home_icon',
'home_score',
@@ -0,0 +1,99 @@
<?php
namespace app\adminapi\validate\match;
use app\common\model\match\MatchEvent;
use app\common\model\match\MatchLive;
use app\common\validate\BaseValidate;
class MatchLiveValidate extends BaseValidate
{
protected $rule = [
'id' => 'require|integer|gt:0|checkLive',
'match_id' => 'require|integer|gt:0|checkMatch',
'title' => 'require|checkTrimmedTitle',
'source_url' => 'require|checkTrimmedSourceUrl',
];
protected $message = [
'id.require' => '直播线路id不能为空',
'id.integer' => '直播线路id须为整数',
'id.gt' => '直播线路id须大于0',
'match_id.require' => '赛事id不能为空',
'match_id.integer' => '赛事id须为整数',
'match_id.gt' => '赛事id须大于0',
'title.require' => '直播标题不能为空',
'title.length' => '直播标题长度须在1-255位字符',
'source_url.require' => '源地址不能为空',
'source_url.max' => '源地址长度不能超过500位字符',
];
public function sceneDetail()
{
return $this->only(['id']);
}
public function sceneAdd()
{
return $this->remove('id', 'require|integer|gt:0|checkLive')
->only(['match_id', 'title', 'source_url']);
}
public function sceneEdit()
{
return $this->only(['id', 'match_id', 'title', 'source_url']);
}
public function sceneDelete()
{
return $this->only(['id']);
}
public function checkLive($value)
{
$live = MatchLive::findOrEmpty($value);
if ($live->isEmpty()) {
return '直播线路不存在';
}
return true;
}
public function checkMatch($value)
{
$match = MatchEvent::findOrEmpty($value);
if ($match->isEmpty()) {
return '赛事不存在';
}
return true;
}
public function checkTrimmedTitle($value)
{
if (!is_scalar($value)) {
return '直播标题格式错误';
}
$value = trim((string) $value);
if ($value === '') {
return '直播标题不能为空';
}
if (mb_strlen($value) > 255) {
return '直播标题长度须在1-255位字符';
}
return true;
}
public function checkTrimmedSourceUrl($value)
{
if (!is_scalar($value)) {
return '源地址格式错误';
}
$value = trim((string) $value);
if ($value === '') {
return '源地址不能为空';
}
if (mb_strlen($value) > 500) {
return '源地址长度不能超过500位字符';
}
return true;
}
}
@@ -13,6 +13,7 @@ use app\common\model\match\MatchLineup;
use app\common\model\match\WorldCupPersonRanking;
use app\common\model\match\WorldCupStanding;
use app\common\model\league\League;
use app\common\service\match\MatchLiveService;
class MatchController extends BaseApiController
{
@@ -157,6 +158,10 @@ class MatchController extends BaseApiController
return $this->fail('赛事不存在');
}
$data = $match->toArray();
$data['live_list'] = MatchLiveService::getLiveListForApi((int) $data['id']);
if (empty($data['live_url']) && !empty($data['live_list'][0]['source_url'])) {
$data['live_url'] = (string) $data['live_list'][0]['source_url'];
}
if (empty($data['live_url']) && $this->isWorldCupMatch($data)) {
$data['live_url'] = $this->worldCupDefaultLiveUrl;
}
@@ -0,0 +1,14 @@
<?php
namespace app\common\model\match;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
class MatchLive extends BaseModel
{
use SoftDelete;
protected $name = 'match_live';
protected $deleteTime = 'delete_time';
}
@@ -0,0 +1,65 @@
<?php
namespace app\common\service\match;
use app\common\model\match\MatchEvent;
use app\common\model\match\MatchLive;
class MatchLiveService
{
public static function syncLegacyLiveUrl(int $matchId): void
{
$row = MatchLive::where('match_id', $matchId)
->whereNull('delete_time')
->order('create_time', 'asc')
->order('id', 'asc')
->find();
MatchEvent::where('id', $matchId)->update([
'live_url' => $row ? (string) $row->source_url : '',
'update_time' => time(),
]);
}
public static function getLiveListForApi(int $matchId): array
{
$rows = MatchLive::where('match_id', $matchId)
->whereNull('delete_time')
->order('create_time', 'asc')
->order('id', 'asc')
->select()
->toArray();
foreach ($rows as &$row) {
$row['stream_options'] = self::buildStreamOptions($row);
$row['default_play_url'] = $row['stream_options'][0]['url'] ?? '';
}
unset($row);
return $rows;
}
public static function buildStreamOptions(array $row): array
{
$definitions = [
['label' => 'M3U8', 'field' => 'play_url_m3u8'],
['label' => 'HD M3U8', 'field' => 'play_url_hd_m3u8'],
['label' => 'FLV', 'field' => 'play_url_flv'],
['label' => 'HD FLV', 'field' => 'play_url_hd_flv'],
];
$options = [];
foreach ($definitions as $definition) {
$url = trim((string) ($row[$definition['field']] ?? ''));
if ($url === '') {
continue;
}
$options[] = [
'label' => $definition['label'],
'url' => $url,
];
}
return $options;
}
}