298 lines
9.6 KiB
JavaScript
298 lines
9.6 KiB
JavaScript
/**
|
|
* 商家后台侧边栏通用脚本
|
|
* 提供侧边栏的HTML结构和交互逻辑
|
|
*/
|
|
|
|
// 生成侧边栏HTML
|
|
function generateSidebarHTML(currentPage) {
|
|
const menuItems = [
|
|
{ href: '/merchant/dashboard.html', icon: 'bi-speedometer2', text: '首页', page: 'dashboard' },
|
|
{ href: '/merchant/products.html', icon: 'bi-box-seam', text: '商品管理', page: 'products' },
|
|
{ href: '/merchant/orders.html', icon: 'bi-receipt-cutoff', text: '订单管理', page: 'orders' },
|
|
{ href: '/merchant/inventory.html', icon: 'bi-archive', text: '库存管理', page: 'inventory' },
|
|
{ href: '/merchant/statistics.html', icon: 'bi-bar-chart', text: '数据统计', page: 'statistics' },
|
|
{ href: '/merchant/announcements.html', icon: 'bi-megaphone', text: '公告管理', page: 'announcements' },
|
|
{ href: '/merchant/messages.html', icon: 'bi-chat-dots', text: '留言管理', page: 'messages' },
|
|
{ href: '/merchant/reviews.html', icon: 'bi-star', text: '评价管理', page: 'reviews' },
|
|
{ href: '/merchant/users.html', icon: 'bi-people', text: '用户管理', page: 'users' }
|
|
];
|
|
|
|
const menuHTML = menuItems.map(item => {
|
|
const activeClass = item.page === currentPage ? 'active' : '';
|
|
return `
|
|
<li class="nav-item">
|
|
<a class="nav-link ${activeClass}" href="${item.href}" data-page="${item.page}">
|
|
<i class="bi ${item.icon}"></i>
|
|
<span class="nav-text">${item.text}</span>
|
|
</a>
|
|
</li>
|
|
`;
|
|
}).join('');
|
|
|
|
return `
|
|
<!-- 侧边栏导航 -->
|
|
<div class="sidebar" id="sidebar">
|
|
<div class="sidebar-header">
|
|
<div class="d-flex align-items-center">
|
|
<i class="bi bi-shop fs-4 me-2"></i>
|
|
<span class="sidebar-brand">商家后台</span>
|
|
</div>
|
|
<button class="btn btn-link text-white p-0 d-md-none" id="sidebarCloseBtn">
|
|
<i class="bi bi-x-lg"></i>
|
|
</button>
|
|
</div>
|
|
<nav class="sidebar-nav">
|
|
<ul class="nav flex-column">
|
|
${menuHTML}
|
|
</ul>
|
|
</nav>
|
|
<div class="sidebar-footer">
|
|
<a href="/merchant/logout" class="btn btn-outline-light w-100">
|
|
<i class="bi bi-box-arrow-right"></i>
|
|
<span class="ms-2">退出登录</span>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 侧边栏遮罩层(移动端) -->
|
|
<div class="sidebar-overlay" id="sidebarOverlay"></div>
|
|
|
|
<!-- 顶部导航栏(移动端) -->
|
|
<nav class="top-navbar d-md-none">
|
|
<div class="container-fluid d-flex align-items-center">
|
|
<button class="btn btn-link text-white p-0" id="sidebarToggleBtn">
|
|
<i class="bi bi-list fs-4"></i>
|
|
</button>
|
|
<span class="ms-3 text-white fw-bold">商家后台</span>
|
|
</div>
|
|
</nav>
|
|
`;
|
|
}
|
|
|
|
// 添加侧边栏CSS样式
|
|
function addSidebarStyles() {
|
|
if (document.getElementById('sidebar-styles')) return;
|
|
|
|
const style = document.createElement('style');
|
|
style.id = 'sidebar-styles';
|
|
style.textContent = `
|
|
/* 侧边栏样式 */
|
|
.sidebar {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 260px;
|
|
height: 100vh;
|
|
background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
|
|
color: #fff;
|
|
z-index: 1050;
|
|
display: flex;
|
|
flex-direction: column;
|
|
transition: transform 0.3s ease;
|
|
box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
|
|
}
|
|
|
|
.sidebar-header {
|
|
padding: 1.5rem;
|
|
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
|
|
.sidebar-brand {
|
|
font-size: 1.25rem;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.sidebar-nav {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 1rem 0;
|
|
}
|
|
|
|
.sidebar-nav .nav-link {
|
|
color: rgba(255, 255, 255, 0.8);
|
|
padding: 0.75rem 1.5rem;
|
|
transition: all 0.3s;
|
|
border-left: 3px solid transparent;
|
|
display: flex;
|
|
align-items: center;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.sidebar-nav .nav-link:hover {
|
|
background-color: rgba(255, 255, 255, 0.1);
|
|
color: #fff;
|
|
border-left-color: #0d6efd;
|
|
}
|
|
|
|
.sidebar-nav .nav-link.active {
|
|
background-color: rgba(13, 110, 253, 0.2);
|
|
color: #fff;
|
|
border-left-color: #0d6efd;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.sidebar-nav .nav-link i {
|
|
width: 24px;
|
|
font-size: 1.1rem;
|
|
margin-right: 0.75rem;
|
|
}
|
|
|
|
.sidebar-nav .nav-text {
|
|
flex: 1;
|
|
}
|
|
|
|
.sidebar-footer {
|
|
padding: 1rem;
|
|
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
|
}
|
|
|
|
/* 移动端样式 */
|
|
@media (max-width: 767.98px) {
|
|
.sidebar {
|
|
transform: translateX(-100%);
|
|
}
|
|
|
|
.sidebar.show {
|
|
transform: translateX(0);
|
|
}
|
|
|
|
.sidebar-overlay {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
bottom: 0;
|
|
background-color: rgba(0, 0, 0, 0.5);
|
|
z-index: 1049;
|
|
display: none;
|
|
transition: opacity 0.3s;
|
|
}
|
|
|
|
.sidebar-overlay.show {
|
|
display: block;
|
|
}
|
|
|
|
.top-navbar {
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
right: 0;
|
|
height: 56px;
|
|
background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
|
|
z-index: 1048;
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
}
|
|
|
|
body {
|
|
padding-top: 56px;
|
|
}
|
|
}
|
|
|
|
/* 桌面端主内容区域 */
|
|
@media (min-width: 768px) {
|
|
.main-content {
|
|
margin-left: 260px;
|
|
min-height: 100vh;
|
|
}
|
|
}
|
|
|
|
/* 滚动条样式 */
|
|
.sidebar-nav::-webkit-scrollbar {
|
|
width: 6px;
|
|
}
|
|
|
|
.sidebar-nav::-webkit-scrollbar-track {
|
|
background: rgba(255, 255, 255, 0.05);
|
|
}
|
|
|
|
.sidebar-nav::-webkit-scrollbar-thumb {
|
|
background: rgba(255, 255, 255, 0.2);
|
|
border-radius: 3px;
|
|
}
|
|
|
|
.sidebar-nav::-webkit-scrollbar-thumb:hover {
|
|
background: rgba(255, 255, 255, 0.3);
|
|
}
|
|
`;
|
|
document.head.appendChild(style);
|
|
}
|
|
|
|
// 初始化侧边栏交互
|
|
function initSidebar() {
|
|
const sidebar = document.getElementById('sidebar');
|
|
const sidebarOverlay = document.getElementById('sidebarOverlay');
|
|
const sidebarToggleBtn = document.getElementById('sidebarToggleBtn');
|
|
const sidebarCloseBtn = document.getElementById('sidebarCloseBtn');
|
|
|
|
if (!sidebar) return;
|
|
|
|
// 移动端切换侧边栏
|
|
if (sidebarToggleBtn) {
|
|
sidebarToggleBtn.addEventListener('click', function() {
|
|
sidebar.classList.add('show');
|
|
if (sidebarOverlay) sidebarOverlay.classList.add('show');
|
|
document.body.style.overflow = 'hidden';
|
|
});
|
|
}
|
|
|
|
// 关闭侧边栏
|
|
function closeSidebar() {
|
|
sidebar.classList.remove('show');
|
|
if (sidebarOverlay) sidebarOverlay.classList.remove('show');
|
|
document.body.style.overflow = '';
|
|
}
|
|
|
|
if (sidebarCloseBtn) {
|
|
sidebarCloseBtn.addEventListener('click', closeSidebar);
|
|
}
|
|
|
|
if (sidebarOverlay) {
|
|
sidebarOverlay.addEventListener('click', closeSidebar);
|
|
}
|
|
|
|
// 点击导航项时,移动端自动关闭侧边栏
|
|
const navLinks = document.querySelectorAll('.sidebar-nav .nav-link');
|
|
navLinks.forEach(link => {
|
|
link.addEventListener('click', function() {
|
|
if (window.innerWidth < 768) {
|
|
closeSidebar();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// 初始化侧边栏(自动检测当前页面)
|
|
function initMerchantSidebar() {
|
|
addSidebarStyles();
|
|
|
|
// 检测当前页面
|
|
const path = window.location.pathname;
|
|
let currentPage = 'dashboard';
|
|
|
|
if (path.includes('products')) currentPage = 'products';
|
|
else if (path.includes('orders')) currentPage = 'orders';
|
|
else if (path.includes('inventory')) currentPage = 'inventory';
|
|
else if (path.includes('statistics')) currentPage = 'statistics';
|
|
else if (path.includes('announcements')) currentPage = 'announcements';
|
|
else if (path.includes('messages')) currentPage = 'messages';
|
|
else if (path.includes('reviews')) currentPage = 'reviews';
|
|
else if (path.includes('users')) currentPage = 'users';
|
|
|
|
// 查找并替换旧的导航栏
|
|
const oldNav = document.querySelector('nav.navbar');
|
|
if (oldNav) {
|
|
oldNav.outerHTML = generateSidebarHTML(currentPage);
|
|
initSidebar();
|
|
}
|
|
}
|
|
|
|
// 页面加载完成后初始化
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', initMerchantSidebar);
|
|
} else {
|
|
initMerchantSidebar();
|
|
}
|