App.vue 4.0 KB

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