109 lines
4.2 KiB
TypeScript
109 lines
4.2 KiB
TypeScript
import { createApp, watch } from 'vue'
|
||
import { createPinia } from 'pinia'
|
||
import ElementPlus from 'element-plus'
|
||
import 'element-plus/dist/index.css'
|
||
import 'virtual:svg-icons-register'
|
||
|
||
import App from './App.vue'
|
||
import router from './router'
|
||
import './permission' // permission control
|
||
import SvgIcon from '@/components/SvgIcon/index.vue'
|
||
import i18n, { loadServerMessages, tMenu } from '@/lang/index'
|
||
import getPageTitle from '@/utils/get-page-title'
|
||
|
||
import '@/styles/index.scss' // global css
|
||
import { getToken } from '@/utils/auth'
|
||
import { initPrinterRuntime } from '@/core/printerRuntime'
|
||
import { usePrinterComponentStore } from '@/stores/modules/printerComponent'
|
||
import { useFrontBrandStore } from '@/stores/modules/frontBrand'
|
||
|
||
const app = createApp(App)
|
||
|
||
let errorHandlerDepth = 0
|
||
app.config.errorHandler = (err, _instance, info) => {
|
||
if (errorHandlerDepth > 0) {
|
||
// 避免错误处理过程中再次抛错导致无限递归与栈溢出
|
||
console.error('[Vue] errorHandler re-entry', err)
|
||
return
|
||
}
|
||
errorHandlerDepth += 1
|
||
try {
|
||
console.error(info ? `[Vue] ${info}` : '[Vue] error', err)
|
||
} finally {
|
||
errorHandlerDepth -= 1
|
||
}
|
||
}
|
||
|
||
app.component('svg-icon', SvgIcon)
|
||
const pinia = createPinia()
|
||
app.use(pinia)
|
||
app.use(router)
|
||
app.use(i18n)
|
||
app.use(ElementPlus)
|
||
|
||
// 加载服务器国际化翻译
|
||
loadServerMessages()
|
||
|
||
// 初始化打印组件运行时(登录成功后再建立连接,见 permission.ts)
|
||
initPrinterRuntime()
|
||
usePrinterComponentStore(pinia).bindRuntimeEvents()
|
||
|
||
app.mount('#app')
|
||
|
||
// ── 页面/浏览器标题(document.title)统一在此设置 ──
|
||
// 使用显式 pinia 的 frontBrand store 引用(与品牌加载同源),避免组件/守卫上下文取到不一致实例。
|
||
// 品牌加载完成前只显示“页面名”(不拼站点,不闪现默认“天豹云仓”);加载完成后:
|
||
// 成功→“页面名 - 品牌名”;失败/豁免→“页面名 - 天豹云仓”;/login 用纯站点名。
|
||
const frontBrandStore = useFrontBrandStore(pinia)
|
||
|
||
function resolveRoutePageTitle(route: { name?: unknown; meta?: Record<string, unknown> }): string {
|
||
// 菜单词条可能是对象(后台按点分层,见 lang/index.ts 的 tMenu),统一走 tMenu 兜底
|
||
const tryMenuKey = (raw: unknown) => {
|
||
if (raw === undefined || raw === null || raw === '') return ''
|
||
return tMenu(raw, false)
|
||
}
|
||
const fromName = tryMenuKey(route.name)
|
||
if (fromName) return fromName
|
||
const fromMeta = tryMenuKey(route.meta?.title)
|
||
if (fromMeta) return fromMeta
|
||
const metaTitle = route.meta?.title
|
||
return metaTitle != null && metaTitle !== '' ? String(metaTitle) : route.name != null ? String(route.name) : ''
|
||
}
|
||
|
||
function applyDocumentTitle(route?: typeof router.currentRoute.value): void {
|
||
const target = route || router.currentRoute.value
|
||
if (!target || !target.path) return
|
||
const page = resolveRoutePageTitle(target)
|
||
if (!frontBrandStore.loaded) {
|
||
// 品牌加载中:仅页面名(/login 用中性“加载中...”),不显示默认站点名
|
||
document.title = target.path.startsWith('/login') ? '加载中...' : page || '加载中...'
|
||
return
|
||
}
|
||
const site = frontBrandStore.siteTitle || '天豹云仓'
|
||
if (target.path.startsWith('/login')) {
|
||
document.title = site
|
||
return
|
||
}
|
||
document.title = getPageTitle(page, site)
|
||
}
|
||
applyDocumentTitle()
|
||
watch(
|
||
[() => frontBrandStore.loaded, () => frontBrandStore.siteTitle, () => router.currentRoute.value.path],
|
||
() => applyDocumentTitle()
|
||
)
|
||
router.afterEach((to) => applyDocumentTitle(to))
|
||
|
||
// 前端独立部署品牌:登录前按当前访问域名拉取(免鉴权),成功后应用 favicon / 登录页标题品牌;失败静默走默认品牌
|
||
frontBrandStore.loadBrandByDomain()
|
||
|
||
// 整页刷新(F5 / 顶部刷新)后的“一次自动连接”兜底:
|
||
// 等路由守卫初始化(getInfo)完成、拿到登录上下文后再执行一次;
|
||
// 与“失败自动重连”主开关无关(主开关只控制断线后的持续重试)。
|
||
// 若守卫已发起连接,runtime 内 connectionsStarted 幂等跳过,不会重复建连。
|
||
if (getToken()) {
|
||
router.isReady().then(() => {
|
||
if (!getToken()) return
|
||
usePrinterComponentStore(pinia).startConnections()
|
||
})
|
||
}
|