/** * EMA 主框架脚本 */ layui.use(['element', 'layer'], function () { var element = layui.element; var layer = layui.layer; var $ = layui.jquery; // 检查登录状态 Common.checkLogin(); // 获取用户信息 var userInfo = Common.getUserInfo(); if (userInfo) { $('#userName').text(userInfo.name || userInfo.account || '用户'); if (userInfo.avatar) { $('#userInfo img').attr('src', userInfo.avatar); } } // 加载菜单 loadMenus(); // 初始化标签页点击事件 initTabEvents(); /** * 加载菜单 */ function loadMenus() { Common.get(Config.api.menuTree, function (res) { if (res.code === 200 || res.code === 0) { var menus = res.data || []; var html = buildMenuHtml(menus); $('#navMenu').html(html); element.render('nav'); // 绑定菜单点击事件 bindMenuEvents(); } }); } /** * 构建菜单HTML */ function buildMenuHtml(menus) { var html = ''; menus.forEach(function (menu) { if (menu.menuType === 'M' || menu.menuType === 'C') { var hasChildren = menu.children && menu.children.length > 0; html += '
  • '; html += ''; if (menu.icon) { html += ''; } html += '' + menu.menuName + ''; if (hasChildren) { html += '
    '; menu.children.forEach(function (child) { html += '
    '; html += ''; if (child.icon) { html += ''; } html += '' + child.menuName + ''; html += '
    '; }); html += '
    '; } html += '
  • '; } }); return html; } /** * 绑定菜单点击事件 */ function bindMenuEvents() { $('.open-tab').on('click', function () { var $this = $(this); var url = $this.data('url'); var title = $this.data('title'); var id = $this.data('id'); if (!url) { layer.msg('页面地址不存在', {icon: 0}); return; } openTab(url, title, id); }); } /** * 打开标签页 */ window.openTab = function(url, title, id) { id = id || url.replace(/\//g, '_').replace(/\./g, '_'); // 检查标签是否已存在 var exists = false; $('#tabTitle li').each(function () { if ($(this).attr('lay-id') === id) { exists = true; return false; } }); if (exists) { element.tabChange('mainTab', id); } else { // 添加新标签 element.tabAdd('mainTab', { title: '' + title + '', content: '', id: id }); element.tabChange('mainTab', id); } } /** * 初始化标签页事件 */ function initTabEvents() { // 标签栏关闭按钮 $(document).on('click', '.layui-tab-bar i', function () { var $parent = $(this).parent(); var id = $parent.attr('lay-id'); if (id !== 'home') { element.tabDelete('mainTab', id); } }); // 刷新当前页 - 使用 document.on 确保动态元素也能响应 $(document).on('click', '.refresh-page', function () { refreshCurrentTab(); }); } /** * 刷新当前标签页 */ function refreshCurrentTab() { var $currentTab = $('.layui-tab-content .layui-show'); if ($currentTab.length) { var iframe = $currentTab.find('iframe')[0]; if (iframe && iframe.contentWindow) { iframe.contentWindow.location.reload(); layer.msg('刷新成功', { icon: 1, time: 1000 }); } } } // 修改密码 $('#updatePwd').click(function () { Common.open({ title: '修改密码', area: ['450px', '300px'], content: 'user/update_pwd.html' }); }); // 退出登录 $('#logoutBtn').click(function () { Common.confirm('确定要退出登录吗?', function () { Common.post(Config.api.logout, {}, function (res) { localStorage.clear(); location.href = '../index.html'; }, function () { localStorage.clear(); location.href = '../index.html'; }); }); }); // 窗口调整时重置iframe高度 window.onresize = function () { var height = $(window).height(); $('.layui-tab-content').height(height - 102); $('.main-iframe').height($('.layui-tab-content').height()); }; // 触发一次resize window.onresize(); });