feat: 拆约后台管理面板(admin-web 单页)
This commit is contained in:
@@ -0,0 +1,305 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>拆约 · 后台管理</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #f3f5f9; --card: #ffffff; --line: #e5e9f2;
|
||||
--text: #1c2333; --muted: #8a93a6; --brand: #4f6ef7; --brand-dark: #3d55c8;
|
||||
--green: #16a34a; --green-bg: #e8f7ee; --red: #dc2626; --red-bg: #fdecec;
|
||||
}
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body { font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; }
|
||||
|
||||
/* ===== 登录页 ===== */
|
||||
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #4f6ef7 0%, #7a5cf0 100%); }
|
||||
.login-card { background: #fff; border-radius: 16px; padding: 40px 36px; width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
|
||||
.login-card h1 { font-size: 22px; margin-bottom: 6px; }
|
||||
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 26px; }
|
||||
.field { margin-bottom: 16px; }
|
||||
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
|
||||
.field input { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; outline: none; transition: border .15s; }
|
||||
.field input:focus { border-color: var(--brand); }
|
||||
.btn { display: inline-block; border: none; cursor: pointer; font-size: 14px; border-radius: 8px; padding: 10px 18px; transition: all .15s; }
|
||||
.btn-primary { background: var(--brand); color: #fff; width: 100%; padding: 12px; font-size: 15px; }
|
||||
.btn-primary:hover { background: var(--brand-dark); }
|
||||
.login-error { color: var(--red); font-size: 13px; margin-bottom: 12px; min-height: 18px; }
|
||||
|
||||
/* ===== 主界面 ===== */
|
||||
.topbar { background: #fff; border-bottom: 1px solid var(--line); padding: 0 28px; height: 60px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 10; }
|
||||
.topbar .brand { font-weight: 700; font-size: 17px; }
|
||||
.topbar .brand span { color: var(--brand); }
|
||||
.topbar .who { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 14px; }
|
||||
.topbar .who a { color: var(--red); cursor: pointer; text-decoration: none; }
|
||||
.container { max-width: 1100px; margin: 24px auto; padding: 0 24px; }
|
||||
|
||||
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px; }
|
||||
.stat-card { background: var(--card); border-radius: 12px; padding: 18px 20px; border: 1px solid var(--line); }
|
||||
.stat-card .num { font-size: 26px; font-weight: 700; }
|
||||
.stat-card .label { color: var(--muted); font-size: 13px; margin-top: 4px; }
|
||||
|
||||
.panel { background: var(--card); border-radius: 12px; border: 1px solid var(--line); overflow: hidden; }
|
||||
.panel-head { padding: 16px 20px; display: flex; align-items: center; justify-content: space-between; gap: 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
|
||||
.panel-head h2 { font-size: 16px; }
|
||||
.search-box { display: flex; gap: 8px; }
|
||||
.search-box input { padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; width: 240px; outline: none; }
|
||||
.search-box input:focus { border-color: var(--brand); }
|
||||
|
||||
table { width: 100%; border-collapse: collapse; font-size: 14px; }
|
||||
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
|
||||
th { color: var(--muted); font-weight: 500; font-size: 12.5px; background: #fafbfe; }
|
||||
td .nick { display: flex; align-items: center; gap: 10px; }
|
||||
td img { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: #eef; }
|
||||
.avatar-fallback { width: 34px; height: 34px; border-radius: 50%; background: #eef0fa; color: var(--brand); display: inline-flex; align-items: center; justify-content: center; font-size: 14px; }
|
||||
.tag { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; }
|
||||
.tag-admin { background: var(--green-bg); color: var(--green); }
|
||||
.tag-user { background: #eef0f5; color: var(--muted); }
|
||||
.btn-sm { padding: 7px 14px; font-size: 13px; border-radius: 7px; }
|
||||
.btn-grant { background: var(--brand); color: #fff; }
|
||||
.btn-grant:hover { background: var(--brand-dark); }
|
||||
.btn-revoke { background: #fff; color: var(--red); border: 1px solid #f3c1c1; }
|
||||
.btn-revoke:hover { background: var(--red-bg); }
|
||||
.muted { color: var(--muted); font-size: 12.5px; }
|
||||
.empty { padding: 40px; text-align: center; color: var(--muted); }
|
||||
.pager { display: flex; align-items: center; justify-content: flex-end; gap: 12px; padding: 14px 20px; font-size: 13px; color: var(--muted); }
|
||||
.pager button { border: 1px solid var(--line); background: #fff; border-radius: 7px; padding: 6px 14px; cursor: pointer; }
|
||||
.pager button:disabled { opacity: .4; cursor: default; }
|
||||
|
||||
/* ===== 改密弹窗 ===== */
|
||||
.modal-mask { position: fixed; inset: 0; background: rgba(20,26,45,.45); display: none; align-items: center; justify-content: center; z-index: 100; }
|
||||
.modal-mask.show { display: flex; }
|
||||
.modal { background: #fff; border-radius: 14px; padding: 30px; width: 380px; }
|
||||
.modal h3 { margin-bottom: 18px; font-size: 17px; }
|
||||
.modal .tip { color: var(--red); font-size: 13px; margin: -6px 0 14px; }
|
||||
|
||||
.toast { position: fixed; top: 24px; left: 50%; transform: translateX(-50%); background: #1c2333; color: #fff; padding: 10px 22px; border-radius: 999px; font-size: 14px; opacity: 0; transition: opacity .25s; z-index: 200; pointer-events: none; }
|
||||
.toast.show { opacity: .92; }
|
||||
.hidden { display: none !important; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<!-- 登录页 -->
|
||||
<div id="loginPage" class="login-wrap">
|
||||
<div class="login-card">
|
||||
<h1>拆约 · 后台管理</h1>
|
||||
<div class="sub">账号权限管理面板</div>
|
||||
<div class="login-error" id="loginError"></div>
|
||||
<div class="field">
|
||||
<label>用户名</label>
|
||||
<input id="loginUser" autocomplete="username" placeholder="请输入用户名">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>密码</label>
|
||||
<input id="loginPass" type="password" autocomplete="current-password" placeholder="请输入密码" >
|
||||
</div>
|
||||
<button class="btn btn-primary" id="loginBtn" onclick="doLogin()">登 录</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 主界面 -->
|
||||
<div id="mainPage" class="hidden">
|
||||
<div class="topbar">
|
||||
<div class="brand">拆约 · <span>后台管理</span></div>
|
||||
<div class="who">
|
||||
<span id="whoami"></span>
|
||||
<a onclick="doLogout()">退出</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="container">
|
||||
<div class="stats" id="stats"></div>
|
||||
<div class="panel">
|
||||
<div class="panel-head">
|
||||
<h2>用户列表</h2>
|
||||
<div class="search-box">
|
||||
<input id="searchInput" placeholder="搜索昵称 / 手机号 / 用户ID" onkeydown="if(event.key==='Enter')loadUsers(1)">
|
||||
<button class="btn btn-primary btn-sm" style="width:auto" onclick="loadUsers(1)">搜索</button>
|
||||
</div>
|
||||
</div>
|
||||
<div style="overflow-x:auto">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>用户</th><th>手机号</th><th>活动数</th><th>注册时间</th><th>角色</th><th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="userRows"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div id="emptyTip" class="empty hidden">暂无用户</div>
|
||||
<div class="pager">
|
||||
<span id="pageInfo"></span>
|
||||
<button id="prevBtn" onclick="loadUsers(page-1)">上一页</button>
|
||||
<button id="nextBtn" onclick="loadUsers(page+1)">下一页</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 强制改密弹窗 -->
|
||||
<div class="modal-mask" id="pwdModal">
|
||||
<div class="modal">
|
||||
<h3>修改密码</h3>
|
||||
<div class="tip" id="pwdTip">首次登录请修改默认密码</div>
|
||||
<div class="field"><label>原密码</label><input id="oldPwd" type="password"></div>
|
||||
<div class="field"><label>新密码(至少 8 位)</label><input id="newPwd" type="password"></div>
|
||||
<div class="field"><label>确认新密码</label><input id="newPwd2" type="password"></div>
|
||||
<div class="login-error" id="pwdError"></div>
|
||||
<button class="btn btn-primary" onclick="doChangePwd()">确认修改</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="toast" id="toast"></div>
|
||||
|
||||
<script>
|
||||
const API = location.origin;
|
||||
let token = sessionStorage.getItem('admin_token') || '';
|
||||
let mustChange = false;
|
||||
let page = 1, pageSize = 20, total = 0;
|
||||
|
||||
// ---------- 工具 ----------
|
||||
function showToast(msg) {
|
||||
const t = document.getElementById('toast');
|
||||
t.textContent = msg; t.classList.add('show');
|
||||
setTimeout(() => t.classList.remove('show'), 2200);
|
||||
}
|
||||
async function api(path, opts = {}) {
|
||||
const res = await fetch(API + path, {
|
||||
method: opts.method || 'GET',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
|
||||
body: opts.body ? JSON.stringify(opts.body) : undefined,
|
||||
});
|
||||
if (res.status === 401) { doLogout(); throw new Error('未登录或会话已过期'); }
|
||||
return res.json();
|
||||
}
|
||||
function fmtDate(s) { return s ? new Date(s).toLocaleString('zh-CN', { hour12: false }) : '-'; }
|
||||
|
||||
// ---------- 登录 ----------
|
||||
async function doLogin() {
|
||||
const username = document.getElementById('loginUser').value.trim();
|
||||
const password = document.getElementById('loginPass').value;
|
||||
const errEl = document.getElementById('loginError');
|
||||
errEl.textContent = '';
|
||||
if (!username || !password) { errEl.textContent = '请输入用户名和密码'; return; }
|
||||
try {
|
||||
const r = await fetch(API + '/api/admin/login', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
}).then(x => x.json());
|
||||
if (!r.success) { errEl.textContent = r.message || '登录失败'; return; }
|
||||
token = r.data.token;
|
||||
mustChange = !!r.data.mustChangePassword;
|
||||
sessionStorage.setItem('admin_token', token);
|
||||
enterMain(r.data.username, r.data.displayName);
|
||||
if (mustChange) openPwdModal();
|
||||
} catch (e) { errEl.textContent = '网络错误,请稍后重试'; }
|
||||
}
|
||||
function doLogout() {
|
||||
sessionStorage.removeItem('admin_token');
|
||||
token = ''; location.reload();
|
||||
}
|
||||
|
||||
// ---------- 主界面 ----------
|
||||
function enterMain(username, displayName) {
|
||||
document.getElementById('loginPage').classList.add('hidden');
|
||||
document.getElementById('mainPage').classList.remove('hidden');
|
||||
document.getElementById('whoami').textContent = displayName || username || 'admin';
|
||||
loadStats(); loadUsers(1);
|
||||
}
|
||||
async function loadStats() {
|
||||
try {
|
||||
const r = await api('/api/admin/stats');
|
||||
if (!r.success) return;
|
||||
const d = r.data;
|
||||
const cards = [
|
||||
[d.users, '注册用户'], [d.admins, '活动管理员'],
|
||||
[d.activities, '活动总数'], [d.activeActivities, '进行中活动'],
|
||||
];
|
||||
document.getElementById('stats').innerHTML = cards.map(([n, l]) =>
|
||||
`<div class="stat-card"><div class="num">${n}</div><div class="label">${l}</div></div>`).join('');
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
async function loadUsers(p) {
|
||||
if (p < 1) return;
|
||||
page = p;
|
||||
const search = document.getElementById('searchInput').value.trim();
|
||||
const r = await api(`/api/admin/users?page=${page}&pageSize=${pageSize}&search=${encodeURIComponent(search)}`);
|
||||
if (!r.success) { showToast(r.message || '加载失败'); return; }
|
||||
total = r.data.total;
|
||||
const rows = r.data.list;
|
||||
const tbody = document.getElementById('userRows');
|
||||
document.getElementById('emptyTip').classList.toggle('hidden', rows.length > 0);
|
||||
tbody.innerHTML = rows.map(u => `
|
||||
<tr>
|
||||
<td>
|
||||
<div class="nick">
|
||||
${u.avatarUrl
|
||||
? `<img src="${u.avatarUrl.startsWith('http') ? u.avatarUrl : API + u.avatarUrl}" onerror="this.outerHTML='<span class=\\'avatar-fallback\\'>${(u.nickname||'?')[0]}</span>'">`
|
||||
: `<span class="avatar-fallback">${(u.nickname || '?')[0]}</span>`}
|
||||
<span>${escapeHtml(u.nickname || '未设置昵称')}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="muted">${escapeHtml(u.phone || '-')}</td>
|
||||
<td class="muted">创建 ${u.createdCount} / 参加 ${u.joinedCount}</td>
|
||||
<td class="muted">${fmtDate(u.createdAt)}</td>
|
||||
<td>${u.isAdmin ? '<span class="tag tag-admin">管理员</span>' : '<span class="tag tag-user">普通用户</span>'}</td>
|
||||
<td>
|
||||
${u.isAdmin
|
||||
? `<button class="btn btn-sm btn-revoke" onclick="toggleRole('${u.id}','user','${escapeHtml(u.nickname)}')">取消管理员</button>`
|
||||
: `<button class="btn btn-sm btn-grant" onclick="toggleRole('${u.id}','admin','${escapeHtml(u.nickname)}')">设为管理员</button>`}
|
||||
</td>
|
||||
</tr>`).join('');
|
||||
const pages = Math.max(1, Math.ceil(total / pageSize));
|
||||
document.getElementById('pageInfo').textContent = `第 ${page} / ${pages} 页 · 共 ${total} 人`;
|
||||
document.getElementById('prevBtn').disabled = page <= 1;
|
||||
document.getElementById('nextBtn').disabled = page >= pages;
|
||||
}
|
||||
function escapeHtml(s) {
|
||||
return String(s || '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||||
}
|
||||
async function toggleRole(id, role, nickname) {
|
||||
const action = role === 'admin' ? `确定将「${nickname}」设为管理员吗?\n设为管理员后,该用户可以在小程序中创建、编辑、删除活动。`
|
||||
: `确定取消「${nickname}」的管理员权限吗?\n取消后,该用户将无法再创建活动(已有活动不受影响)。`;
|
||||
if (!confirm(action)) return;
|
||||
const r = await api(`/api/admin/users/${id}/role`, { method: 'PUT', body: { role } });
|
||||
if (r.success) { showToast(role === 'admin' ? '已设为管理员' : '已取消管理员'); loadUsers(page); loadStats(); }
|
||||
else showToast(r.message || '操作失败');
|
||||
}
|
||||
|
||||
// ---------- 改密 ----------
|
||||
function openPwdModal(force) {
|
||||
document.getElementById('pwdTip').style.display = force === false ? 'none' : 'block';
|
||||
document.getElementById('pwdModal').classList.add('show');
|
||||
}
|
||||
async function doChangePwd() {
|
||||
const oldPassword = document.getElementById('oldPwd').value;
|
||||
const newPassword = document.getElementById('newPwd').value;
|
||||
const confirm2 = document.getElementById('newPwd2').value;
|
||||
const errEl = document.getElementById('pwdError');
|
||||
errEl.textContent = '';
|
||||
if (newPassword !== confirm2) { errEl.textContent = '两次输入的新密码不一致'; return; }
|
||||
const r = await api('/api/admin/change-password', { method: 'POST', body: { oldPassword, newPassword } });
|
||||
if (r.success) {
|
||||
document.getElementById('pwdModal').classList.remove('show');
|
||||
mustChange = false;
|
||||
showToast('密码已修改');
|
||||
} else errEl.textContent = r.message || '修改失败';
|
||||
}
|
||||
|
||||
// ---------- 启动 ----------
|
||||
(async function init() {
|
||||
if (!token) return;
|
||||
try {
|
||||
const r = await api('/api/admin/me');
|
||||
if (r.success) {
|
||||
mustChange = !!r.data.mustChangePassword;
|
||||
enterMain(r.data.username, r.data.displayName);
|
||||
if (mustChange) openPwdModal();
|
||||
}
|
||||
} catch (e) { /* 已在 api() 里处理登出 */ }
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user