App.vue 5.2 KB

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