| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /**
- * 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 += '<li class="layui-nav-item';
- if (hasChildren) {
- html += ' layui-nav-itemed';
- }
- html += '">';
- html += '<a href="javascript:;" ' + (hasChildren ? '' : 'class="open-tab"') + ' data-url="' + (menu.component || '') + '" data-title="' + menu.menuName + '" data-id="' + menu.id + '">';
- if (menu.icon) {
- html += '<i class="layui-icon ' + menu.icon + '"></i>';
- }
- html += '<cite>' + menu.menuName + '</cite></a>';
- if (hasChildren) {
- html += '<dl class="layui-nav-child">';
- menu.children.forEach(function (child) {
- html += '<dd>';
- html += '<a href="javascript:;" class="open-tab" data-url="' + (child.component || '') + '" data-title="' + child.menuName + '" data-id="' + child.id + '">';
- if (child.icon) {
- html += '<i class="layui-icon ' + child.icon + '"></i>';
- }
- html += '<cite>' + child.menuName + '</cite></a>';
- html += '</dd>';
- });
- html += '</dl>';
- }
- html += '</li>';
- }
- });
- 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: '<i class="layui-icon layui-icon-app"></i>' + title + '<i class="layui-icon layui-icon-close layui-unselect layui-tab-bar"></i>',
- content: '<iframe src="' + url + '" frameborder="0" class="main-iframe"></iframe>',
- 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();
- });
|