Files
pk10/App/Controllers/Admin/AutoPeriodController.php
T

175 lines
5.1 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Core\AdminBaseController;
use Db\Database;
/**
* 自动开期设置控制器
*/
class AutoPeriodController extends AdminBaseController {
private $db;
public function __construct(Database $db) {
$this->db = $db;
}
/**
* 自动开期设置页面
*/
public function index() {
$this->checkLogin();
$this->checkAdmin();
// 获取所有游戏
$games = $this->db->select('games', '*', [
'status' => 1,
'ORDER' => ['id' => 'ASC']
]);
// 获取最近的自动开期日志
$logs = [];
try {
$logs = $this->db->select('auto_period_log', '*', [
'ORDER' => ['id' => 'DESC'],
'LIMIT' => 50
]);
} catch (\Throwable $e) {
// 表可能不存在
}
$this->render('Admin/auto_period.php', compact('games', 'logs'));
}
/**
* 更新游戏的自动开期设置
*/
public function update() {
$this->checkLogin();
$this->checkAdmin();
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
if (empty($data)) $data = $_POST;
$gameId = (int)($data['game_id'] ?? 0);
if ($gameId <= 0) {
echo json_encode(['success' => false, 'message' => '无效的游戏ID']);
return;
}
$game = $this->db->get('games', '*', ['id' => $gameId]);
if (!$game) {
echo json_encode(['success' => false, 'message' => '游戏不存在']);
return;
}
$updateData = [];
// 自动开期开关
if (isset($data['auto_period_enabled'])) {
$updateData['auto_period_enabled'] = (int)$data['auto_period_enabled'] ? 1 : 0;
}
// 每期时长(秒)
if (isset($data['period_duration'])) {
$duration = (int)$data['period_duration'];
if ($duration < 60) $duration = 60; // 最少1分钟
if ($duration > 3600) $duration = 3600; // 最多1小时
$updateData['period_duration'] = $duration;
}
// 封盘提前时间(秒)
if (isset($data['lock_before_end'])) {
$lockTime = (int)$data['lock_before_end'];
if ($lockTime < 5) $lockTime = 5;
if ($lockTime > 120) $lockTime = 120;
$updateData['lock_before_end'] = $lockTime;
}
if (empty($updateData)) {
echo json_encode(['success' => false, 'message' => '无更新内容']);
return;
}
$updateData['updated_at'] = date('Y-m-d H:i:s');
try {
$this->db->update('games', $updateData, ['id' => $gameId]);
echo json_encode([
'success' => true,
'message' => '设置已保存',
'data' => $updateData
]);
} catch (\Throwable $e) {
echo json_encode(['success' => false, 'message' => '保存失败: ' . $e->getMessage()]);
}
}
/**
* 批量启用/禁用
*/
public function toggleAll() {
$this->checkLogin();
$this->checkAdmin();
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$enabled = (int)($data['enabled'] ?? 0);
try {
$this->db->update('games', [
'auto_period_enabled' => $enabled,
'updated_at' => date('Y-m-d H:i:s')
], ['status' => 1]);
echo json_encode([
'success' => true,
'message' => $enabled ? '已全部启用自动开期' : '已全部关闭自动开期'
]);
} catch (\Throwable $e) {
echo json_encode(['success' => false, 'message' => '操作失败: ' . $e->getMessage()]);
}
}
/**
* 获取自动开期运行状态
*/
public function status() {
$this->checkLogin();
header('Content-Type: application/json');
$games = $this->db->select('games', ['id', 'name', 'type', 'auto_period_enabled', 'period_duration', 'lock_before_end'], [
'status' => 1,
'ORDER' => ['id' => 'ASC']
]);
// 每个游戏的当前期号
foreach ($games as &$g) {
$g['current_period'] = $this->db->get('periods', ['id', 'period_number', 'status', 'start_time', 'end_time'], [
'game_id' => $g['id'],
'status' => ['pending', 'locked', 'drawn'],
'ORDER' => ['id' => 'DESC']
]);
}
// 最近日志
$recentLogs = [];
try {
$recentLogs = $this->db->select('auto_period_log', '*', [
'ORDER' => ['id' => 'DESC'],
'LIMIT' => 10
]);
} catch (\Throwable $e) {}
echo json_encode([
'success' => true,
'data' => [
'games' => $games,
'logs' => $recentLogs,
'server_time' => date('Y-m-d H:i:s'),
]
]);
}
}