index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div :class="classObj" class="app-wrapper" :style="{ '--current-color': theme }">
  3. <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
  4. <side-bar v-if="!sidebar.hide" class="sidebar-container" />
  5. <div :class="{ hasTagsView: needTagsView, sidebarHide: sidebar.hide }" class="main-container">
  6. <!-- <el-scrollbar>
  7. <div :class="{ 'fixed-header': fixedHeader }">
  8. <navbar ref="navbarRef" @setLayout="setLayout" />
  9. <tags-view v-if="needTagsView" />
  10. </div>
  11. <app-main />
  12. <settings ref="settingRef" />
  13. </el-scrollbar> -->
  14. <div :class="{ 'fixed-header': fixedHeader }">
  15. <navbar ref="navbarRef" @set-layout="setLayout" />
  16. <tags-view v-if="needTagsView" />
  17. </div>
  18. <app-main />
  19. <settings ref="settingRef" />
  20. </div>
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import SideBar from './components/Sidebar/index.vue';
  25. import { AppMain, Navbar, Settings, TagsView } from './components';
  26. import { useAppStore } from '@/store/modules/app';
  27. import { useSettingsStore } from '@/store/modules/settings';
  28. import { NavTypeEnum } from '@/enums/NavTypeEnum';
  29. import { initWebSocket } from '@/utils/websocket';
  30. import { initSSE } from '@/utils/sse';
  31. const settingsStore = useSettingsStore();
  32. const theme = computed(() => settingsStore.theme);
  33. const sidebar = computed(() => useAppStore().sidebar);
  34. const device = computed(() => useAppStore().device);
  35. const needTagsView = computed(() => settingsStore.tagsView);
  36. const fixedHeader = computed(() => settingsStore.fixedHeader);
  37. const layout = computed(() => settingsStore.navType);
  38. // 根据布局模式判断是否显示侧边栏
  39. const showSidebar = computed(() => {
  40. if (sidebar.value.hide) return false;
  41. return layout.value === NavTypeEnum.LEFT || layout.value === NavTypeEnum.MIX;
  42. });
  43. const classObj = computed(() => ({
  44. hideSidebar: !sidebar.value.opened,
  45. openSidebar: sidebar.value.opened,
  46. withoutAnimation: sidebar.value.withoutAnimation,
  47. mobile: device.value === 'mobile'
  48. }));
  49. const { width } = useWindowSize();
  50. const WIDTH = 992; // refer to Bootstrap's responsive design
  51. watchEffect(() => {
  52. if (device.value === 'mobile') {
  53. useAppStore().closeSideBar({ withoutAnimation: false });
  54. }
  55. if (width.value - 1 < WIDTH) {
  56. useAppStore().toggleDevice('mobile');
  57. useAppStore().closeSideBar({ withoutAnimation: true });
  58. } else {
  59. useAppStore().toggleDevice('desktop');
  60. }
  61. });
  62. const navbarRef = ref<InstanceType<typeof Navbar>>();
  63. const settingRef = ref<InstanceType<typeof Settings>>();
  64. onMounted(() => {
  65. nextTick(() => {
  66. navbarRef.value?.initTenantList();
  67. });
  68. });
  69. onMounted(() => {
  70. const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
  71. initWebSocket(protocol + window.location.host + import.meta.env.VITE_APP_BASE_API + '/resource/websocket');
  72. });
  73. onMounted(() => {
  74. initSSE(import.meta.env.VITE_APP_BASE_API + '/resource/sse');
  75. });
  76. const handleClickOutside = () => {
  77. useAppStore().closeSideBar({ withoutAnimation: false });
  78. };
  79. const setLayout = () => {
  80. settingRef.value?.openSetting();
  81. };
  82. </script>
  83. <style lang="scss" scoped>
  84. @use '@/assets/styles/mixin.scss';
  85. @use '@/assets/styles/variables.module.scss' as *;
  86. .app-wrapper {
  87. @include mixin.clearfix;
  88. position: relative;
  89. height: 100%;
  90. width: 100%;
  91. &.mobile.openSidebar {
  92. position: fixed;
  93. top: 0;
  94. }
  95. }
  96. .drawer-bg {
  97. background: #000;
  98. opacity: 0.3;
  99. width: 100%;
  100. top: 0;
  101. height: 100%;
  102. position: absolute;
  103. z-index: 999;
  104. }
  105. .fixed-header {
  106. position: fixed;
  107. top: 0;
  108. right: 0;
  109. z-index: 9;
  110. width: calc(100% - #{$base-sidebar-width});
  111. transition: width 0.28s;
  112. background: $fixed-header-bg;
  113. }
  114. .hideSidebar .fixed-header {
  115. width: calc(100% - 54px);
  116. }
  117. .sidebarHide .fixed-header {
  118. width: 100%;
  119. }
  120. .mobile .fixed-header {
  121. width: 100%;
  122. }
  123. </style>