App.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <el-config-provider :size="getGlobalComponentSize" :locale="getGlobalI18n">
  3. <!-- v-show="themeConfig.lockScreenTime > 1" -->
  4. <router-view v-show="themeConfig.lockScreenTime > 1" />
  5. <LockScreen v-if="themeConfig.isLockScreen" />
  6. <Setings ref="setingsRef" v-show="themeConfig.lockScreenTime > 1" />
  7. <CloseFull v-if="!themeConfig.isLockScreen" />
  8. </el-config-provider>
  9. </template>
  10. <script setup lang="ts" name="app">
  11. import { defineAsyncComponent, computed, ref, onBeforeMount, onMounted, onUnmounted, nextTick, watch, onBeforeUnmount } from 'vue';
  12. import { useRoute } from 'vue-router';
  13. import { useI18n } from 'vue-i18n';
  14. import { storeToRefs } from 'pinia';
  15. import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
  16. import { useThemeConfig } from '/@/stores/themeConfig';
  17. import other from '/@/utils/other';
  18. import { Local, Session } from '/@/utils/storage';
  19. import mittBus from '/@/utils/mitt';
  20. import setIntroduction from '/@/utils/setIconfont';
  21. // 引入组件
  22. const LockScreen = defineAsyncComponent(() => import('/@/layout/lockScreen/index.vue'));
  23. const Setings = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/setings.vue'));
  24. const CloseFull = defineAsyncComponent(() => import('/@/layout/navBars/breadcrumb/closeFull.vue'));
  25. // 定义变量内容
  26. const { messages, locale } = useI18n();
  27. const setingsRef = ref();
  28. const route = useRoute();
  29. const stores = useTagsViewRoutes();
  30. const storesThemeConfig = useThemeConfig();
  31. const { themeConfig } = storeToRefs(storesThemeConfig);
  32. import websocket from '/@/utils/websocket';
  33. import { ElNotification } from 'element-plus';
  34. // 获取版本号
  35. const getVersion = computed(() => {
  36. let isVersion = false;
  37. if (route.path !== '/login') {
  38. // @ts-ignore
  39. if ((Local.get('version') && Local.get('version') !== __VERSION__) || !Local.get('version')) isVersion = true;
  40. }
  41. return isVersion;
  42. });
  43. // 获取全局组件大小
  44. const getGlobalComponentSize = computed(() => {
  45. return other.globalComponentSize();
  46. });
  47. // 获取全局 i18n
  48. const getGlobalI18n = computed(() => {
  49. return messages.value[locale.value];
  50. });
  51. // 设置初始化,防止刷新时恢复默认
  52. onBeforeMount(() => {
  53. // 设置批量第三方 icon 图标
  54. setIntroduction.cssCdn();
  55. // 设置批量第三方 js
  56. setIntroduction.jsCdn();
  57. });
  58. // 页面加载时
  59. onMounted(() => {
  60. nextTick(() => {
  61. // 监听布局配'置弹窗点击打开
  62. mittBus.on('openSetingsDrawer', () => {
  63. setingsRef.value.openDrawer();
  64. });
  65. // 获取缓存中的布局配置
  66. if (Local.get('themeConfig')) {
  67. storesThemeConfig.setThemeConfig({ themeConfig: Local.get('themeConfig') });
  68. document.documentElement.style.cssText = Local.get('themeConfigStyle');
  69. }
  70. // 获取缓存中的全屏配置
  71. if (Session.get('isTagsViewCurrenFull')) {
  72. stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
  73. }
  74. });
  75. });
  76. // 页面销毁时,关闭监听布局配置/i18n监听
  77. onUnmounted(() => {
  78. mittBus.off('openSetingsDrawer', () => {});
  79. });
  80. // 监听路由的变化,设置网站标题
  81. watch(
  82. () => route.path,
  83. () => {
  84. other.useTitle();
  85. other.useFavicon();
  86. if (!websocket.websocket) {
  87. //websockt 模块
  88. try {
  89. websocket.init(wsReceive)
  90. } catch (e) {
  91. console.log('websocket错误');
  92. }
  93. }
  94. },
  95. {
  96. deep: true,
  97. }
  98. );
  99. // websocket相关代码
  100. import { messageCenterStore } from '/@/stores/messageCenter';
  101. const wsReceive = (message: any) => {
  102. const data = JSON.parse(message.data);
  103. const { unread } = data;
  104. const messageCenter = messageCenterStore();
  105. messageCenter.setUnread(unread);
  106. // if (data.contentType === 'SYSTEM') {
  107. // ElNotification({
  108. // title: '系统消息',
  109. // message: data.content,
  110. // type: 'success',
  111. // position: 'bottom-right',
  112. // duration: 5000,
  113. // });
  114. // }
  115. };
  116. onBeforeUnmount(() => {
  117. // 关闭连接
  118. websocket.close();
  119. });
  120. </script>