window.UsersPage = (function() {
'use strict';
let currentPage = 1, pageSize = 20;
async function render() {
if (!Auth.isAdmin) {
UI.toast('无权访问', 'error');
Router.navigate('dashboard');
return '';
}
return buildPage();
}
async function buildPage(keyword, role) {
let params = `?page=${currentPage}&page_size=${pageSize}`;
if (keyword) params += `&keyword=${encodeURIComponent(keyword)}`;
if (role) params += `&role=${encodeURIComponent(role)}`;
let html = '
';
try {
const res = await API.get('/users' + params);
const data = res.data || {};
const items = data.items || [];
if (items.length === 0) {
html += UI.emptyState('暂无用户');
} else {
html += '| ID | 用户名 | 姓名 | 角色 | 管理员 | 状态 | 创建时间 | 操作 |
';
items.forEach(u => {
html += `
| ${u.id} | ${esc(u.username)} | ${esc(u.name)} |
${UI.roleBadge(u.role)} |
${u.is_admin ? UI.badge('是', 'warning') : '-'} |
${u.is_active ? UI.badge('正常', 'success') : UI.badge('禁用', 'danger')} |
${UI.formatDate(u.create_time)} |
|
`;
});
html += '
';
html += UI.renderPagination(data.total, data.page, data.page_size, 'UsersPage.goPage');
}
} catch (e) {
html += `加载失败: ${esc(e.message)}
`;
}
bindEvents();
return html;
}
function bindEvents() {
setTimeout(() => {
const ks = document.getElementById('user-search');
const rf = document.getElementById('user-role-filter');
if (ks) ks.oninput = debounce(() => { currentPage = 1; refresh(); }, 400);
if (rf) rf.onchange = () => { currentPage = 1; refresh(); };
const addBtn = document.getElementById('btn-add-user');
if (addBtn) addBtn.onclick = () => showEdit(null);
}, 0);
}
async function refresh() {
const ks = document.getElementById('user-search');
const rf = document.getElementById('user-role-filter');
const html = await buildPage(ks ? ks.value : '', rf ? rf.value : '');
document.getElementById('content-area').innerHTML = html;
}
async function goPage(p) {
currentPage = p;
await refresh();
}
function showEdit(id) {
const isNew = !id;
let formHtml = '';
formHtml += '';
if (!isNew) {
formHtml += '';
}
UI.showModal(isNew ? '新建用户' : '编辑用户', formHtml, async () => {
if (isNew) {
await API.post('/users', {
name: elVal('f-name'),
password: elVal('f-password'),
role: elVal('f-role'),
});
UI.toast('用户创建成功', 'success');
} else {
const body = {};
const name = elVal('f-name');
if (name) body.name = name;
const role = elVal('f-role');
if (role) body.role = role;
const pwd = elVal('f-password');
if (pwd) body.password = pwd;
const active = elVal('f-is_active');
if (active === '1') body.is_active = true;
else if (active === '0') body.is_active = false;
if (Object.keys(body).length > 0) {
await API.put(`/users/${id}`, body);
UI.toast('用户更新成功', 'success');
}
}
UI.closeModal();
Router.navigate(Router.getCurrentHash());
});
}
function showResetPwd(id) {
let formHtml = '';
UI.showModal('重置密码', formHtml, async () => {
await API.post(`/users/${id}/reset-password`, { new_password: elVal('f-new_password') });
UI.toast('密码重置成功', 'success');
UI.closeModal();
});
}
async function doDelete(id) {
if (!await UI.confirm('确定删除此用户?此操作会解除所有关联数据。')) return;
try {
await API.delete(`/users/${id}`);
UI.toast('用户已删除', 'success');
Router.navigate(Router.getCurrentHash());
} catch (e) { UI.toast(e.message, 'error'); }
}
function elVal(id) { const el = document.getElementById(id); return el ? el.value : ''; }
function esc(s) { return (s || '').replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"'); }
function escAttr(s) { return (s || '').replace(/&/g,'&').replace(/"/g,'"'); }
function debounce(fn, ms) { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), ms); }; }
return { render, showEdit, showResetPwd, doDelete, goPage };
})();