feat: wire match live backend api

This commit is contained in:
hajimi
2026-06-21 11:52:02 +08:00
parent 8d528ba5fe
commit 80e0e6a8a6
7 changed files with 329 additions and 1 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,97 @@
<?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();
$live->save([
'match_id' => $newMatchId,
'title' => trim((string) $params['title']),
'source_url' => trim((string) $params['source_url']),
'play_url_m3u8' => '',
'play_url_hd_m3u8' => '',
'play_url_flv' => '',
'play_url_hd_flv' => '',
'fetch_status' => 0,
'fetch_error' => '',
'last_fetch_at' => 0,
'next_fetch_at' => $now,
'update_time' => $now,
]);
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,69 @@
<?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|length:1,255',
'source_url' => 'require|max:500',
];
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;
}
}