App.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <el-config-provider :size="getGlobalComponentSize" :locale="zhCn" :message="messageConfig" :button="buttonConfig">
  3. <el-watermark :content="watermarkText" class="h100">
  4. <router-view v-show="setLockScreen" />
  5. <LockScreen v-if="themeConfig.isLockScreen" />
  6. <SetTings ref="setTingsRef" v-show="setLockScreen" />
  7. <CloseFull v-if="!themeConfig.isLockScreen" />
  8. </el-watermark>
  9. </el-config-provider>
  10. </template>
  11. <script lang="ts" name="app" setup>
  12. import { computed, ref, onBeforeMount, onMounted, onUnmounted, nextTick, watch, reactive, defineAsyncComponent } from 'vue';
  13. import { useRoute } from 'vue-router';
  14. import zhCn from 'element-plus/es/locale/lang/zh-cn';
  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 checkUpdate from '@/utils/checkUpdate';
  20. import mittBus from '@/utils/mitt';
  21. import { Session, Local, Cookie } from '@/utils/storage';
  22. import setIntroduction from '@/utils/setIconfont';
  23. import { loginPageInfo } from '@/api/login';
  24. import { getImageUrl } from '@/utils/tools';
  25. import { useKeepALiveNames } from '@/stores/keepAliveNames';
  26. import { ola } from '@/utils/ola_api';
  27. import signalR from '@/utils/signalR';
  28. import { useUserInfo } from '@/stores/userInfo';
  29. // 引入组件
  30. const LockScreen = defineAsyncComponent(() => import('@/layout/lockScreen/index.vue'));
  31. const SetTings = defineAsyncComponent(() => import('@/layout/navBars/breadcrumb/setings.vue'));
  32. const CloseFull = defineAsyncComponent(() => import('@/layout/navBars/breadcrumb/closeFull.vue'));
  33. const route = useRoute();
  34. const stores = useTagsViewRoutes();
  35. const storesThemeConfig = useThemeConfig();
  36. const { themeConfig } = storeToRefs(storesThemeConfig);
  37. const storesKeepALiveNames = useKeepALiveNames();
  38. const storesTagsViewRoutes = useTagsViewRoutes();
  39. const { tagsViewRoutes } = storeToRefs(storesTagsViewRoutes);
  40. const userStore = useUserInfo();
  41. const { userInfos } = storeToRefs(userStore);
  42. // 设置锁屏时组件显示隐藏
  43. const setLockScreen = computed(() => {
  44. // 防止锁屏后,刷新出现不相关界面
  45. // https://gitee.com/lyt-top/vue-next-admin/issues/I6AF8P
  46. return !themeConfig.value.isLockScreen;
  47. });
  48. // 水印字符串
  49. const watermarkText = computed(() => {
  50. return themeConfig.value.watermarkText;
  51. });
  52. // 可同时显示的消息最大数量
  53. const messageConfig = reactive<any>({
  54. max: 3,
  55. });
  56. // 自动在两个中文字符之间插入空格
  57. const buttonConfig = reactive<any>({
  58. autoInsertSpace: false,
  59. });
  60. // 获取全局组件大小
  61. const getGlobalComponentSize = computed(() => {
  62. return other.globalComponentSize();
  63. });
  64. // 布局配置弹窗打开
  65. const setTingsRef = ref<RefType>();
  66. const openSetTingsDrawer = () => {
  67. setTingsRef.value.openDrawer();
  68. };
  69. // 设置初始化,防止刷新时恢复默认
  70. onBeforeMount(async () => {
  71. // 获取登录页的背景图和系统名称等
  72. const res: any = await loginPageInfo();
  73. const globalTitle = res.result.sysName.join('|') ?? ''; // 标题名称
  74. const loginImage = res.result.loginImage ? `url${res.result.loginImage}` : `url(${getImageUrl('login/bg.png')})`; // 登录页背景图
  75. const isLoginMessageCode = res.result.isLoginMessageCode; // 是否开启短信验证码
  76. storesThemeConfig.setThemeConfig(Object.assign(themeConfig.value, { globalTitle, loginImage, isLoginMessageCode }));
  77. // 设置批量第三方 icon 图标
  78. setIntroduction.cssCdn();
  79. // 设置批量第三方 js
  80. setIntroduction.jsCdn();
  81. });
  82. const unloadHandler = (e: BeforeUnloadEvent) => {
  83. //发送消息
  84. /*if (ola.ws) {
  85. ola.logout();
  86. }*/
  87. };
  88. // 页面加载时
  89. onMounted(() => {
  90. window.addEventListener('beforeunload', (e) => unloadHandler(e));
  91. nextTick(async () => {
  92. try {
  93. // 获取缓存中的布局配置
  94. if (Local.get('themeConfig')) {
  95. storesThemeConfig.setThemeConfig(Local.get('themeConfig'));
  96. document.documentElement.style.cssText = Local.get('themeConfigStyle');
  97. }
  98. // 开发环境不提示更新
  99. if (import.meta.env.VITE_MODE_NAME != 'development') {
  100. // 监听是否更新
  101. await checkUpdate();
  102. }
  103. // 监听布局配置弹窗点击打开
  104. mittBus.on('openSetTingsDrawer', () => {
  105. openSetTingsDrawer();
  106. });
  107. // 获取缓存中的全屏配置
  108. if (Session.get('isTagsViewCurrenFull')) {
  109. stores.setCurrenFullscreen(Session.get('isTagsViewCurrenFull'));
  110. }
  111. // 清除某个页面的缓存
  112. mittBus.on('clearCache', (val: any) => {
  113. clearCacheTagsView(val);
  114. });
  115. // 解决火狐拖动打开新窗口
  116. document.body.ondrop = (event) => {
  117. event.preventDefault();
  118. event.stopPropagation();
  119. };
  120. /*mittBus.on('*', (index, data) => {
  121. console.log(index, data);
  122. });*/
  123. } catch (error) {
  124. console.log(error);
  125. }
  126. });
  127. });
  128. // 清除缓存 name
  129. const clearCacheTagsView = async (routeName: string) => {
  130. let item: any = {};
  131. tagsViewRoutes.value.forEach((v: any) => {
  132. if (v.name === routeName) {
  133. item = v;
  134. }
  135. });
  136. if (!item) return false;
  137. await storesKeepALiveNames.delCachedView(item);
  138. if (item.meta?.isKeepAlive) await storesKeepALiveNames.addCachedView(item);
  139. };
  140. // 页面销毁时,关闭监听布局配置/i18n监听
  141. onUnmounted(() => {
  142. mittBus.off('openSetTingsDrawer', () => {});
  143. mittBus.off('clearCache', () => {});
  144. window.removeEventListener('unload', (e) => unloadHandler(e));
  145. });
  146. // 监听路由的变化,设置网站标题
  147. watch(
  148. () => route.path,
  149. () => {
  150. other.useTitle();
  151. },
  152. {
  153. deep: true,
  154. }
  155. );
  156. </script>