-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_edit.php
More file actions
121 lines (107 loc) · 4.87 KB
/
Copy pathuser_edit.php
File metadata and controls
121 lines (107 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/init_check.php';
require_once __DIR__ . '/core/Session.php';
require_once __DIR__ . '/core/TokenSystem.php';
require_once __DIR__ . '/core/StockEngine.php';
require_once __DIR__ . '/core/GachaEngine.php';
require_once __DIR__ . '/core/TradingEngine.php';
require_once __DIR__ . '/core/PoolEngine.php';
require_once __DIR__ . '/core/MarketEngine.php';
require_once __DIR__ . '/adapters/manager.php';
Session::start();
Session::requireAuth();
$admin = Session::user();
if (!$admin || !$admin['is_admin']) { header('Location: ' . url('/')); exit; }
$targetId = (int)($_GET['id'] ?? 0);
if ($targetId <= 0) { header('Location: ' . url('/user_manager.php')); exit; }
$db = Database::getInstance();
// Fetch target user
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$targetId]);
$targetUser = $stmt->fetch();
if (!$targetUser) { echo '用户不存在'; exit; }
$message = null;
$error = null;
// Handle POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'update_user') {
$fields = ['username', 'token_balance', 'is_admin'];
$sets = [];
$params = [];
foreach ($fields as $f) {
if (isset($_POST[$f])) {
if ($f === 'is_admin') {
$sets[] = "is_admin = ?";
$params[] = (int)(bool)$_POST[$f];
} else {
$sets[] = "{$f} = ?";
$params[] = $_POST[$f];
}
}
}
if (!empty($sets)) {
$params[] = $targetId;
$db->prepare("UPDATE users SET " . implode(', ', $sets) . ", updated_at = CURRENT_TIMESTAMP WHERE id = ?")->execute($params);
$message = '✅ 用户信息已更新。';
// Refresh
$stmt->execute([$targetId]);
$targetUser = $stmt->fetch();
}
}
if ($action === 'clear_holdings') {
$db->prepare("DELETE FROM card_placements WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM holdings WHERE user_id = ?")->execute([$targetId]);
$message = '✅ 持仓已清空。';
}
if ($action === 'reset_password') {
$newPw = trim($_POST['new_password'] ?? '');
if (strlen($newPw) >= 6) {
$hash = password_hash($newPw, PASSWORD_DEFAULT);
$db->prepare("UPDATE users SET password_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?")->execute([$hash, $targetId]);
$message = '✅ 密码已重置。';
} else { $error = '密码至少6位。'; }
}
if ($action === 'delete_user') {
$db->prepare("DELETE FROM card_placements WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM holdings WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM transactions WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM gacha_logs WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM daily_checkins WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM user_quests WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM user_hustoj_bindings WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM claimed_rewards WHERE user_id = ?")->execute([$targetId]);
$db->prepare("DELETE FROM card_market_listings WHERE seller_id = ? OR buyer_id = ?")->execute([$targetId, $targetId]);
$db->prepare("DELETE FROM users WHERE id = ?")->execute([$targetId]);
header('Location: ' . url('/user_manager.php'));
exit;
}
}
// Stats
$holdingCount = $db->prepare("SELECT COUNT(*) FROM holdings WHERE user_id = ? AND quantity > 0");
$holdingCount->execute([$targetId]);
$hc = (int)$holdingCount->fetchColumn();
$gachaCount = $db->prepare("SELECT COUNT(*) FROM gacha_logs WHERE user_id = ?");
$gachaCount->execute([$targetId]);
$gc = (int)$gachaCount->fetchColumn();
$txCount = $db->prepare("SELECT COUNT(*) FROM transactions WHERE user_id = ?");
$txCount->execute([$targetId]);
$tc = (int)$txCount->fetchColumn();
// Holdings with pagination
$holdPage = max(1, (int)($_GET['hold_page'] ?? 1));
$holdPerPage = 20;
$holdings = $db->prepare("
SELECT h.*, s.symbol, s.name as stock_name, s.rarity, s.limited_edition, s.current_price,
(h.quantity * s.current_price) as market_value,
((s.current_price - h.avg_cost) * h.quantity) as profit_loss
FROM holdings h JOIN stocks s ON h.stock_id = s.id
WHERE h.user_id = ? AND h.quantity > 0
ORDER BY market_value DESC
LIMIT ? OFFSET ?
");
$holdings->execute([$targetId, $holdPerPage, ($holdPage - 1) * $holdPerPage]);
$holdRows = $holdings->fetchAll();
$holdTotalPages = max(1, ceil($hc / $holdPerPage));
require_once __DIR__ . '/templates/user_edit.php';