XH_Digital_Management/templates/authority_modal.html

226 lines
8.5 KiB
HTML
Raw Normal View History

2024-06-04 18:45:11 +08:00
<div class="modal" id="userPermissionModal" tabindex="-1" aria-labelledby="userPermissionModalLabel" aria-hidden="true">
2024-06-05 14:44:44 +08:00
<div class="modal-dialog modal-lg">
<div class="modal-content">
2024-06-05 11:34:45 +08:00
<div class="modal-header">
<h5 class="modal-title" id="userPermissionModalLabel">用户权限管理</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
2024-06-05 14:44:44 +08:00
<table class="table" id="permissionsTable">
2024-06-05 11:34:45 +08:00
<thead>
<tr>
<th>资源分组</th>
<th>资源名称</th>
2024-06-05 11:40:48 +08:00
<th>新增</th>
<th>删除</th>
<th>修改</th>
<th>查看</th>
2024-06-05 11:34:45 +08:00
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="modal-footer">
2024-06-05 14:44:44 +08:00
<div id="pagination" class="pagination">
<span id="totalData" class="mx-2"></span>
<span id="pageInfo" class="mx-2"></span>
<button id="prevPage" class="btn btn-secondary" type="button">上一页</button>
<button id="nextPage" class="btn btn-secondary" type="button">下一页</button>
</div>
2024-06-05 16:31:01 +08:00
<button type="button" class="btn btn-secondary" id="refreshPermissions" data-user-id="">刷新权限</button>
2024-06-05 15:26:27 +08:00
<button type="button" class="btn btn-primary" id="savePermissions" data-user-id="">保存更改</button>
2024-06-05 11:34:45 +08:00
</div>
</div>
</div>
</div>
<script>
2024-06-05 15:26:27 +08:00
// 获取 CSRF 令牌
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
2024-06-05 11:34:45 +08:00
$(document).ready(function() {
2024-06-05 14:44:44 +08:00
const itemsPerPage = 10;
let currentPage = 1;
let permissionsData = [];
2024-06-05 15:39:26 +08:00
let modifiedPermissions = {};
2024-06-05 16:31:01 +08:00
let userId = null
2024-06-05 14:44:44 +08:00
2024-06-05 17:31:10 +08:00
// 渲染权限表格
2024-06-05 14:44:44 +08:00
function renderTable(page) {
const start = (page - 1) * itemsPerPage;
const end = start + itemsPerPage;
const paginatedData = permissionsData.slice(start, end);
const tableBody = $('#permissionsTable tbody');
tableBody.empty();
paginatedData.forEach(perm => {
tableBody.append(`
<tr>
<td>${perm.resource_group}</td>
<td>${perm.resource}</td>
2024-06-05 15:26:27 +08:00
<td><input type="checkbox" name="add_permission" data-id="${perm.id}" ${perm.add ? 'checked' : ''}></td>
<td><input type="checkbox" name="delete_permission" data-id="${perm.id}" ${perm.remove ? 'checked' : ''}></td>
<td><input type="checkbox" name="edit_permission" data-id="${perm.id}" ${perm.change ? 'checked' : ''}></td>
<td><input type="checkbox" name="view_permission" data-id="${perm.id}" ${perm.view ? 'checked' : ''}></td>
2024-06-05 14:44:44 +08:00
</tr>
`);
});
$('#pageInfo').text('第 ' + currentPage + ' 页, 共 ' + Math.ceil(permissionsData.length / itemsPerPage) + ' 页');
}
2024-06-05 17:31:10 +08:00
// 渲染分页
2024-06-05 14:44:44 +08:00
function renderPagination() {
const totalPages = Math.ceil(permissionsData.length / itemsPerPage);
const pagination = $('#pagination .page-numbers');
pagination.empty();
$('#totalData').text('总数: ' + permissionsData.length);
$('#pageInfo').text('第 ' + currentPage + ' 页, 共 ' + totalPages + ' 页');
for (let i = 1; i <= totalPages; i++) {
pagination.append(`<span class="page-number" data-page="${i}">${i}</span>`);
}
2024-06-05 15:26:27 +08:00
$('.page-number').removeClass('active');
$(`.page-number[data-page="${currentPage}"]`).addClass('active');
2024-06-05 14:44:44 +08:00
}
2024-06-05 17:31:10 +08:00
// 监听当前页
2024-06-05 14:44:44 +08:00
$('#pagination').on('click', '.page-number', function() {
currentPage = $(this).data('page');
renderTable(currentPage);
renderPagination();
});
2024-06-05 17:31:10 +08:00
// 监听上一页
2024-06-05 14:44:44 +08:00
$('#prevPage').on('click', function() {
if (currentPage > 1) {
currentPage--;
renderTable(currentPage);
renderPagination();
}
});
2024-06-05 17:31:10 +08:00
// 监听下一页
2024-06-05 14:44:44 +08:00
$('#nextPage').on('click', function() {
const totalPages = Math.ceil(permissionsData.length / itemsPerPage);
if (currentPage < totalPages) {
currentPage++;
renderTable(currentPage);
renderPagination();
}
});
2024-06-05 17:31:10 +08:00
// 渲染用户权限列表框
2024-06-05 11:34:45 +08:00
$('.btn-outline-primary').on('click', function() {
2024-06-05 16:31:01 +08:00
userId = $(this).closest('tr').data('id');
2024-06-05 15:26:27 +08:00
$('#savePermissions').data('user-id', userId); // 确保在模态框显示时设置用户ID
2024-06-05 11:34:45 +08:00
const url = "{% url 'get_user_existing_permissions' 0 %}".replace('0', userId);
$.ajax({
url: url,
type: 'GET',
success: function(data) {
2024-06-05 14:44:44 +08:00
permissionsData = data.permissions;
currentPage = 1;
renderTable(currentPage);
renderPagination();
2024-06-05 11:34:45 +08:00
$('#userPermissionModal').modal('show');
},
error: function(xhr, status, error) {
alert('获取权限数据失败');
}
});
});
2024-06-05 15:26:27 +08:00
2024-06-05 15:39:26 +08:00
// 监听权限复选框的更改
$('#permissionsTable').on('change', 'input[type="checkbox"]', function() {
const permId = $(this).data('id');
const permType = $(this).attr('name');
if (!modifiedPermissions[permId]) {
modifiedPermissions[permId] = {};
}
modifiedPermissions[permId][permType] = $(this).is(':checked');
});
2024-06-05 17:31:10 +08:00
// 保存用户权限更改
2024-06-05 15:26:27 +08:00
$('#savePermissions').on('click', function() {
const url = "{% url 'save_user_permissions' 0 %}".replace('0', userId);
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
2024-06-05 15:39:26 +08:00
data: JSON.stringify({ permissions: modifiedPermissions }),
2024-06-05 15:26:27 +08:00
headers: {
'X-CSRFToken': csrftoken // 添加CSRF令牌
},
success: function(response) {
2024-06-05 16:31:01 +08:00
$('#userPermissionModal').modal('hide');
2024-06-05 17:31:10 +08:00
showAlert('success', '权限保存成功');
2024-06-05 15:26:27 +08:00
},
error: function(xhr, status, error) {
2024-06-05 17:31:10 +08:00
showAlert('danger', '保存权限时出错');
2024-06-05 15:26:27 +08:00
}
});
});
2024-06-05 16:31:01 +08:00
2024-06-05 17:31:10 +08:00
// 刷新用户权限(重新继承用户所在组权限)
2024-06-05 16:31:01 +08:00
$('#refreshPermissions').on('click', function() {
const url = "{% url 'refresh_user_permissions' 0 %}".replace('0', userId);
$.ajax({
url: url,
type: 'POST',
2024-06-05 17:31:10 +08:00
headers: {'X-CSRFToken': csrftoken},
2024-06-05 16:31:01 +08:00
success: function(response) {
alert('权限已刷新');
$.ajax({
url: "{% url 'get_user_existing_permissions' 0 %}".replace('0', userId),
type: 'GET',
success: function(data) {
permissionsData = data.permissions;
modifiedPermissions = {}; // 重置修改的权限项
currentPage = 1;
renderTable(currentPage);
renderPagination();
$('#userPermissionModal').modal('show');
},
error: function(xhr, status, error) {
alert('获取权限数据失败');
}
});
},
error: function(xhr, status, error) {
alert('刷新权限时出错');
}
});
});
2024-06-05 17:31:10 +08:00
function showAlert(type, message) {
const alertHtml = `
<div class="alert alert-${type} alert-dismissible fade show" role="alert">
<strong>操作提示:</strong> ${message}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>`;
$('#alertContainer').html(alertHtml);
}
2024-06-05 11:34:45 +08:00
});
</script>
2024-06-05 15:26:27 +08:00