优化
This commit is contained in:
+38
-25
@@ -8,7 +8,7 @@ import 'element-plus/es/components/message-box/style/css'
|
||||
import App from './App.vue'
|
||||
import router from '@/app/router'
|
||||
import { sanitizeInternalPath } from '@/core/routing/accessDecision'
|
||||
import { clearNavigationRecoveryAttempt, recoverNavigationError } from '@/core/routing/navigationRecovery'
|
||||
import { createNavigationRecoveryHandler, installAssetRecoveryListeners, isStaleRouteAssetError, reloadNavigationTarget } from '@/core/routing/navigationRecovery'
|
||||
import { useAccessPreviewStore } from '@/core/access/accessPreview'
|
||||
import { useUnsavedWorkStore } from '@/core/unsaved-work/unsavedWork'
|
||||
import { useWorkspaceTabsStore } from '@/core/workspace-tabs/workspaceTabs'
|
||||
@@ -22,8 +22,6 @@ const auth = useAuthStore(pinia)
|
||||
const unsaved = useUnsavedWorkStore(pinia)
|
||||
const workspace = useWorkspaceTabsStore(pinia)
|
||||
|
||||
await auth.restoreSession()
|
||||
|
||||
registerAuthSessionExpiredHandler(async () => {
|
||||
const currentRoute = router.currentRoute.value
|
||||
if (currentRoute.name === 'login') return
|
||||
@@ -39,31 +37,41 @@ registerAuthSessionExpiredHandler(async () => {
|
||||
})
|
||||
})
|
||||
|
||||
const recoverAssetError = createNavigationRecoveryHandler({
|
||||
storage: {
|
||||
getItem: (key) => window.sessionStorage.getItem(key),
|
||||
setItem: (key, value) => window.sessionStorage.setItem(key, value),
|
||||
},
|
||||
now: Date.now,
|
||||
hasUnsavedWork: () => unsaved.entries.some((entry) => entry.dirty),
|
||||
confirmDiscard: async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
'页面资源加载失败,可能已发布新版本,需要重新加载页面。继续后将放弃当前未保存内容。',
|
||||
'重新加载页面?',
|
||||
{ confirmButtonText: '重新加载', cancelButtonText: '继续编辑', type: 'warning', customClass: 'admin-density-confirm' },
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
},
|
||||
discardUnsavedWork: () => unsaved.clear(),
|
||||
navigate: (target) => reloadNavigationTarget(window, target),
|
||||
showError: (message) => ElMessage.error(message),
|
||||
})
|
||||
let pendingRoute: string | undefined
|
||||
const recoveryTarget = () => pendingRoute
|
||||
?? `${window.location.pathname}${window.location.search}${window.location.hash}`
|
||||
router.beforeEach((to) => { pendingRoute = to.fullPath })
|
||||
router.onError((error, to) => {
|
||||
void recoverNavigationError(error, to.fullPath, {
|
||||
storage: sessionStorage,
|
||||
now: Date.now,
|
||||
hasUnsavedWork: () => unsaved.entries.some((entry) => entry.dirty),
|
||||
confirmDiscard: async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
'检测到后台已发布新版本,需要重新加载页面。继续后将放弃当前未保存内容。',
|
||||
'重新加载页面?',
|
||||
{ confirmButtonText: '重新加载', cancelButtonText: '继续编辑', type: 'warning', customClass: 'admin-density-confirm' },
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
},
|
||||
discardUnsavedWork: () => unsaved.clear(),
|
||||
navigate: (target) => window.location.assign(target),
|
||||
showError: (message) => ElMessage.error(message),
|
||||
})
|
||||
void recoverAssetError(error, to.fullPath)
|
||||
if (pendingRoute === to.fullPath) pendingRoute = undefined
|
||||
})
|
||||
router.afterEach((_to, _from, failure) => {
|
||||
if (!failure) clearNavigationRecoveryAttempt(sessionStorage)
|
||||
router.afterEach((to) => {
|
||||
if (pendingRoute === to.fullPath) pendingRoute = undefined
|
||||
})
|
||||
installAssetRecoveryListeners(window, recoveryTarget, recoverAssetError)
|
||||
|
||||
window.addEventListener('beforeunload', (event) => {
|
||||
if (!unsaved.entries.some((entry) => entry.dirty)) return
|
||||
@@ -72,5 +80,10 @@ window.addEventListener('beforeunload', (event) => {
|
||||
})
|
||||
|
||||
const app = createApp(App)
|
||||
app.config.errorHandler = (error) => {
|
||||
if (isStaleRouteAssetError(error)) void recoverAssetError(error, recoveryTarget())
|
||||
else console.error(error)
|
||||
}
|
||||
app.provide(UI_PREFERENCE_SCOPE, computed(() => auth.user?.id || 'anonymous'))
|
||||
await auth.restoreSession()
|
||||
app.use(pinia).use(router).mount('#app')
|
||||
|
||||
Reference in New Issue
Block a user