完成菜单权限添加和软件菜单改造

This commit is contained in:
super
2026-04-12 17:15:15 +08:00
parent 7589b4e418
commit e53eaf738e
17 changed files with 814 additions and 297 deletions

View File

@@ -198,6 +198,10 @@
<label>栏目标识</label>
<input type="text" id="columnKey" placeholder="例如home_recommend">
</div>
<div class="form-group" style="min-width:220px;">
<label>菜单路由</label>
<input type="text" id="columnRoutePath" placeholder="例如dedupe-total-data">
</div>
<button class="btn" id="btnAddColumn">新增栏目</button>
</div>
<p class="msg" id="msgColumn"></p>
@@ -210,6 +214,7 @@
<th>ID</th>
<th>栏目名</th>
<th>栏目标识</th>
<th>菜单路由</th>
<th>创建时间</th>
<th>操作</th>
</tr>
@@ -522,6 +527,10 @@
<label>栏目标识</label>
<input type="text" id="editColumnKey" placeholder="栏目标识">
</div>
<div class="form-group">
<label>菜单路由</label>
<input type="text" id="editColumnRoutePath" placeholder="菜单路由">
</div>
<p class="msg" id="msgEditColumn"></p>
<div style="margin-top:16px;display:flex;gap:8px;">
<button class="btn" id="btnSaveColumn">保存</button>
@@ -771,6 +780,71 @@
updateShopManageAccess();
});
}
function applyTabAccess(tabName, canUse) {
var tab = document.querySelector('.tab[data-tab="' + tabName + '"]');
var panel = document.getElementById('panel-' + tabName);
if (tab) tab.style.display = canUse ? '' : 'none';
if (panel) panel.style.display = canUse ? '' : 'none';
if (!canUse && tab && tab.classList.contains('active')) {
tab.classList.remove('active');
if (panel) panel.classList.remove('active');
var usersTab = document.querySelector('.tab[data-tab="users"]');
var usersPanel = document.getElementById('panel-users');
if (usersTab) usersTab.classList.add('active');
if (usersPanel) usersPanel.classList.add('active');
}
}
function updateAdminOnlyAccess() {
var canUse = currentUserRole === 'super_admin';
applyTabAccess('columns', canUse);
applyTabAccess('shop-keys', canUse);
}
function updateDedupeTotalDataAccess() {
var canUse = currentUserRole === 'super_admin' ||
!!currentUserAdminPermissionKeys['admin_dedupe_total_data'] ||
!!currentUserAdminPermissionRoutes['dedupe-total-data'];
applyTabAccess('dedupe-total-data', canUse);
}
function updateShopManageAccess() {
var canUse = currentUserRole === 'super_admin' ||
!!currentUserAdminPermissionKeys['admin_shop_manage'] ||
!!currentUserAdminPermissionRoutes['shop-manage'];
applyTabAccess('shop-manage', canUse);
}
function loadCurrentUserAdminPermissions() {
currentUserAdminPermissionKeys = {};
currentUserAdminPermissionRoutes = {};
updateAdminOnlyAccess();
if (!currentUserId || currentUserRole === 'super_admin') {
updateDedupeTotalDataAccess();
updateShopManageAccess();
return;
}
fetch('/api/admin/user/' + currentUserId + '/column-permissions?menu_type=admin')
.then(function(r) { return r.json(); })
.then(function(res) {
if (!res.success) {
updateAdminOnlyAccess();
updateDedupeTotalDataAccess();
updateShopManageAccess();
return;
}
(res.items || []).forEach(function(item) {
var columnKey = (item.column_key || '').trim();
var routePath = (item.route_path || '').trim();
if (columnKey) currentUserAdminPermissionKeys[columnKey] = true;
if (routePath) currentUserAdminPermissionRoutes[routePath] = true;
});
updateAdminOnlyAccess();
updateDedupeTotalDataAccess();
updateShopManageAccess();
})
.catch(function() {
updateAdminOnlyAccess();
updateDedupeTotalDataAccess();
updateShopManageAccess();
});
}
var allColumnsList = [];
function loadColumnsForPermission() {
fetch('/api/admin/columns')
@@ -1994,6 +2068,115 @@
document.getElementById('editColumnModal').classList.remove('show');
};
function loadColumns() {
fetch('/api/admin/columns')
.then(function(r) { return r.json(); })
.then(function(res) {
var tbody = document.getElementById('columnListBody');
if (!res.success) {
tbody.innerHTML = '<tr><td colspan="6" class="empty-tip">加载失败: ' + (res.error || '') + '</td></tr>';
return;
}
allColumnsList = res.items || [];
if (allColumnsList.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" class="empty-tip">暂无栏目,请先在上方新增</td></tr>';
} else {
tbody.innerHTML = allColumnsList.map(function(c) {
return '<tr><td>' + c.id + '</td><td>' + (c.name || '') + '</td><td>' + (c.column_key || '') + '</td><td>' + (c.route_path || '') + '</td><td>' + (c.created_at || '') + '</td><td>' +
'<button class="btn btn-sm" data-column-edit="' + c.id + '" data-name="' + (c.name || '').replace(/"/g, '&quot;') + '" data-key="' + (c.column_key || '').replace(/"/g, '&quot;') + '" data-route="' + (c.route_path || '').replace(/"/g, '&quot;') + '">编辑</button> ' +
'<button class="btn btn-sm btn-danger" data-column-delete="' + c.id + '" data-name="' + (c.name || '').replace(/"/g, '&quot;') + '">删除</button></td></tr>';
}).join('');
}
bindColumnActions();
})
.catch(function() {
document.getElementById('columnListBody').innerHTML = '<tr><td colspan="6" class="empty-tip">请求失败</td></tr>';
});
}
function bindColumnActions() {
document.querySelectorAll('[data-column-edit]').forEach(function(btn) {
btn.onclick = function() {
document.getElementById('editColumnId').value = btn.dataset.columnEdit || '';
document.getElementById('editColumnName').value = (btn.dataset.name || '').replace(/&quot;/g, '"');
document.getElementById('editColumnKey').value = (btn.dataset.key || '').replace(/&quot;/g, '"');
document.getElementById('editColumnRoutePath').value = (btn.dataset.route || '').replace(/&quot;/g, '"');
document.getElementById('msgEditColumn').textContent = '';
document.getElementById('editColumnModal').classList.add('show');
};
});
document.querySelectorAll('[data-column-delete]').forEach(function(btn) {
btn.onclick = function() {
if (!confirm('确定删除栏目「' + (btn.dataset.name || '').replace(/&quot;/g, '"') + '」吗?')) return;
fetch('/api/admin/column/' + btn.dataset.columnDelete, { method: 'DELETE' })
.then(function(r) { return r.json(); })
.then(function(res) {
if (res.success) { loadColumns(); loadColumnsForPermission(); }
else { alert(res.error || '删除失败'); }
});
};
});
}
document.getElementById('btnAddColumn').onclick = function() {
var name = (document.getElementById('columnName').value || '').trim();
var key = (document.getElementById('columnKey').value || '').trim();
var routePath = (document.getElementById('columnRoutePath').value || '').trim();
var msgEl = document.getElementById('msgColumn');
msgEl.textContent = '';
msgEl.className = 'msg';
if (!name) { msgEl.textContent = '请填写栏目名'; msgEl.classList.add('err'); return; }
if (!key) { msgEl.textContent = '请填写栏目标识'; msgEl.classList.add('err'); return; }
if (!routePath) { msgEl.textContent = '请填写菜单路由'; msgEl.classList.add('err'); return; }
fetch('/api/admin/column', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: name, column_key: key, route_path: routePath, menu_type: 'admin' })
})
.then(function(r) { return r.json(); })
.then(function(res) {
if (res.success) {
msgEl.textContent = res.msg || '新增成功';
msgEl.classList.add('ok');
document.getElementById('columnName').value = '';
document.getElementById('columnKey').value = '';
document.getElementById('columnRoutePath').value = '';
loadColumns();
loadColumnsForPermission();
} else {
msgEl.textContent = res.error || '新增失败';
msgEl.classList.add('err');
}
})
.catch(function() { msgEl.textContent = '请求失败'; msgEl.classList.add('err'); });
};
document.getElementById('btnSaveColumn').onclick = function() {
var cid = document.getElementById('editColumnId').value;
var name = (document.getElementById('editColumnName').value || '').trim();
var key = (document.getElementById('editColumnKey').value || '').trim();
var routePath = (document.getElementById('editColumnRoutePath').value || '').trim();
var msgEl = document.getElementById('msgEditColumn');
msgEl.textContent = '';
msgEl.className = 'msg';
if (!name) { msgEl.textContent = '请填写栏目名'; msgEl.classList.add('err'); return; }
if (!key) { msgEl.textContent = '请填写栏目标识'; msgEl.classList.add('err'); return; }
if (!routePath) { msgEl.textContent = '请填写菜单路由'; msgEl.classList.add('err'); return; }
fetch('/api/admin/column/' + cid, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: name, column_key: key, route_path: routePath, menu_type: 'admin' })
})
.then(function(r) { return r.json(); })
.then(function(res) {
if (res.success) {
document.getElementById('editColumnModal').classList.remove('show');
loadColumns();
loadColumnsForPermission();
} else {
msgEl.textContent = res.error || '保存失败';
msgEl.classList.add('err');
}
});
};
// ========== 分页 ==========
function renderPagination(elId, total, page, pageSize, onPage) {
var el = document.getElementById(elId);