main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * EMA 主框架脚本
  3. */
  4. layui.use(['element', 'layer'], function () {
  5. var element = layui.element;
  6. var layer = layui.layer;
  7. var $ = layui.jquery;
  8. // 检查登录状态
  9. Common.checkLogin();
  10. // 获取用户信息
  11. var userInfo = Common.getUserInfo();
  12. if (userInfo) {
  13. $('#userName').text(userInfo.name || userInfo.account || '用户');
  14. if (userInfo.avatar) {
  15. $('#userInfo img').attr('src', userInfo.avatar);
  16. }
  17. }
  18. // 加载菜单
  19. loadMenus();
  20. // 初始化标签页点击事件
  21. initTabEvents();
  22. /**
  23. * 加载菜单
  24. */
  25. function loadMenus() {
  26. Common.get(Config.api.menuTree, function (res) {
  27. if (res.code === 200 || res.code === 0) {
  28. var menus = res.data || [];
  29. var html = buildMenuHtml(menus);
  30. $('#navMenu').html(html);
  31. element.render('nav');
  32. // 绑定菜单点击事件
  33. bindMenuEvents();
  34. }
  35. });
  36. }
  37. /**
  38. * 构建菜单HTML
  39. */
  40. function buildMenuHtml(menus) {
  41. var html = '';
  42. menus.forEach(function (menu) {
  43. if (menu.menuType === 'M' || menu.menuType === 'C') {
  44. var hasChildren = menu.children && menu.children.length > 0;
  45. html += '<li class="layui-nav-item';
  46. if (hasChildren) {
  47. html += ' layui-nav-itemed';
  48. }
  49. html += '">';
  50. html += '<a href="javascript:;" ' + (hasChildren ? '' : 'class="open-tab"') + ' data-url="' + (menu.component || '') + '" data-title="' + menu.menuName + '" data-id="' + menu.id + '">';
  51. if (menu.icon) {
  52. html += '<i class="layui-icon ' + menu.icon + '"></i>';
  53. }
  54. html += '<cite>' + menu.menuName + '</cite></a>';
  55. if (hasChildren) {
  56. html += '<dl class="layui-nav-child">';
  57. menu.children.forEach(function (child) {
  58. html += '<dd>';
  59. html += '<a href="javascript:;" class="open-tab" data-url="' + (child.component || '') + '" data-title="' + child.menuName + '" data-id="' + child.id + '">';
  60. if (child.icon) {
  61. html += '<i class="layui-icon ' + child.icon + '"></i>';
  62. }
  63. html += '<cite>' + child.menuName + '</cite></a>';
  64. html += '</dd>';
  65. });
  66. html += '</dl>';
  67. }
  68. html += '</li>';
  69. }
  70. });
  71. return html;
  72. }
  73. /**
  74. * 绑定菜单点击事件
  75. */
  76. function bindMenuEvents() {
  77. $('.open-tab').on('click', function () {
  78. var $this = $(this);
  79. var url = $this.data('url');
  80. var title = $this.data('title');
  81. var id = $this.data('id');
  82. if (!url) {
  83. layer.msg('页面地址不存在', {icon: 0});
  84. return;
  85. }
  86. openTab(url, title, id);
  87. });
  88. }
  89. /**
  90. * 打开标签页
  91. */
  92. window.openTab = function(url, title, id) {
  93. id = id || url.replace(/\//g, '_').replace(/\./g, '_');
  94. // 检查标签是否已存在
  95. var exists = false;
  96. $('#tabTitle li').each(function () {
  97. if ($(this).attr('lay-id') === id) {
  98. exists = true;
  99. return false;
  100. }
  101. });
  102. if (exists) {
  103. element.tabChange('mainTab', id);
  104. } else {
  105. // 添加新标签
  106. element.tabAdd('mainTab', {
  107. title: '<i class="layui-icon layui-icon-app"></i>' + title + '<i class="layui-icon layui-icon-close layui-unselect layui-tab-bar"></i>',
  108. content: '<iframe src="' + url + '" frameborder="0" class="main-iframe"></iframe>',
  109. id: id
  110. });
  111. element.tabChange('mainTab', id);
  112. }
  113. }
  114. /**
  115. * 初始化标签页事件
  116. */
  117. function initTabEvents() {
  118. // 标签栏关闭按钮
  119. $(document).on('click', '.layui-tab-bar i', function () {
  120. var $parent = $(this).parent();
  121. var id = $parent.attr('lay-id');
  122. if (id !== 'home') {
  123. element.tabDelete('mainTab', id);
  124. }
  125. });
  126. // 刷新当前页 - 使用 document.on 确保动态元素也能响应
  127. $(document).on('click', '.refresh-page', function () {
  128. refreshCurrentTab();
  129. });
  130. }
  131. /**
  132. * 刷新当前标签页
  133. */
  134. function refreshCurrentTab() {
  135. var $currentTab = $('.layui-tab-content .layui-show');
  136. if ($currentTab.length) {
  137. var iframe = $currentTab.find('iframe')[0];
  138. if (iframe && iframe.contentWindow) {
  139. iframe.contentWindow.location.reload();
  140. layer.msg('刷新成功', { icon: 1, time: 1000 });
  141. }
  142. }
  143. }
  144. // 修改密码
  145. $('#updatePwd').click(function () {
  146. Common.open({
  147. title: '修改密码',
  148. area: ['450px', '300px'],
  149. content: 'user/update_pwd.html'
  150. });
  151. });
  152. // 退出登录
  153. $('#logoutBtn').click(function () {
  154. Common.confirm('确定要退出登录吗?', function () {
  155. Common.post(Config.api.logout, {}, function (res) {
  156. localStorage.clear();
  157. location.href = '../index.html';
  158. }, function () {
  159. localStorage.clear();
  160. location.href = '../index.html';
  161. });
  162. });
  163. });
  164. // 窗口调整时重置iframe高度
  165. window.onresize = function () {
  166. var height = $(window).height();
  167. $('.layui-tab-content').height(height - 102);
  168. $('.main-iframe').height($('.layui-tab-content').height());
  169. };
  170. // 触发一次resize
  171. window.onresize();
  172. });