Commit 3cc7a772a3265221ef1b72c54234c39040d40d4d
1 parent
e8094d15
fix: use config array in admin portal instead of env parsing
Showing
1 changed file
with
94 additions
and
91 deletions
| 1 | 1 | <?php |
| 2 | 2 | require_once __DIR__ . '/../vendor/autoload.php'; |
| 3 | 3 | |
| 4 | -// 加载配置 (.env 手动加载逻辑同 start.php) | |
| 5 | -if (file_exists(__DIR__ . '/../.env')) { | |
| 6 | - $lines = file(__DIR__ . '/../.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
| 7 | - foreach ($lines as $line) { | |
| 8 | - if (strpos(trim($line), '#') === 0) continue; | |
| 9 | - list($name, $value) = explode('=', $line, 2); | |
| 10 | - $_ENV[trim($name)] = trim($value); | |
| 11 | - } | |
| 4 | +// Ensure $config is available (passed from start.php context or loaded manually) | |
| 5 | +if (!isset($config)) { | |
| 6 | + $config = require __DIR__ . '/../config.php'; | |
| 12 | 7 | } |
| 13 | 8 | |
| 14 | 9 | $pdo = null; |
| ... | ... | @@ -16,13 +11,14 @@ $message = ''; |
| 16 | 11 | |
| 17 | 12 | // 连接数据库 |
| 18 | 13 | try { |
| 19 | - $dsn = "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_NAME']};charset=utf8mb4"; | |
| 20 | - $pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASS']); | |
| 14 | + $db = $config['database']; | |
| 15 | + $dsn = "mysql:host={$db['host']};port={$db['port']};dbname={$db['database']};charset=utf8mb4"; | |
| 16 | + $pdo = new PDO($dsn, $db['username'], $db['password']); | |
| 21 | 17 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 22 | 18 | } catch (PDOException $e) { |
| 23 | 19 | // FATAL: Do NOT use die() in Workerman, it kills the worker process! |
| 24 | 20 | $message = "<div style='color: red; padding: 20px; text-align: center; border: 1px solid red; background: #ffe6e6;'>Database Connection Failed: " . htmlspecialchars($e->getMessage()) . "</div>"; |
| 25 | - $pdo = null; | |
| 21 | + $pdo = null; | |
| 26 | 22 | } |
| 27 | 23 | |
| 28 | 24 | // 处理绑定表单提交 |
| ... | ... | @@ -37,13 +33,13 @@ if ($pdo && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && |
| 37 | 33 | $stmt = $pdo->prepare("SELECT id, nickname FROM users WHERE phone = ?"); |
| 38 | 34 | $stmt->execute([$phone]); |
| 39 | 35 | $user = $stmt->fetch(PDO::FETCH_ASSOC); |
| 40 | - | |
| 36 | + | |
| 41 | 37 | if ($user) { |
| 42 | 38 | // 2. 插入/更新绑定 |
| 43 | 39 | // 先检查是否已存在 |
| 44 | 40 | $check = $pdo->prepare("SELECT id FROM user_device_bindings WHERE user_id = ? AND device_id = ?"); |
| 45 | 41 | $check->execute([$user['id'], $deviceId]); |
| 46 | - | |
| 42 | + | |
| 47 | 43 | if (!$check->fetch()) { |
| 48 | 44 | $bind = $pdo->prepare("INSERT INTO user_device_bindings (user_id, device_id, is_primary, created_at) VALUES (?, ?, ?, NOW())"); |
| 49 | 45 | $bind->execute([$user['id'], $deviceId, $isPrimary]); |
| ... | ... | @@ -55,7 +51,7 @@ if ($pdo && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && |
| 55 | 51 | $message = "<div style='color: red; margin-bottom: 20px;'>❌ 手机号 <b>$phone</b> 未找到。请确保用户已在小程序登录过。</div>"; |
| 56 | 52 | } |
| 57 | 53 | } catch (Exception $e) { |
| 58 | - $message = "<div style='color: red;'>Operation Failed: " . $e->getMessage() . "</div>"; | |
| 54 | + $message = "<div style='color: red;'>Operation Failed: " . $e->getMessage() . "</div>"; | |
| 59 | 55 | } |
| 60 | 56 | } |
| 61 | 57 | } |
| ... | ... | @@ -71,13 +67,15 @@ if ($pdo) { |
| 71 | 67 | ORDER BY b.created_at DESC |
| 72 | 68 | ")->fetchAll(PDO::FETCH_ASSOC); |
| 73 | 69 | } catch (Exception $e) { |
| 74 | - if (empty($message)) $message = "<div style='color: red;'>Load Error: " . $e->getMessage() . "</div>"; | |
| 70 | + if (empty($message)) | |
| 71 | + $message = "<div style='color: red;'>Load Error: " . $e->getMessage() . "</div>"; | |
| 75 | 72 | } |
| 76 | 73 | } |
| 77 | 74 | |
| 78 | 75 | ?> |
| 79 | 76 | <!DOCTYPE html> |
| 80 | 77 | <html lang="zh-CN"> |
| 78 | + | |
| 81 | 79 | <head> |
| 82 | 80 | <meta charset="UTF-8"> |
| 83 | 81 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| ... | ... | @@ -85,89 +83,94 @@ if ($pdo) { |
| 85 | 83 | <link rel="stylesheet" href="style.css"> |
| 86 | 84 | <link rel="icon" href="data:,"> |
| 87 | 85 | </head> |
| 86 | + | |
| 88 | 87 | <body> |
| 89 | 88 | |
| 90 | -<div class="header"> | |
| 91 | - <div class="brand">Moltbot Admin</div> | |
| 92 | - <div><?php echo date('Y-m-d H:i'); ?></div> | |
| 93 | -</div> | |
| 94 | - | |
| 95 | -<div class="container"> | |
| 96 | - | |
| 97 | - <?php echo $message; ?> | |
| 98 | - | |
| 99 | - <?php if (!$pdo): ?> | |
| 100 | - <div class="card"> | |
| 101 | - <h3 style="color: red;">System Error</h3> | |
| 102 | - <p>Cannot connect to the database. Please check your configuration.</p> | |
| 103 | - </div> | |
| 104 | - <?php else: ?> | |
| 105 | - | |
| 106 | - <!-- 新增绑定卡片 --> | |
| 107 | - <div class="card"> | |
| 108 | - <div class="title">新增绑定</div> | |
| 109 | - <form method="POST" action=""> | |
| 110 | - <input type="hidden" name="action" value="bind"> | |
| 111 | - <div style="display: flex; gap: 20px;"> | |
| 112 | - <div class="form-group" style="flex: 1;"> | |
| 113 | - <label class="form-label">设备 ID</label> | |
| 114 | - <input type="text" name="device_id" class="form-control" placeholder="例如: dev_test_001" required> | |
| 115 | - </div> | |
| 116 | - <div class="form-group" style="flex: 1;"> | |
| 117 | - <label class="form-label">用户手机号</label> | |
| 118 | - <input type="text" name="phone" class="form-control" placeholder="输入用户注册手机号" required> | |
| 119 | - </div> | |
| 89 | + <div class="header"> | |
| 90 | + <div class="brand">Moltbot Admin</div> | |
| 91 | + <div><?php echo date('Y-m-d H:i'); ?></div> | |
| 92 | + </div> | |
| 93 | + | |
| 94 | + <div class="container"> | |
| 95 | + | |
| 96 | + <?php echo $message; ?> | |
| 97 | + | |
| 98 | + <?php if (!$pdo): ?> | |
| 99 | + <div class="card"> | |
| 100 | + <h3 style="color: red;">System Error</h3> | |
| 101 | + <p>Cannot connect to the database. Please check your configuration.</p> | |
| 120 | 102 | </div> |
| 121 | - <div class="form-group"> | |
| 122 | - <label> | |
| 123 | - <input type="checkbox" name="is_primary" value="1" checked> 设为主设备 (默认) | |
| 124 | - </label> | |
| 103 | + <?php else: ?> | |
| 104 | + | |
| 105 | + <!-- 新增绑定卡片 --> | |
| 106 | + <div class="card"> | |
| 107 | + <div class="title">新增绑定</div> | |
| 108 | + <form method="POST" action=""> | |
| 109 | + <input type="hidden" name="action" value="bind"> | |
| 110 | + <div style="display: flex; gap: 20px;"> | |
| 111 | + <div class="form-group" style="flex: 1;"> | |
| 112 | + <label class="form-label">设备 ID</label> | |
| 113 | + <input type="text" name="device_id" class="form-control" placeholder="例如: dev_test_001" | |
| 114 | + required> | |
| 115 | + </div> | |
| 116 | + <div class="form-group" style="flex: 1;"> | |
| 117 | + <label class="form-label">用户手机号</label> | |
| 118 | + <input type="text" name="phone" class="form-control" placeholder="输入用户注册手机号" required> | |
| 119 | + </div> | |
| 120 | + </div> | |
| 121 | + <div class="form-group"> | |
| 122 | + <label> | |
| 123 | + <input type="checkbox" name="is_primary" value="1" checked> 设为主设备 (默认) | |
| 124 | + </label> | |
| 125 | + </div> | |
| 126 | + <button type="submit" class="btn btn-primary">立即绑定</button> | |
| 127 | + </form> | |
| 125 | 128 | </div> |
| 126 | - <button type="submit" class="btn btn-primary">立即绑定</button> | |
| 127 | - </form> | |
| 128 | - </div> | |
| 129 | 129 | |
| 130 | - <!-- 绑定列表卡片 --> | |
| 131 | - <div class="card"> | |
| 132 | - <div class="title">绑定记录 (<?php echo count($bindings); ?>)</div> | |
| 133 | - <table> | |
| 134 | - <thead> | |
| 135 | - <tr> | |
| 136 | - <th>用户</th> | |
| 137 | - <th>手机号</th> | |
| 138 | - <th>设备 ID</th> | |
| 139 | - <th>主设备</th> | |
| 140 | - <th>绑定时间</th> | |
| 141 | - <th>操作</th> | |
| 142 | - </tr> | |
| 143 | - </thead> | |
| 144 | - <tbody> | |
| 145 | - <?php foreach ($bindings as $row): ?> | |
| 146 | - <tr> | |
| 147 | - <td><?php echo htmlspecialchars($row['nickname']); ?></td> | |
| 148 | - <td><?php echo htmlspecialchars($row['phone']); ?></td> | |
| 149 | - <td><?php echo htmlspecialchars($row['device_id']); ?></td> | |
| 150 | - <td> | |
| 151 | - <?php if ($row['is_primary']): ?> | |
| 152 | - <span style="color: var(--primary-color);">✔</span> | |
| 130 | + <!-- 绑定列表卡片 --> | |
| 131 | + <div class="card"> | |
| 132 | + <div class="title">绑定记录 (<?php echo count($bindings); ?>)</div> | |
| 133 | + <table> | |
| 134 | + <thead> | |
| 135 | + <tr> | |
| 136 | + <th>用户</th> | |
| 137 | + <th>手机号</th> | |
| 138 | + <th>设备 ID</th> | |
| 139 | + <th>主设备</th> | |
| 140 | + <th>绑定时间</th> | |
| 141 | + <th>操作</th> | |
| 142 | + </tr> | |
| 143 | + </thead> | |
| 144 | + <tbody> | |
| 145 | + <?php foreach ($bindings as $row): ?> | |
| 146 | + <tr> | |
| 147 | + <td><?php echo htmlspecialchars($row['nickname']); ?></td> | |
| 148 | + <td><?php echo htmlspecialchars($row['phone']); ?></td> | |
| 149 | + <td><?php echo htmlspecialchars($row['device_id']); ?></td> | |
| 150 | + <td> | |
| 151 | + <?php if ($row['is_primary']): ?> | |
| 152 | + <span style="color: var(--primary-color);">✔</span> | |
| 153 | + <?php endif; ?> | |
| 154 | + </td> | |
| 155 | + <td><?php echo $row['created_at']; ?></td> | |
| 156 | + <td> | |
| 157 | + <a href="#" style="color: red; font-size: 12px; text-decoration: none;">解绑</a> | |
| 158 | + </td> | |
| 159 | + </tr> | |
| 160 | + <?php endforeach; ?> | |
| 161 | + <?php if (empty($bindings)): ?> | |
| 162 | + <tr> | |
| 163 | + <td colspan="6" style="text-align: center; color: #999;">暂无数据</td> | |
| 164 | + </tr> | |
| 153 | 165 | <?php endif; ?> |
| 154 | - </td> | |
| 155 | - <td><?php echo $row['created_at']; ?></td> | |
| 156 | - <td> | |
| 157 | - <a href="#" style="color: red; font-size: 12px; text-decoration: none;">解绑</a> | |
| 158 | - </td> | |
| 159 | - </tr> | |
| 160 | - <?php endforeach; ?> | |
| 161 | - <?php if (empty($bindings)): ?> | |
| 162 | - <tr><td colspan="6" style="text-align: center; color: #999;">暂无数据</td></tr> | |
| 163 | - <?php endif; ?> | |
| 164 | - </tbody> | |
| 165 | - </table> | |
| 166 | - </div> | |
| 167 | - | |
| 168 | - <?php endif; ?> | |
| 166 | + </tbody> | |
| 167 | + </table> | |
| 168 | + </div> | |
| 169 | + | |
| 170 | + <?php endif; ?> | |
| 169 | 171 | |
| 170 | -</div> | |
| 172 | + </div> | |
| 171 | 173 | |
| 172 | 174 | </body> |
| 175 | + | |
| 173 | 176 | </html> |
| \ No newline at end of file | ... | ... |
Please
register
or
login
to post a comment