Files
pk10/App/Controllers/Web/PeriodController.php
T
li 4a94fe36cf feat: 重构代码架构 + 新增报表/跟单/输赢统计功能
- 拆分 HomeController → TransactionController, ReportWebController, FollowPlanController
- 新增 Service 层: TransactionService, ReportService, FollowPlanService
- pk10.php JS 抽离为 4 个独立文件 (sound/race/bet/poll)
- 前台新增报表查询页面 (/report) + 跟单计划页面 (/follow-plan)
- 后台新增跟单计划管理 + 用户输赢明细统计
- 封盘状态显示倒计时 (x:xx)
- 音效仅在开奖弹窗打开时播放
- 路由按模块分组整理
- autoload 支持 App\Services 命名空间
2026-03-27 18:41:06 +08:00

147 lines
5.3 KiB
PHP
Executable File

<?php
namespace App\Controllers\Web;
use App\Core\WebBaseController;
use App\Core\GameFactory;
use Db\Database;
class PeriodController extends WebBaseController {
public function getCurrent() {
header('Content-Type: application/json');
$db = new Database();
$gameId = (int)($_GET['game_id'] ?? 0);
if (!$gameId) {
$gameId = (int)($db->get('games', 'id', ['code' => 'pk10']) ?: 0);
}
if (!$gameId) {
echo json_encode(['success' => false, 'message' => 'Game not found']);
return;
}
// 获取游戏类型和算法
$game = $db->get('games', ['id', 'type', 'period_duration', 'lock_before_end'], ['id' => $gameId]);
$gameType = $game['type'] ?? 'pk10';
$algoClass = GameFactory::getAlgorithm($gameType);
$resultTable = $algoClass::getResultTable();
$period = $db->get('periods', '*', [
'game_id' => $gameId,
'ORDER' => ['id' => 'DESC']
]);
$now = time();
$countdown = 0;
$lockCountdown = 0;
if ($period && in_array($period['status'], ['pending', 'locked'])) {
$start = strtotime($period['start_time'] ?? $period['created_at']);
$countdown = max(0, !empty($period['end_time']) ? strtotime($period['end_time']) - $now : 300 - ($now - $start));
$periodDuration = (int)($game['period_duration'] ?? 300);
$lockBefore = (int)($game['lock_before_end'] ?? 30);
$lockCountdown = max(0, ($periodDuration - $lockBefore) - ($now - $start));
}
// 获取结果的通用方法
$getResult = function($periodRow) use ($db, $resultTable, $algoClass) {
if (!$periodRow) return null;
if ($resultTable) {
return $db->get($resultTable, '*', ['period_id' => $periodRow['id']]);
}
// dice/xocdia: 结果在 periods 表中
return $periodRow;
};
// ====== 最近开奖结果 ======
$lastResult = null;
$lastResultStatus = null;
$lastDrawn = $db->get('periods', '*', [
'game_id' => $gameId,
'status' => ['drawn', 'settled'],
'ORDER' => ['id' => 'DESC']
]);
if ($lastDrawn) {
$pk10 = $getResult($lastDrawn);
if ($pk10) {
$lastResult = $pk10;
$lastResult['period_number'] = $lastDrawn['period_number'];
$lastResultStatus = $lastDrawn['status'];
}
}
// ====== locked 状态预开奖结果 ======
$raceResult = null;
if ($period && $period['status'] === 'locked' && !empty($period['result'])) {
$pk10Current = $getResult($period);
if ($pk10Current) {
$raceResult = $pk10Current;
$raceResult['period_number'] = $period['period_number'];
}
}
// history
$history = [];
$recentPeriods = $db->select('periods', ['id','period_number','result','status'], [
'game_id' => $gameId,
'status' => ['drawn', 'settled'],
'ORDER' => ['id' => 'DESC'],
'LIMIT' => 30
]);
foreach ($recentPeriods as $s) {
$pk = $getResult($s);
if ($pk) { $pk['period_number'] = $s['period_number']; $history[] = $pk; }
}
// user bets + balance
$myBets = [];
$balance = null;
$userId = $this->getWebUserId();
if ($userId && $period) {
$betsRaw = $db->select('bets', ['id', 'bet_type', 'bet_value', 'amount', 'odds', 'status', 'win_amount'], [
'user_id' => $userId,
'period_id' => $period['id'],
'ORDER' => ['id' => 'DESC']
]);
$myBets = $betsRaw ?: [];
$userRow = $db->get('users', ['balance'], ['id' => $userId]);
$balance = $userRow ? (float)$userRow['balance'] : null;
}
$lastMyBets = [];
if ($userId && $lastDrawn && $lastDrawn['status'] === 'settled') {
$lastBetsRaw = $db->select('bets', ['id', 'bet_type', 'bet_value', 'amount', 'odds', 'status', 'win_amount'], [
'user_id' => $userId,
'period_id' => $lastDrawn['id'],
'ORDER' => ['id' => 'DESC']
]);
$lastMyBets = $lastBetsRaw ?: [];
}
echo json_encode([
'success' => true,
'data' => [
'id' => $period['id'] ?? 0,
'period_number' => $period['period_number'] ?? '',
'status' => $period['status'] ?? 'none',
'remaining_seconds' => $countdown,
'lock_countdown' => $lockCountdown,
'auto_generated' => (int)($period['auto_generated'] ?? 0),
'server_time' => date('Y-m-d H:i:s'),
],
'last_result' => $lastResult,
'last_result_status' => $lastResultStatus,
'race_result' => $raceResult,
'history' => $history,
'my_bets' => $myBets,
'last_my_bets' => $lastMyBets,
'balance' => $balance,
]);
}
private function getWebUserId(): int {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
return (int)($_SESSION['web_user_id'] ?? 0);
}
}