App.vue 3.9 KB

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