296 lines
11 KiB
PHP
296 lines
11 KiB
PHP
<?php
|
||
/**
|
||
* 班级操行分管理系统 - 管理端管理员管理
|
||
*
|
||
* 开发者: Canglan
|
||
* 联系方式: admin@sea-studio.top
|
||
* 版权归属: Sea Network Technology Studio
|
||
* 许可证: MIT License
|
||
*
|
||
* 版权所有 © Sea Network Technology Studio
|
||
*/
|
||
|
||
require_once __DIR__ . '/../config.php';
|
||
|
||
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'admin') {
|
||
header('Location: /index.php');
|
||
exit();
|
||
}
|
||
|
||
$role = $_SESSION['role'] ?? '';
|
||
if ($role !== '班主任') {
|
||
header('Location: /admin/dashboard.php');
|
||
exit();
|
||
}
|
||
|
||
$page_title = '管理员管理';
|
||
include __DIR__ . '/../includes/header.php';
|
||
?>
|
||
|
||
<?php include __DIR__ . '/../includes/nav.php'; ?>
|
||
|
||
<div class="container">
|
||
<div class="card">
|
||
<div class="action-bar">
|
||
<button class="btn btn-primary" onclick="showAddAdminModal()">添加管理员</button>
|
||
</div>
|
||
<div class="table-wrapper">
|
||
<table class="table">
|
||
<thead>
|
||
<tr><th>用户名</th><th>姓名</th><th>角色</th><th>操作</th></tr>
|
||
</thead>
|
||
<tbody id="adminList"></tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 添加管理员模态框 -->
|
||
<div id="addAdminModal" class="modal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h3>添加管理员</h3>
|
||
<button class="modal-close" onclick="closeModal('addAdminModal')">×</button>
|
||
</div>
|
||
<form onsubmit="event.preventDefault(); submitAddAdmin()">
|
||
<div class="form-group">
|
||
<label>用户名</label>
|
||
<input type="text" id="adminUsername" required placeholder="登录账号">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>姓名</label>
|
||
<input type="text" id="adminRealName" required placeholder="真实姓名">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>密码</label>
|
||
<input type="text" id="adminPassword" placeholder="留空则自动生成">
|
||
<small>自动生成8位随机密码</small>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>角色</label>
|
||
<select id="adminRole" required>
|
||
<option value="">请选择角色</option>
|
||
<option value='班长'>班长</option>
|
||
<option value='学习委员'>学习委员</option>
|
||
<option value='考勤委员'>考勤委员</option>
|
||
<option value='劳动委员'>劳动委员</option>
|
||
<option value='志愿委员'>志愿委员</option>
|
||
</select>
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button type="submit" class="btn btn-primary">添加</button>
|
||
<button type="button" class="btn" onclick="closeModal('addAdminModal')">取消</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 编辑管理员模态框 -->
|
||
<div id="editAdminModal" class="modal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h3>编辑管理员</h3>
|
||
<button class="modal-close" onclick="closeModal('editAdminModal')">×</button>
|
||
</div>
|
||
<form onsubmit="event.preventDefault(); submitEditAdmin()">
|
||
<input type="hidden" id="editAdminUserId">
|
||
<div class="form-group">
|
||
<label>用户名</label>
|
||
<input type="text" id="editAdminUsername" disabled>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>姓名</label>
|
||
<input type="text" id="editAdminRealName" required>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>角色</label>
|
||
<select id="editAdminRole" required>
|
||
<option value="">请选择角色</option>
|
||
<option value='班长'>班长</option>
|
||
<option value='学习委员'>学习委员</option>
|
||
<option value='考勤委员'>考勤委员</option>
|
||
<option value='劳动委员'>劳动委员</option>
|
||
<option value='志愿委员'>志愿委员</option>
|
||
</select>
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button type="submit" class="btn btn-primary">保存</button>
|
||
<button type="button" class="btn" onclick="closeModal('editAdminModal')">取消</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 重置密码模态框 -->
|
||
<div id="resetPasswordModal" class="modal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h3>重置密码</h3>
|
||
<button class="modal-close" onclick="closeModal('resetPasswordModal')">×</button>
|
||
</div>
|
||
<form onsubmit="event.preventDefault(); submitResetPassword()">
|
||
<input type="hidden" id="resetPasswordUserId">
|
||
<div class="form-group">
|
||
<label>管理员</label>
|
||
<input type="text" id="resetPasswordAdminName" disabled>
|
||
</div>
|
||
<div class="form-group">
|
||
<label>新密码</label>
|
||
<input type="text" id="newPassword" required minlength="6" placeholder="请输入新密码(至少6位)">
|
||
<small>密码长度至少6位</small>
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button type="submit" class="btn btn-primary">确认重置</button>
|
||
<button type="button" class="btn" onclick="closeModal('resetPasswordModal')">取消</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
var currentEditUserId = null;
|
||
var currentResetUserId = null;
|
||
|
||
async function loadAdmins() {
|
||
const res = await apiGet('/api/admin/list');
|
||
if (res && res.success) {
|
||
let html = '';
|
||
res.data.admins.forEach(admin => {
|
||
html += `<tr>
|
||
<td>${escapeHtml(admin.username)}</td>
|
||
<td>${escapeHtml(admin.real_name)}</td>
|
||
<td>${escapeHtml(admin.role_type)}</td>
|
||
<td>
|
||
<button class="btn btn-sm btn-primary" onclick="showEditAdminModal(${admin.user_id}, '${escapeHtml(admin.username)}', '${escapeHtml(admin.real_name)}', '${escapeHtml(admin.role_type)}')">编辑</button>
|
||
<button class="btn btn-sm btn-warning" onclick="resetAdminPassword(${admin.user_id}, '${escapeHtml(admin.real_name)}')">重置密码</button>
|
||
<button class="btn btn-sm btn-danger" onclick="deleteAdmin(${admin.user_id}, '${escapeHtml(admin.real_name)}')">删除</button>
|
||
</td>
|
||
</tr>`;
|
||
});
|
||
if (res.data.admins.length === 0) {
|
||
html = '<tr><td colspan="4" style="text-align:center;">暂无管理员</td></tr>';
|
||
}
|
||
document.getElementById('adminList').innerHTML = html;
|
||
}
|
||
}
|
||
|
||
function showAddAdminModal() {
|
||
document.getElementById('addAdminModal').style.display = 'flex';
|
||
document.getElementById('adminUsername').value = '';
|
||
document.getElementById('adminRealName').value = '';
|
||
document.getElementById('adminPassword').value = '';
|
||
document.getElementById('adminRole').value = '';
|
||
}
|
||
|
||
async function submitAddAdmin() {
|
||
const username = document.getElementById('adminUsername').value.trim();
|
||
const realName = document.getElementById('adminRealName').value.trim();
|
||
const password = document.getElementById('adminPassword').value;
|
||
const roleType = document.getElementById('adminRole').value;
|
||
if (!username || !realName || !roleType) {
|
||
showToast('请填写完整信息', 'warning');
|
||
return;
|
||
}
|
||
const res = await apiPost('/api/admin/add', {
|
||
username: username,
|
||
real_name: realName,
|
||
password: password || undefined,
|
||
role_type: roleType
|
||
});
|
||
if (res && res.success) {
|
||
let msg = `管理员 ${res.data.username} 添加成功`;
|
||
if (res.data.password) msg += `,密码: ${res.data.password}`;
|
||
showToast(msg);
|
||
closeModal('addAdminModal');
|
||
loadAdmins();
|
||
} else {
|
||
showToast(res?.message || '添加失败', 'error');
|
||
}
|
||
}
|
||
|
||
function showEditAdminModal(userId, username, realName, roleType) {
|
||
currentEditUserId = userId;
|
||
document.getElementById('editAdminUserId').value = userId;
|
||
document.getElementById('editAdminUsername').value = username;
|
||
document.getElementById('editAdminRealName').value = realName;
|
||
document.getElementById('editAdminRole').value = roleType;
|
||
document.getElementById('editAdminModal').style.display = 'flex';
|
||
}
|
||
|
||
async function submitEditAdmin() {
|
||
if (!currentEditUserId) return;
|
||
|
||
const roleType = document.getElementById('editAdminRole').value;
|
||
if (!roleType) {
|
||
showToast('请选择角色', 'warning');
|
||
return;
|
||
}
|
||
|
||
const res = await apiPut(`/api/admin/update/${currentEditUserId}`, {
|
||
real_name: document.getElementById('editAdminRealName').value,
|
||
role_type: roleType
|
||
});
|
||
|
||
if (res && res.success) {
|
||
showToast('管理员更新成功');
|
||
closeModal('editAdminModal');
|
||
loadAdmins();
|
||
} else {
|
||
showToast(res?.message || '更新失败', 'error');
|
||
}
|
||
}
|
||
|
||
async function deleteAdmin(userId, realName) {
|
||
if (!confirm(`确定要删除管理员 "${realName}" 吗?此操作不可恢复。`)) {
|
||
return;
|
||
}
|
||
|
||
const res = await apiDelete(`/api/admin/delete/${userId}`);
|
||
if (res && res.success) {
|
||
showToast('管理员删除成功');
|
||
loadAdmins();
|
||
} else {
|
||
showToast(res?.message || '删除失败', 'error');
|
||
}
|
||
}
|
||
|
||
function resetAdminPassword(userId, realName) {
|
||
currentResetUserId = userId;
|
||
document.getElementById('resetPasswordUserId').value = userId;
|
||
document.getElementById('resetPasswordAdminName').value = realName;
|
||
document.getElementById('newPassword').value = '';
|
||
document.getElementById('resetPasswordModal').style.display = 'flex';
|
||
}
|
||
|
||
async function submitResetPassword() {
|
||
if (!currentResetUserId) return;
|
||
|
||
const newPassword = document.getElementById('newPassword').value;
|
||
if (!newPassword || newPassword.length < 6) {
|
||
showToast('密码长度至少6位', 'warning');
|
||
return;
|
||
}
|
||
|
||
const res = await apiPost(`/api/admin/reset-password/${currentResetUserId}`, {
|
||
new_password: newPassword
|
||
});
|
||
|
||
if (res && res.success) {
|
||
showToast('密码重置成功');
|
||
closeModal('resetPasswordModal');
|
||
} else {
|
||
showToast(res?.message || '密码重置失败', 'error');
|
||
}
|
||
}
|
||
|
||
function closeModal(modalId) {
|
||
const modal = document.getElementById(modalId);
|
||
if (modal) modal.style.display = 'none';
|
||
}
|
||
|
||
loadAdmins();
|
||
</script>
|
||
<script src="/assets/js/admin.js"></script>
|
||
|
||
<?php include __DIR__ . '/../includes/footer.php'; ?>
|