fix(admin-web): 角色变更改用页内确认弹窗,修复点击无响应
- confirm() 被浏览器"阻止更多对话框"后静默返回 false,PUT 请求从未发出(nginx 全日志零条 PUT 实证) - 内联 onclick 移除昵称参数,改传 UUID + userCache 查昵称,消除昵称含引号时的 JS 语法错误 - 确认按钮加防重复提交,失败分支补 toast 提示
This commit is contained in:
@@ -151,6 +151,18 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 角色变更确认弹窗 -->
|
||||
<div class="modal-mask" id="roleModal">
|
||||
<div class="modal">
|
||||
<h3 id="roleTitle">确认操作</h3>
|
||||
<div id="roleMsg" style="font-size:14px;line-height:1.7;white-space:pre-line;"></div>
|
||||
<div style="display:flex;gap:10px;justify-content:flex-end;margin-top:20px;">
|
||||
<button class="btn" onclick="closeRoleModal()">取消</button>
|
||||
<button class="btn btn-primary" id="roleConfirmBtn" onclick="confirmRole()">确认</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="toast" id="toast"></div>
|
||||
|
||||
<script>
|
||||
@@ -158,6 +170,7 @@ const API = location.origin;
|
||||
let token = sessionStorage.getItem('admin_token') || '';
|
||||
let mustChange = false;
|
||||
let page = 1, pageSize = 20, total = 0;
|
||||
const userCache = {};
|
||||
|
||||
// ---------- 工具 ----------
|
||||
function showToast(msg) {
|
||||
@@ -229,6 +242,7 @@ async function loadUsers(p) {
|
||||
if (!r.success) { showToast(r.message || '加载失败'); return; }
|
||||
total = r.data.total;
|
||||
const rows = r.data.list;
|
||||
rows.forEach(u => { userCache[u.id] = u.nickname || '未设置昵称'; });
|
||||
const tbody = document.getElementById('userRows');
|
||||
document.getElementById('emptyTip').classList.toggle('hidden', rows.length > 0);
|
||||
tbody.innerHTML = rows.map(u => `
|
||||
@@ -247,8 +261,8 @@ async function loadUsers(p) {
|
||||
<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>`}
|
||||
? `<button class="btn btn-sm btn-revoke" onclick="toggleRole('${u.id}','user')">取消管理员</button>`
|
||||
: `<button class="btn btn-sm btn-grant" onclick="toggleRole('${u.id}','admin')">设为管理员</button>`}
|
||||
</td>
|
||||
</tr>`).join('');
|
||||
const pages = Math.max(1, Math.ceil(total / pageSize));
|
||||
@@ -259,13 +273,39 @@ async function loadUsers(p) {
|
||||
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 || '操作失败');
|
||||
let pendingRole = null; // { id, role }
|
||||
function toggleRole(id, role) {
|
||||
const nickname = userCache[id] || id;
|
||||
pendingRole = { id, role };
|
||||
document.getElementById('roleTitle').textContent = role === 'admin' ? '设为管理员' : '取消管理员';
|
||||
document.getElementById('roleMsg').textContent = role === 'admin'
|
||||
? `确定将「${nickname}」设为管理员吗?\n设为管理员后,该用户可以在小程序中创建、编辑、删除活动。`
|
||||
: `确定取消「${nickname}」的管理员权限吗?\n取消后,该用户将无法再创建活动(已有活动不受影响)。`;
|
||||
document.getElementById('roleModal').classList.add('show');
|
||||
}
|
||||
function closeRoleModal() {
|
||||
document.getElementById('roleModal').classList.remove('show');
|
||||
pendingRole = null;
|
||||
}
|
||||
async function confirmRole() {
|
||||
if (!pendingRole) return;
|
||||
const { id, role } = pendingRole;
|
||||
const btn = document.getElementById('roleConfirmBtn');
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const r = await api(`/api/admin/users/${id}/role`, { method: 'PUT', body: { role } });
|
||||
if (r.success) {
|
||||
closeRoleModal();
|
||||
showToast(role === 'admin' ? '已设为管理员' : '已取消管理员');
|
||||
loadUsers(page); loadStats();
|
||||
} else {
|
||||
showToast(r.message || '操作失败');
|
||||
}
|
||||
} catch (e) {
|
||||
showToast('网络错误,请稍后重试');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 改密 ----------
|
||||
|
||||
Reference in New Issue
Block a user