Files
pk10/App/Controllers/Web/HomeController.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

193 lines
7.2 KiB
PHP
Executable File

<?php
namespace App\Controllers\Web;
use App\Core\WebBaseController;
use App\Core\I18n;
use Db\Database;
class HomeController extends WebBaseController {
public function index() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$userId = $this->getCurrentUserId();
$user = $db->get('users', ['id','username','balance','lang'], ['id' => $userId]);
$game = $db->get('games', '*', ['code' => 'pk10', 'status' => 1]);
extract([
'user' => $user ?: ['id' => $userId, 'username' => $this->getCurrentUsername(), 'balance' => 0],
'game' => $game,
]);
include __DIR__ . '/../../Views/Web/home.php';
}
public function pk10Game() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$userId = $this->getCurrentUserId();
$user = $db->get('users', ['id','username','balance','lang'], ['id' => $userId]);
$game = $db->get('games', '*', ['code' => 'pk10', 'status' => 1]);
$isAgent = !!$db->get('agents', 'id', ['user_id' => $userId, 'status' => 1]);
// 加载赔率(优先代理专属赔率,否则用游戏默认赔率)
$gameId = $game['id'] ?? 0;
$oddsRaw = $db->select('game_odds', '*', ['game_id' => $gameId]);
$oddsMap = [];
foreach ($oddsRaw as $o) {
$oddsMap[$o['type'] . '_' . $o['target']] = (float)$o['odds'];
}
// 代理专属赔率覆盖
$agentId = $db->get('agents', 'id', ['user_id' => $userId, 'status' => 1]);
if ($agentId) {
$agentOdds = $db->select('agent_odds', '*', ['agent_id' => $agentId, 'game_id' => $gameId]);
foreach ($agentOdds as $ao) {
$oddsMap[$ao['bet_type'] . '_' . $ao['bet_target']] = (float)$ao['odds'];
}
}
extract([
'user' => $user ?: ['id' => $userId, 'username' => $this->getCurrentUsername(), 'balance' => 0],
'game' => $game,
'isAgent' => !!$agentId,
'oddsMap' => $oddsMap,
]);
include __DIR__ . '/../../Views/Web/pk10.php';
}
public function profile() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$userId = $this->getCurrentUserId();
$user = $db->get('users', '*', ['id' => $userId]);
// 最近交易
$transactions = $db->select('transactions', '*', [
'user_id' => $userId, 'is_virtual' => 0,
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 20
]);
// 最近投注
$bets = $db->select('bets', '*', [
'user_id' => $userId,
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 20
]);
// 充提记录
$fundRequests = $db->select('fund_requests', '*', [
'user_id' => $userId,
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 20
]);
extract(compact('user', 'transactions', 'bets', 'fundRequests'));
include __DIR__ . '/../../Views/Web/profile.php';
}
public function bindUsdt() {
$this->checkWebLogin();
header('Content-Type: application/json');
$db = new Database();
$userId = $this->getCurrentUserId();
$data = json_decode(file_get_contents('php://input'), true);
$address = trim($data['usdt_address'] ?? '');
// TRC20地址验证: 以T开头, 34字符, base58
if (!preg_match('/^T[1-9A-HJ-NP-Za-km-z]{33}$/', $address)) {
echo json_encode(['success' => false, 'message' => 'Invalid TRC20 address']);
return;
}
// 唯一性
$exists = $db->get('users', 'id', ['usdt_address' => $address, 'id[!]' => $userId]);
if ($exists) {
echo json_encode(['success' => false, 'message' => 'Address already bound to another account']);
return;
}
$db->update('users', ['usdt_address' => $address, 'usdt_chain' => 'TRC20'], ['id' => $userId]);
echo json_encode(['success' => true, 'message' => 'Address bound successfully']);
}
public function setLang() {
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$lang = $data['lang'] ?? 'en';
if (isset(I18n::LANGUAGES[$lang])) {
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + 86400 * 365, '/');
$db = new Database();
$userId = $this->getCurrentUserId();
if ($userId) $db->update('users', ['lang' => $lang], ['id' => $userId]);
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => 'Invalid language']);
}
}
public function lottery() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$gameCode = $_GET['game'] ?? 'pk10';
$game = $db->get('games', '*', ['code' => $gameCode, 'status' => 1]);
if (!$game) $game = $db->get('games', '*', ['code' => 'pk10', 'status' => 1]);
$gameId = $game['id'] ?? 0;
$gameType = $game['type'] ?? 'pk10';
$algoClass = \App\Core\GameFactory::getAlgorithm($gameType);
$resultTable = $algoClass::getResultTable();
$periods = $db->select('periods', '*', [
'game_id' => $gameId, 'status' => 'settled',
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 50
]);
foreach ($periods as &$p) {
if ($resultTable) {
$pk = $db->get($resultTable, '*', ['period_id' => $p['id']]);
if ($pk) { foreach ($pk as $k => $v) $p[$k] = $v; }
}
}
unset($p);
extract(compact('game', 'periods'));
include __DIR__ . '/../../Views/Web/lottery.php';
}
public function pk10Stats() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$game = $db->get('games', '*', ['code' => 'pk10', 'status' => 1]);
$gameId = $game['id'] ?? 0;
// 最近100期已结算结果
$periods = $db->select('periods', ['id','period_number'], [
'game_id' => $gameId, 'status' => 'settled',
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 100
]);
$results = [];
foreach ($periods as $p) {
$pk = $db->get('pk10_results', '*', ['period_id' => $p['id']]);
if ($pk) {
$pk['period_number'] = $p['period_number'];
$results[] = $pk;
}
}
extract(compact('game', 'results'));
include __DIR__ . '/../../Views/Web/pk10_stats.php';
}
public function details() {
$this->checkWebLogin();
I18n::init();
$db = new Database();
$userId = $this->getCurrentUserId();
$settled = $db->select('bets', '*', [
'user_id' => $userId, 'status[!]' => 'pending',
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 50
]);
$pending = $db->select('bets', '*', [
'user_id' => $userId, 'status' => 'pending',
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 50
]);
extract(compact('settled', 'pending'));
include __DIR__ . '/../../Views/Web/details.php';
}
}