App.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. //websockt 模块
  60. try {
  61. //websocket.init(wsReceive)
  62. } catch (e) {
  63. console.log('websocket错误');
  64. }
  65. });
  66. // 页面加载时
  67. onMounted(() => {
  68. nextTick(() => {
  69. // 监听布局配'置弹窗点击打开
  70. mittBus.on('openSetingsDrawer', () => {
  71. setingsRef.value.openDrawer();
  72. });
  73. // 获取缓存中的布局配置
  74. if (Local.get('themeConfig')) {
  75. storesThemeConfig.setThemeConfig({ themeConfig: Local.get('themeConfig') });
  76. document.documentElement.style.cssText = Local.get('themeConfigStyle');
  77. }
  78. // 获取缓存中的全屏配置
  79. if (Session.get('isTagsViewCurrenFull')) {
  80. stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
  81. }
  82. });
  83. });
  84. // 页面销毁时,关闭监听布局配置/i18n监听
  85. onUnmounted(() => {
  86. mittBus.off('openSetingsDrawer', () => {});
  87. });
  88. // 监听路由的变化,设置网站标题
  89. watch(
  90. () => route.path,
  91. () => {
  92. other.useTitle();
  93. },
  94. {
  95. deep: true,
  96. }
  97. );
  98. // websocket相关代码
  99. import { messageCenterStore } from '/@/stores/messageCenter';
  100. const wsReceive = (message: any) => {
  101. const data = JSON.parse(message.data);
  102. const { unread } = data;
  103. const messageCenter = messageCenterStore();
  104. messageCenter.setUnread(unread);
  105. if (data.contentType === 'SYSTEM') {
  106. ElNotification({
  107. title: '系统消息',
  108. message: data.content,
  109. type: 'success',
  110. position: 'bottom-right',
  111. duration: 5000,
  112. });
  113. }
  114. };
  115. onBeforeUnmount(() => {
  116. // 关闭连接
  117. websocket.close();
  118. });
  119. </script>