更新:models/routes/services/templates/docs

This commit is contained in:
hmo
2026-04-26 18:02:36 +08:00
parent f7a82ac48a
commit 6abdd49c04
31 changed files with 1480 additions and 676 deletions
+52 -6
View File
@@ -5,7 +5,7 @@
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
<h4><i class="bi bi-person-badge"></i> 用户管理</h4>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#userModal">
<button class="btn btn-primary" onclick="openAddUser()">
<i class="bi bi-plus-circle"></i> 新增用户
</button>
</div>
@@ -17,6 +17,7 @@
<tr>
<th>ID</th>
<th>用户名</th>
<th>姓名</th>
<th>角色</th>
<th>创建时间</th>
<th>操作</th>
@@ -42,6 +43,10 @@
<label class="form-label">用户名</label>
<input type="text" class="form-control" id="username" required>
</div>
<div class="mb-3">
<label class="form-label">姓名</label>
<input type="text" class="form-control" id="userName">
</div>
<div class="mb-3">
<label class="form-label" id="passwordLabel">初始密码</label>
<input type="password" class="form-control" id="password">
@@ -93,6 +98,8 @@
{% block extra_js %}
<script>
const ROLE_LABELS = { 'admin': '管理员', 'user': '普通用户' };
window.pageInit = function() {
loadUsers();
};
@@ -125,17 +132,19 @@ function loadUsers() {
.then(users => {
const tbody = document.querySelector('#usersTable tbody');
if (users.length === 0) {
tbody.innerHTML = '<tr><td colspan="5" class="text-center text-muted">暂无用户</td></tr>';
tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted">暂无用户</td></tr>';
} else {
tbody.innerHTML = users.map(u => `
<tr>
<td>${u.id}</td>
<td>${u.username}</td>
<td>${u.name || '-'}</td>
<td><span class="badge ${u.role === 'admin' ? 'bg-danger' : 'bg-secondary'}">${ROLE_LABELS[u.role]}</span></td>
<td>${u.created_at}</td>
<td>
<button class="btn btn-sm btn-primary me-1" onclick="openEditUser(${u.id}, '${u.username}', '${u.name || ''}', '${u.role}')">编辑</button>
<button class="btn btn-sm btn-warning me-1" onclick="openResetPwd(${u.id})">重置密码</button>
<button class="btn btn-sm btn-danger" onclick="deleteUser(${u.id})">删除</button>
${u.role === 'admin' ? `<button class="btn btn-sm btn-danger" onclick="deleteUser(${u.id})">删除</button>` : ''}
</td>
</tr>
`).join('');
@@ -147,23 +156,60 @@ function loadUsers() {
});
}
// 打开新增用户
function openAddUser() {
document.getElementById('userId').value = '';
document.getElementById('username').value = '';
document.getElementById('username').readOnly = false;
document.getElementById('userName').value = '';
document.getElementById('password').value = '';
document.getElementById('password').parentElement.querySelector('.form-text').textContent = '8位以上,包含大小写字母、数字和特殊字符';
document.getElementById('role').value = 'user';
document.getElementById('role').disabled = false;
document.getElementById('userModalTitle').textContent = '新增用户';
new bootstrap.Modal(document.getElementById('userModal')).show();
}
// 打开编辑用户
function openEditUser(id, username, name, role) {
document.getElementById('userId').value = id;
document.getElementById('username').value = username;
document.getElementById('username').readOnly = true;
document.getElementById('userName').value = name;
document.getElementById('password').value = '';
document.getElementById('password').parentElement.querySelector('.form-text').textContent = '不填则保持不变';
document.getElementById('role').value = role;
document.getElementById('role').disabled = true;
document.getElementById('userModalTitle').textContent = '编辑用户';
new bootstrap.Modal(document.getElementById('userModal')).show();
}
// 保存用户
document.getElementById('saveUserBtn').onclick = () => {
const id = document.getElementById('userId').value;
const username = document.getElementById('username').value.trim();
const name = document.getElementById('userName').value.trim();
const password = document.getElementById('password').value;
const role = document.getElementById('role').value;
if (!username || !password) {
alert('请填写完整');
if (!username) {
alert('请填写用户名');
return;
}
if (!id && !password) {
alert('请填写密码');
return;
}
const method = id ? 'PUT' : 'POST';
const body = { username, role };
if (name) body.name = name;
if (password) body.password = password;
fetch('/api/users' + (id ? '/' + id : ''), {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password, role })
body: JSON.stringify(body)
}).then(r => r.json()).then(data => {
if (data.error) {
showToast(data.error);