better-staridc-MNBT
1<?php
2/**
3 * 管理员端 - 用户列表管理
4 */
5if (!defined('IN_CRONLITE')) {
6 exit;
7}
8
9global $DB, $date;
10
11// POST 操作:启用/禁用/删除用户
12if ($_SERVER['REQUEST_METHOD'] === 'POST') {
13 $action = $_POST['action'] ?? '';
14 $user_id = (int)($_POST['user_id'] ?? 0);
15 if ($user_id > 0) {
16 if ($action === 'toggle_status') {
17 $row = $DB->get_row_prepare("SELECT status FROM MN_plugin_user WHERE id=? LIMIT 1", [$user_id]);
18 if ($row) {
19 $new_status = (int)$row['status'] === 1 ? 0 : 1;
20 $now = $date ?: date('Y-m-d H:i:s');
21 $DB->query_prepare("UPDATE MN_plugin_user SET status=?, updated_at=? WHERE id=?", [$new_status, $now, $user_id]);
22 }
23 }
24 }
25 header('Location: plugin.php?p=user_info&page=users&' . http_build_query([
26 'page_num' => $_GET['page_num'] ?? 1,
27 'kw' => $_GET['kw'] ?? '',
28 ]));
29 exit;
30}
31
32// 筛选与分页
33$page_num = max(1, (int)($_GET['page_num'] ?? 1));
34$per_page = 30;
35$offset = ($page_num - 1) * $per_page;
36$kw = trim((string)($_GET['kw'] ?? ''));
37
38$where = '';
39$params = [];
40if ($kw !== '') {
41 $where = " WHERE username LIKE ? OR email LIKE ? OR qq LIKE ?";
42 $like = '%' . $kw . '%';
43 $params = [$like, $like, $like];
44}
45
46$count_row = $DB->get_row_prepare("SELECT COUNT(*) AS cnt FROM MN_plugin_user{$where}", $params);
47$total = $count_row ? (int)$count_row['cnt'] : 0;
48
49$list = $DB->get_all_prepare(
50 "SELECT * FROM MN_plugin_user{$where} ORDER BY id DESC LIMIT {$offset},{$per_page}",
51 $params
52) ?: [];
53
54$total_pages = $per_page > 0 ? (int)ceil($total / $per_page) : 1;
55
56$title = $title ?? '用户管理';
57mnbt_admin_include('head');
58?>
59<link rel="stylesheet" href="<?= mnbt_asset_url('js/jquery-confirm/jquery-confirm.min.css') ?>">
60<script src="<?= mnbt_asset_url('js/jquery-confirm/jquery-confirm.min.js') ?>"></script>
61
62<div class="container-fluid p-t-15">
63 <div class="card">
64 <div class="card-header">
65 <h4 style="display:inline-block">用户管理</h4>
66 <small class="text-muted ml-2">共 <?= $total ?> 个用户</small>
67 </div>
68 <div class="card-body">
69 <form method="get" class="form-inline mb-3">
70 <input type="hidden" name="p" value="user_info">
71 <input type="hidden" name="page" value="users">
72 <input type="text" name="kw" class="form-control form-control-sm mr-2" placeholder="用户名 / 邮箱 / QQ" value="<?= htmlspecialchars($kw, ENT_QUOTES) ?>">
73 <button type="submit" class="btn btn-sm btn-primary">搜索</button>
74 <a href="plugin.php?p=user_info&page=users" class="btn btn-sm btn-outline-secondary ml-2">重置</a>
75 </form>
76
77 <div class="table-responsive">
78 <table class="table table-bordered table-hover">
79 <thead>
80 <tr>
81 <th style="width:60px">ID</th>
82 <th>用户名</th>
83 <th>邮箱</th>
84 <th>QQ</th>
85 <th>状态</th>
86 <th>注册时间</th>
87 <th style="width:200px">操作</th>
88 </tr>
89 </thead>
90 <tbody>
91 <?php if (empty($list)): ?>
92 <tr><td colspan="7" class="text-center text-muted">暂无用户</td></tr>
93 <?php else: ?>
94 <?php foreach ($list as $u): ?>
95 <tr>
96 <td><?= (int)$u['id'] ?></td>
97 <td><code><?= htmlspecialchars($u['username'], ENT_QUOTES) ?></code></td>
98 <td><?= htmlspecialchars($u['email'] ?: '-', ENT_QUOTES) ?></td>
99 <td><?= htmlspecialchars($u['qq'] ?: '-', ENT_QUOTES) ?></td>
100 <td>
101 <?php if ((int)$u['status'] === 1): ?>
102 <span class="badge badge-success">正常</span>
103 <?php else: ?>
104 <span class="badge badge-danger">禁用</span>
105 <?php endif; ?>
106 </td>
107 <td class="small text-muted"><?= htmlspecialchars($u['created_at'], ENT_QUOTES) ?></td>
108 <td>
109 <a href="plugin.php?p=user_info&page=user_edit&id=<?= (int)$u['id'] ?>" class="btn btn-sm btn-outline-primary multitabs">编辑</a>
110 <button type="button" class="btn btn-sm btn-outline-warning" onclick="resetPass(<?= (int)$u['id'] ?>, '<?= htmlspecialchars($u['username'], ENT_QUOTES) ?>')">重置密码</button>
111 <form method="post" class="d-inline" onsubmit="return confirm('确定<?= (int)$u['status'] === 1 ? '禁用' : '启用' ?>该用户?')">
112 <input type="hidden" name="action" value="toggle_status">
113 <input type="hidden" name="user_id" value="<?= (int)$u['id'] ?>">
114 <button type="submit" class="btn btn-sm <?= (int)$u['status'] === 1 ? 'btn-outline-danger' : 'btn-outline-success' ?>">
115 <?= (int)$u['status'] === 1 ? '禁用' : '启用' ?>
116 </button>
117 </form>
118 </td>
119 </tr>
120 <?php endforeach; ?>
121 <?php endif; ?>
122 </tbody>
123 </table>
124 </div>
125
126 <?php if ($total_pages > 1): ?>
127 <nav>
128 <ul class="pagination justify-content-center">
129 <?php for ($i = 1; $i <= $total_pages; $i++): ?>
130 <li class="page-item <?= $i === $page_num ? 'active' : '' ?>">
131 <a class="page-link" href="plugin.php?p=user_info&page=users&page_num=<?= $i ?>&kw=<?= urlencode($kw) ?>"><?= $i ?></a>
132 </li>
133 <?php endfor; ?>
134 </ul>
135 </nav>
136 <?php endif; ?>
137 </div>
138 </div>
139</div>
140
141<script>
142function resetPass(uid, uname) {
143 $.confirm({
144 title: '重置密码 - ' + uname,
145 content: '<input type="password" class="form-control" id="reset-pwd" placeholder="新密码(至少6位)">',
146 buttons: {
147 confirm: {
148 text: '确定重置',
149 btnClass: 'btn-primary',
150 action: function () {
151 var pwd = $.trim($('#reset-pwd').val());
152 if (pwd.length < 6) {
153 $.alert('新密码至少 6 个字符');
154 return false;
155 }
156 $.post('ajax.php', {
157 gn: 'user_info_admin_reset_password',
158 user_id: uid,
159 new_password: pwd
160 }, function (resp) {
161 var j;
162 try { j = typeof resp === 'string' ? JSON.parse(resp) : resp; } catch (e) { j = { code: resp }; }
163 var code = j.code || j.msg || '';
164 if (code === '重置成功') {
165 $.alert({ title: '成功', content: '密码已重置', type: 'green' });
166 } else {
167 $.alert({ title: '失败', content: code || '操作失败', type: 'red' });
168 }
169 }).fail(function () {
170 $.alert({ title: '错误', content: '网络错误', type: 'red' });
171 });
172 return true;
173 }
174 },
175 cancel: { text: '取消' }
176 }
177 });
178}
179</script>