/**
* ============================================
* GX-WMS 左侧侧边栏导航组件
* 用法:
* 1. 页面
引入 nav.css:
* 2. 顶部加一个容器:
* 3. 主内容区域包裹在 中
* 4. 页面底部引入本JS,自动初始化
*
* 维护点:仅修改本文件的 MENU_GROUPS 配置
* ============================================
*/
// ---------- 应用根路径自动探测 ----------
// 支持 IIS 子目录部署(如 /WebApi,名称可变)和 VS 调试(网站根目录)两种场景。
// 原理:根据本脚本自身 URL(<应用根>/View/common/nav.js)反推出应用根前缀。
// - 部署在子目录时自动得到 '/WebApi' 之类前缀
// - VS 调试 / 根目录部署时为空字符串,不影响原行为
var APP_BASE = '';
(function () {
try {
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length - 1; i >= 0; i--) {
var src = scripts[i].getAttribute('src') || '';
if (src.indexOf('nav.js') >= 0) {
var a = document.createElement('a');
a.href = src;
var p = a.pathname.replace(/\\/g, '/');
var idx = p.indexOf('/View/common/nav.js');
if (idx >= 0) APP_BASE = p.substring(0, idx);
break;
}
}
} catch (e) { APP_BASE = ''; }
window.APP_BASE = APP_BASE;
})();
// ---------- 菜单配置(唯一维护点) ----------
// 顶级项:{ title, icon, role, children? } 有 children = 分组
// 子项:{ title, href, role }
// href 使用绝对路径(以 / 开头),渲染时会自动加上应用根前缀 APP_BASE
var MENU_GROUPS = [
{ title: '首页', icon: '🏠', href: '/home.html', hideOnHome: true },
{
title: '生产管理', icon: '🏭', role: 'Purchase01',
children: [
{ title: 'BOM主数据', href: '/View/Delivery/bom_master.html', icon: '🧩', role: 'Purchase01' },
{ title: '生产排程计划', href: '/View/Delivery/schedule.html', icon: '📅', role: 'Purchase01' },
{ title: '生产排程看板', href: '/View/Delivery/schedule_board.html', icon: '📊', role: 'Purchase01' },
{ title: '线边仓库存', href: '/View/Delivery/production_delivery.html', icon: '🏬', role: 'Purchase01' },
{ title: 'MES追溯看板', href: '/View/MES/mes_dashboard.html', icon: '🔍', role: 'Produce02' }
]
},
{
title: '采购管理', icon: '📋', role: ['YF01', 'Purchase01'],
children: [
{
title: '采购申请管理', icon: '📦', role: ['YF01', 'Purchase01'],
children: [
{ title: '采购申请管理', href: '/procurement.html', icon: '📋', role: 'Purchase01' },
{ title: '采购看板', href: '/procurement_dashboard.html', icon: '📊', role: 'Purchase01' },
{ title: '采购物料交付状态看板', href: '/delivery_dashboard.html', icon: '🚚', role: 'Purchase01' },
{ title: '研发采购看板', href: '/dashboard.html', icon: '🔬', role: 'YF01' },
{ title: '研发采购全链路看板', href: '/procurement_board_list.html', icon: '📈', role: ['YF01', 'Purchase01'] },
{ title: '研发订单考核时效看板', href: '/procurement_audit_leadtime.html', icon: '⏱️', role: ['YF01', 'Purchase01'] }
]
},
{
title: '采购物料交付', icon: '🚚', role: 'Purchase01',
children: [
{ title: '采购物料交付', href: '/View/Delivery/material_delivery.html', icon: '🚚', role: 'Purchase01' },
{ title: '采购物料交付计划看板', href: '/View/Delivery/delivery_dashboard.html', icon: '📊', role: 'Purchase01' }
]
}
]
},
{
title: 'CRM', icon: '📊', role: 'CRM01',
children: [
{ title: '金蝶-CRM产品同步', href: '/index.html', icon: '🔄', role: 'CRM01' },
{ title: '金蝶-CRM销售订单新变更单同步', href: '/crm_sale_order_sync.html', icon: '📄', role: 'CRM01' }
]
}
];
// ============================================
// 通用逻辑,一般无需修改
// ============================================
function getLoginUser() {
try {
var raw = sessionStorage.getItem('gx_user');
if (!raw) return null;
return JSON.parse(raw);
} catch (e) { return null; }
}
function hasRole(role) {
if (!role) return true;
// 支持 role 为数组:任一角色命中即通过(一个功能可分给多个角色)
if (Array.isArray(role)) {
for (var i = 0; i < role.length; i++) {
if (hasRole(role[i])) return true;
}
return false;
}
var user = getLoginUser();
if (!user) return false;
var rn = user.roleNumbers;
if (Array.isArray(rn)) {
if (rn.indexOf(role) >= 0) return true;
for (var i = 0; i < rn.length; i++) {
if (String(rn[i]).toUpperCase() === String(role).toUpperCase()) return true;
}
}
var rs = user.roles;
if (Array.isArray(rs)) {
for (var j = 0; j < rs.length; j++) {
var item = rs[j] || {};
if (String(item.roleNumber || '').toUpperCase() === String(role).toUpperCase()) return true;
if (String(item.roleName || '').toUpperCase() === String(role).toUpperCase()) return true;
}
}
return false;
}
function isActive(href) {
try {
var u = new URL(APP_BASE + href, window.location.origin);
return window.location.pathname.replace(/\\/g, '/').indexOf(u.pathname) >= 0;
} catch (e) { return false; }
}
function getLoginPath() {
// 用 APP_BASE(自动探测的应用根前缀)拼接登录页地址:
// - VS 调试/根目录部署:APP_BASE 为空,返回 '/login.html'
// - 子目录部署(如 /WebApi):返回 '/WebApi/login.html'
return APP_BASE + '/login.html';
}
function logout() {
sessionStorage.removeItem('gx_user');
window.location.href = getLoginPath();
}
/**
* 递归判断菜单项(含子孙)是否有激活项
*/
function isItemActive(item) {
if (item.href && isActive(item.href)) return true;
if (item.children) {
for (var i = 0; i < item.children.length; i++) {
if (isItemActive(item.children[i])) return true;
}
}
return false;
}
/**
* 递归判断菜单项是否有可见(有权限)的子孙
*/
function hasVisibleItem(item) {
if (!hasRole(item.role)) return false;
if (!item.children || !item.children.length) return true;
for (var i = 0; i < item.children.length; i++) {
if (hasVisibleItem(item.children[i])) return true;
}
return false;
}
/**
* 递归渲染子菜单项(支持任意层级)
* items: 菜单配置数组;depth: 当前层级(从1开始)
*/
function renderSubItems(items, depth) {
var html = '';
for (var i = 0; i < items.length; i++) {
var it = items[i];
if (!hasVisibleItem(it)) continue;
if (it.children && it.children.length) {
// 子分组(可继续递归)
var subActive = isItemActive(it) ? ' expanded' : '';
var subRotate = isItemActive(it) ? ' rotate' : '';
html += '';
} else {
// 叶子项
var activeCls = isActive(it.href) ? ' active' : '';
html += '';
}
}
return html;
}
/**
* 切换子分组展开/收起
*/
function toggleSubGroup(titleEl) {
var sub = titleEl.parentNode;
if (!sub) return;
sub.classList.toggle('expanded');
var arrow = titleEl.querySelector('.menu-arrow');
if (arrow) arrow.classList.toggle('rotate');
}
/**
* 渲染侧边栏
*/
function renderSidebar() {
var user = getLoginUser();
if (!user) {
if (window.location.href.indexOf('login.html') < 0) {
window.location.href = getLoginPath();
}
return;
}
var html = '';
// 收缩按钮
html += '';
// 用户信息区
var name = user.userName || user.userId || user.name || '用户';
html += '';
// 找出当前页面所在分组(用于默认展开)
var activeGroupIdx = -1;
var activeChildIdx = -1;
// 渲染菜单
html += '';
var el = document.getElementById('sidebar');
if (el) el.innerHTML = html;
// 主体内容左边距
document.body.classList.add('with-sidebar');
// 恢复折叠状态
if (localStorage.getItem('gx_sidebar_collapsed') === '1') {
document.body.classList.add('sidebar-collapsed');
}
}
/**
* 切换侧边栏折叠/展开
*/
function toggleSidebar() {
document.body.classList.toggle('sidebar-collapsed');
var collapsed = document.body.classList.contains('sidebar-collapsed');
if (collapsed) localStorage.setItem('gx_sidebar_collapsed', '1');
else localStorage.removeItem('gx_sidebar_collapsed');
}
/**
* 切换分组展开/折叠
*/
function toggleGroup(idx) {
// 折叠状态下点击分组:先展开侧边栏,再展开该分组
if (document.body.classList.contains('sidebar-collapsed')) {
toggleSidebar();
// 展开侧边栏后延迟让 CSS 过渡完成,再展开分组
var el = document.querySelector('.menu-group[data-group="' + idx + '"]');
if (el) el.classList.add('expanded');
return;
}
var el = document.querySelector('.menu-group[data-group="' + idx + '"]');
if (!el) return;
el.classList.toggle('expanded');
var arrow = el.querySelector('.menu-arrow');
if (arrow) arrow.classList.toggle('rotate');
}
function initNav() {
renderSidebar();
}
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', function () { initNav(); });
} else {
window.onload = function () { initNav(); };
}