unis-oa/frontend/src/main.ts

66 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 老引擎兼容层必须是第一份被求值的模块:它补的是内置对象(`Object.entries`、
* `Array.prototype.includes`、`padStart`、`new Set(iterable)`、`Element.closest`…),
* 而下面这些模块在顶层就可能用它们。ESM 的求值顺序按 import 走,所以这一行
* 一旦被挪到后面,IE / 旧 Edge 上就是"某格带出值不显示"这种查半天的病。
*/
import '@/utils/legacyCompat'
import { createApp, watch } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import { applyDocumentTitle } from './router/title'
import i18n, { initI18n } from './i18n'
import { useAppStore } from '@/stores/app'
import { useAuthStore } from '@/stores/auth'
import { updateFavicon } from '@/utils/branding'
import { bindSessionIdle } from '@/utils/sessionIdle'
import { bindSessionRecovery } from '@/utils/sessionRecovery'
import { startSsoLanding } from '@/utils/ssoLanding'
import { initPwa } from '@/composables/usePwa'
import './style.css'
async function bootstrap() {
// 深色档与首屏骨架都在 index.html 里画第一笔之前就定好了:写在这儿的话,
// 等这份模块脚本跑到,页面已经白了一瞬。
const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
const appStore = useAppStore()
appStore.initFromInjectedConfig()
// 页签图标跟着站点 logo 走,而且是"跟着"而不是"进门时看一眼":后端没把公开配置
// 注进首屏(库还没起来、静态托管 index.html)时,这一格是接口那趟才到的,
// 界面上改了 logo 也是下一次拉到配置才变。盯住它,这几种来源最后都落到同一枚图标上。
watch(() => appStore.siteLogo, (logo) => updateFavicon(logo), { immediate: true })
// Restore a previous session before the router guard runs.
useAuthStore().checkAuth()
bindSessionRecovery(useAuthStore(), router)
// 统一门户免登落地:换会话这一趟现在就发出去,与语言包、路由块并行下载。
// 放在组件挂载之后才发,就等于让票据排在所有静态资源后面,浏览器会先画一屏
// 「正在确认你的统一门户身份」再走人 —— 从企业微信卡片点进来的人看见的就是那一下闪。
startSsoLanding()
// 空闲超时:后端配了才生效,窗口值每次现读公开配置,改配置不用重新部署前端
bindSessionIdle(useAuthStore(), router, () =>
Number(appStore.cachedPublicSettings?.session?.idle_minutes || 0)
)
// 语言包和首屏路由块都是懒加载:两件事一起发出去,别把路由的 chunk 排在
// 语言包后面下载——首屏白屏多等的就是这一轮往返。
const i18nReady = initI18n()
app.use(router)
const routeReady = router.isReady()
await Promise.all([i18nReady, routeReady])
app.use(i18n)
app.mount('#app')
// 服务工作者与「安装到桌面」:放在挂载之后,别和首屏抢那几毫秒
initPwa()
// 标题是在语言包到位前算的(afterEach 触发),翻译好了再补一次。
applyDocumentTitle()
}
bootstrap()