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

180 lines
6.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Controllers\Web;
use App\Core\WebBaseController;
use App\Core\I18n;
use Db\Database;
class AgentController extends WebBaseController {
private function getAgent() {
$db = new Database();
$agent = $db->get('agents', '*', ['user_id' => $this->getCurrentUserId(), 'status' => 1]);
if (!$agent) { header('Location: /'); exit; }
return $agent;
}
// 代理后台主页
public function dashboard() {
$this->checkWebLogin();
$agent = $this->getAgent();
I18n::init();
$db = new Database();
$user = $db->get('users', '*', ['id' => $this->getCurrentUserId()]);
$players = $db->select('users', ['id','username','balance','created_at','status'], [
'agent_id' => $agent['id'], 'is_virtual' => 0, 'ORDER' => ['id' => 'DESC']
]);
$today = date('Y-m-d');
$todayRange = [$today . ' 00:00:00', $today . ' 23:59:59'];
$todayComm = $db->sum('agent_commissions', 'commission', [
'agent_id' => $agent['id'], 'created_at[<>]' => $todayRange
]) ?: 0;
$totalComm = $db->sum('agent_commissions', 'commission', ['agent_id' => $agent['id']]) ?: 0;
// 玩家总投注额
$totalBets = $db->sum('bets', 'amount', ['agent_id' => $agent['id'], 'is_virtual' => 0]) ?: 0;
// 下级代理
$subAgents = [];
if ($agent['level'] == 1) {
$subAgents = $db->select('agents', '*', ['parent_id' => $agent['id']]);
foreach ($subAgents as &$sa) {
$sa['user'] = $db->get('users', ['username','balance'], ['id' => $sa['user_id']]);
$sa['player_count'] = $db->count('users', ['agent_id' => $sa['id']]);
}
}
$page = 'home';
extract(compact('agent','user','players','todayComm','totalComm','totalBets','subAgents','page'));
include __DIR__ . '/../../Views/Web/agent_dashboard.php';
}
// 赔率管理页
public function odds() {
$this->checkWebLogin();
$agent = $this->getAgent();
I18n::init();
$db = new Database();
$user = $db->get('users', '*', ['id' => $this->getCurrentUserId()]);
$game = $db->get('games', '*', ['code' => 'pk10']);
// 系统赔率
$sysOdds = $db->select('game_odds', '*', ['game_id' => $game['id']]);
$sysMap = [];
foreach ($sysOdds as $s) $sysMap[$s['type'].'_'.$s['target']] = (float)$s['odds'];
// 上级赔率
$parentMap = $sysMap;
if ($agent['parent_id']) {
$po = $db->select('agent_odds', '*', ['agent_id' => $agent['parent_id'], 'game_id' => $game['id']]);
foreach ($po as $p) $parentMap[$p['bet_type'].'_'.$p['bet_target']] = (float)$p['odds'];
}
// 当前代理赔率
$myOdds = $db->select('agent_odds', '*', ['agent_id' => $agent['id'], 'game_id' => $game['id']]);
$myMap = [];
foreach ($myOdds as $m) $myMap[$m['bet_type'].'_'.$m['bet_target']] = (float)$m['odds'];
$page = 'odds';
extract(compact('agent','user','game','sysMap','parentMap','myMap','page'));
include __DIR__ . '/../../Views/Web/agent_dashboard.php';
}
// 保存赔率
public function setOdds() {
$this->checkWebLogin();
$agent = $this->getAgent();
header('Content-Type: application/json');
$db = new Database();
$data = json_decode(file_get_contents('php://input'), true);
$gameId = (int)($data['game_id'] ?? 0);
// 上级赔率限制
$parentOdds = [];
if ($agent['parent_id']) {
$po = $db->select('agent_odds', '*', ['agent_id' => $agent['parent_id'], 'game_id' => $gameId]);
foreach ($po as $p) $parentOdds[$p['bet_type'].'_'.$p['bet_target']] = (float)$p['odds'];
}
if (empty($parentOdds)) {
$so = $db->select('game_odds', '*', ['game_id' => $gameId]);
foreach ($so as $s) $parentOdds[$s['type'].'_'.$s['target']] = (float)$s['odds'];
}
foreach ($data['odds'] ?? [] as $o) {
$key = $o['bet_type'].'_'.$o['bet_target'];
$newOdds = floatval($o['odds']);
if (isset($parentOdds[$key]) && $newOdds > $parentOdds[$key]) {
echo json_encode(['success'=>false,'message'=>"赔率不能超过上级: $key 上限 ".$parentOdds[$key]]);
return;
}
$existing = $db->get('agent_odds', 'id', [
'agent_id'=>$agent['id'],'game_id'=>$gameId,'bet_type'=>$o['bet_type'],'bet_target'=>$o['bet_target']
]);
if ($existing) {
$db->update('agent_odds', ['odds'=>$newOdds], ['id'=>$existing]);
} else {
$db->insert('agent_odds', [
'agent_id'=>$agent['id'],'game_id'=>$gameId,
'bet_type'=>$o['bet_type'],'bet_target'=>$o['bet_target'],'odds'=>$newOdds
]);
}
}
echo json_encode(['success' => true, 'message' => '赔率已更新']);
}
// 玩家投注记录
public function bets() {
$this->checkWebLogin();
$agent = $this->getAgent();
I18n::init();
$db = new Database();
$user = $db->get('users', '*', ['id' => $this->getCurrentUserId()]);
$betRecords = $db->select('bets', '*', [
'agent_id' => $agent['id'], 'is_virtual' => 0,
'ORDER' => ['id' => 'DESC'], 'LIMIT' => 200
]);
foreach ($betRecords as &$b) {
$b['username'] = $db->get('users', 'username', ['id' => $b['user_id']]) ?: '?';
}
$page = 'bets';
extract(compact('agent','user','betRecords','page'));
include __DIR__ . '/../../Views/Web/agent_dashboard.php';
}
// 佣金明细
public function commissions() {
$this->checkWebLogin();
$agent = $this->getAgent();
I18n::init();
$db = new Database();
$user = $db->get('users', '*', ['id' => $this->getCurrentUserId()]);
$records = $db->select('agent_commissions', '*', [
'agent_id' => $agent['id'], 'ORDER' => ['id' => 'DESC'], 'LIMIT' => 200
]);
foreach ($records as &$r) {
$r['username'] = $db->get('users', 'username', ['id' => $r['from_user_id']]) ?: '?';
}
$page = 'commissions';
extract(compact('agent','user','records','page'));
include __DIR__ . '/../../Views/Web/agent_dashboard.php';
}
// 佣金APIJSON
public function commissionsApi() {
$this->checkWebLogin();
$agent = $this->getAgent();
header('Content-Type: application/json');
$db = new Database();
$records = $db->select('agent_commissions', '*', [
'agent_id' => $agent['id'], 'ORDER' => ['id' => 'DESC'], 'LIMIT' => 100
]);
echo json_encode(['success' => true, 'data' => $records]);
}
}