105 lines
4.2 KiB
PHP
105 lines
4.2 KiB
PHP
<?php
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Core\AdminBaseController;
|
|
use Db\Database;
|
|
|
|
class AgentController extends AdminBaseController {
|
|
private $db;
|
|
public function __construct(Database $db) { $this->db = $db; }
|
|
|
|
// 后台代理管理
|
|
public function index() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$agents = $this->db->select('agents', '*', ['ORDER' => ['id' => 'DESC']]);
|
|
foreach ($agents as &$a) {
|
|
$a['user'] = $this->db->get('users', ['username','balance'], ['id' => $a['user_id']]);
|
|
$a['player_count'] = $this->db->count('users', ['agent_id' => $a['id']]);
|
|
}
|
|
$this->render('Admin/agents.php', compact('agents'));
|
|
}
|
|
|
|
public function update() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$id = (int)($data['id'] ?? 0);
|
|
|
|
if ($id > 0) {
|
|
$this->db->update('agents', [
|
|
'commission_rate' => floatval($data['commission_rate'] ?? 0),
|
|
'rebate_rate' => floatval($data['rebate_rate'] ?? 0),
|
|
'status' => (int)($data['status'] ?? 1),
|
|
], ['id' => $id]);
|
|
} else {
|
|
// 创建代理:先创建用户,再创建代理记录
|
|
$userId = (int)($data['user_id'] ?? 0);
|
|
if (!$userId) { $this->json(['status'=>'error','message'=>'User ID required']); return; }
|
|
$code = strtoupper(substr(md5(uniqid()), 0, 8));
|
|
$this->db->insert('agents', [
|
|
'user_id' => $userId,
|
|
'parent_id' => !empty($data['parent_id']) ? (int)$data['parent_id'] : null,
|
|
'level' => !empty($data['parent_id']) ? 2 : 1,
|
|
'agent_code' => $code,
|
|
'commission_rate' => floatval($data['commission_rate'] ?? 0),
|
|
'rebate_rate' => floatval($data['rebate_rate'] ?? 0),
|
|
'status' => 1,
|
|
]);
|
|
$this->db->update('users', ['role' => 'agent'], ['id' => $userId]);
|
|
}
|
|
$this->json(['status' => 'success']);
|
|
}
|
|
|
|
public function delete($id) {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$agent = $this->db->get('agents', '*', ['id' => (int)$id]);
|
|
if ($agent) {
|
|
$this->db->update('users', ['role' => 'user', 'agent_id' => null], ['agent_id' => $agent['id']]);
|
|
$this->db->delete('agents', ['id' => (int)$id]);
|
|
}
|
|
$this->json(['status' => 'success']);
|
|
}
|
|
|
|
// 代理赔率设置
|
|
public function odds($agentId) {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$odds = $this->db->select('agent_odds', '*', ['agent_id' => (int)$agentId]);
|
|
$this->json(['status' => 'success', 'data' => $odds]);
|
|
}
|
|
|
|
public function updateOdds() {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$agentId = (int)($data['agent_id'] ?? 0);
|
|
$odds = $data['odds'] ?? [];
|
|
$gameId = (int)($data['game_id'] ?? 0);
|
|
|
|
foreach ($odds as $o) {
|
|
$existing = $this->db->get('agent_odds', 'id', [
|
|
'agent_id' => $agentId, 'game_id' => $gameId,
|
|
'bet_type' => $o['bet_type'], 'bet_target' => $o['bet_target']
|
|
]);
|
|
if ($existing) {
|
|
$this->db->update('agent_odds', ['odds' => floatval($o['odds'])], ['id' => $existing]);
|
|
} else {
|
|
$this->db->insert('agent_odds', [
|
|
'agent_id' => $agentId, 'game_id' => $gameId,
|
|
'bet_type' => $o['bet_type'], 'bet_target' => $o['bet_target'],
|
|
'odds' => floatval($o['odds']),
|
|
]);
|
|
}
|
|
}
|
|
$this->json(['status' => 'success']);
|
|
}
|
|
|
|
// 佣金记录
|
|
public function commissions($agentId) {
|
|
$this->checkLogin(); $this->checkAdmin();
|
|
$records = $this->db->select('agent_commissions', '*', [
|
|
'agent_id' => (int)$agentId, 'ORDER' => ['id' => 'DESC'], 'LIMIT' => 100
|
|
]);
|
|
$this->json(['status' => 'success', 'data' => $records]);
|
|
}
|
|
|
|
private function json($data) { header('Content-Type: application/json'); echo json_encode($data); }
|
|
}
|