index.php
7.91 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
require_once __DIR__ . '/../vendor/autoload.php';
// Ensure $config is available (passed from start.php context or loaded manually)
if (!isset($config)) {
$config = require __DIR__ . '/../config.php';
}
$pdo = null;
$message = '';
// 连接数据库
try {
$db = $config['database'];
$dsn = "mysql:host={$db['host']};port={$db['port']};dbname={$db['database']};charset=utf8mb4";
$pdo = new PDO($dsn, $db['username'], $db['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// FATAL: Do NOT use die() in Workerman, it kills the worker process!
$message = "<div style='color: red; padding: 20px; text-align: center; border: 1px solid red; background: #ffe6e6;'>Database Connection Failed: " . htmlspecialchars($e->getMessage()) . "</div>";
$pdo = null;
}
// 处理绑定表单提交
if ($pdo && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'bind') {
$deviceId = trim($_POST['device_id']);
$phone = trim($_POST['phone']);
$isPrimary = isset($_POST['is_primary']) ? 1 : 0;
if ($deviceId && $phone) {
// 1. 查找用户
try {
$stmt = $pdo->prepare("SELECT id, nickname FROM users WHERE phone = ?");
$stmt->execute([$phone]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) {
// 2. 验证设备是否存在 (Prevent Ghost Bindings)
$deviceCheck = $pdo->prepare("SELECT id FROM devices WHERE id = ?");
$deviceCheck->execute([$deviceId]);
if (!$deviceCheck->fetch()) {
$message = "<div style='color: red; margin-bottom: 20px;'>❌ 设备ID <b>$deviceId</b> 不存在。请确认该设备已录入系统。</div>";
} else {
// 3. 插入/更新绑定
// 先检查是否已存在
$check = $pdo->prepare("SELECT id FROM user_device_bindings WHERE user_id = ? AND device_id = ?");
$check->execute([$user['id'], $deviceId]);
if (!$check->fetch()) {
// FIX: use 'bound_at' instead of 'created_at'
$bind = $pdo->prepare("INSERT INTO user_device_bindings (user_id, device_id, is_primary, bound_at) VALUES (?, ?, ?, NOW())");
$bind->execute([$user['id'], $deviceId, $isPrimary]);
$message = "<div style='color: green; margin-bottom: 20px; background: #e6fffa; padding: 10px; border-radius: 4px;'>✅ 成功将设备 <b>$deviceId</b> 绑定给用户 <b>{$user['nickname']}</b></div>";
} else {
$message = "<div style='color: orange; margin-bottom: 20px;'>⚠️ 该用户已经绑定过此设备,无需重复操作。</div>";
}
}
} else {
$message = "<div style='color: red; margin-bottom: 20px;'>❌ 手机号 <b>$phone</b> 未找到。请确保用户已在小程序登录过。</div>";
}
} catch (Exception $e) {
$message = "<div style='color: red;'>Operation Failed: " . $e->getMessage() . "</div>";
}
}
}
// 获取设备列表
$bindings = [];
if ($pdo) {
try {
// FIX: use 'bound_at' instead of 'created_at'
$bindings = $pdo->query("
SELECT b.id, u.nickname, u.phone, b.device_id, b.is_primary, b.bound_at
FROM user_device_bindings b
JOIN users u ON b.user_id = u.id
ORDER BY b.bound_at DESC
")->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
if (empty($message))
$message = "<div style='color: red;'>Load Error: " . $e->getMessage() . "</div>";
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moltbot 管理后台</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="data:,">
</head>
<body>
<div class="header">
<div class="brand">Moltbot Admin</div>
<div><?php echo date('Y-m-d H:i'); ?></div>
</div>
<div class="container">
<?php echo $message; ?>
<?php if (!$pdo): ?>
<div class="card">
<h3 style="color: red;">System Error</h3>
<p>Cannot connect to the database. Please check your configuration.</p>
</div>
<?php else: ?>
<!-- 新增绑定卡片 -->
<div class="card">
<div class="title">新增绑定</div>
<form method="POST" action="">
<input type="hidden" name="action" value="bind">
<div style="display: flex; gap: 20px;">
<div class="form-group" style="flex: 1;">
<label class="form-label">设备 ID</label>
<input type="text" name="device_id" class="form-control" placeholder="例如: dev_test_001"
required>
</div>
<div class="form-group" style="flex: 1;">
<label class="form-label">用户手机号</label>
<input type="text" name="phone" class="form-control" placeholder="输入用户注册手机号" required>
</div>
</div>
<div class="form-group">
<label>
<input type="checkbox" name="is_primary" value="1" checked> 设为主设备 (默认)
</label>
</div>
<button type="submit" class="btn btn-primary">立即绑定</button>
</form>
</div>
<!-- 绑定列表卡片 -->
<div class="card">
<div class="title">绑定记录 (<?php echo count($bindings); ?>)</div>
<table>
<thead>
<tr>
<th>用户</th>
<th>手机号</th>
<th>设备 ID</th>
<th>主设备</th>
<th>绑定时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($bindings as $row): ?>
<tr>
<td><?php echo htmlspecialchars($row['nickname']); ?></td>
<td><?php echo htmlspecialchars($row['phone']); ?></td>
<td><?php echo htmlspecialchars($row['device_id']); ?></td>
<td>
<?php if ($row['is_primary']): ?>
<span style="color: var(--primary-color);">✔</span>
<?php endif; ?>
</td>
<td><?php echo $row['bound_at']; ?></td>
<td>
<a href="#" style="color: red; font-size: 12px; text-decoration: none;">解绑</a>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($bindings)): ?>
<tr>
<td colspan="6" style="text-align: center; color: #999;">暂无数据</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</body>
</html>