Files
pk10/App/Controllers/Web/HomeController.php
T

171 lines
6.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]);
extract([
'user' => $user ?: ['id' => $userId, 'username' => $this->getCurrentUsername(), 'balance' => 0],
'game' => $game,
'isAgent' => $isAgent,
]);
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
]);
extract(compact('user', 'transactions', 'bets'));
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';
}
}