fix:修改bug: 虚流量关闭的时候还是显示了
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m46s
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m46s
This commit is contained in:
80
src/App.vue
80
src/App.vue
@@ -16,6 +16,7 @@
|
||||
import { setThemeTransitionClass } from '@/utils'
|
||||
import { checkStorageCompatibility } from '@/utils'
|
||||
import ChunkErrorBoundary from '@/components/core/others/ChunkErrorBoundary.vue'
|
||||
import { ElMessageBox } from 'element-plus'
|
||||
|
||||
const userStore = useUserStore()
|
||||
const { language } = storeToRefs(userStore)
|
||||
@@ -25,6 +26,12 @@
|
||||
en: en
|
||||
}
|
||||
|
||||
const VERSION_CHECK_INTERVAL = 5 * 60 * 1000
|
||||
let versionCheckTimer: number | undefined
|
||||
let currentEntrySignature = ''
|
||||
let upgradePromptVisible = false
|
||||
let removeVisibilityChangeListener: (() => void) | undefined
|
||||
|
||||
onBeforeMount(() => {
|
||||
setThemeTransitionClass(true)
|
||||
})
|
||||
@@ -36,10 +43,19 @@
|
||||
setThemeTransitionClass(false)
|
||||
// 系统升级
|
||||
systemUpgrade()
|
||||
// 检查前端版本更新
|
||||
startVersionUpdateCheck()
|
||||
// 获取用户信息
|
||||
getUserInfo()
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (versionCheckTimer) {
|
||||
window.clearInterval(versionCheckTimer)
|
||||
}
|
||||
removeVisibilityChangeListener?.()
|
||||
})
|
||||
|
||||
// 获取用户信息
|
||||
const getUserInfo = async () => {
|
||||
if (userStore.isLogin && userStore.accessToken) {
|
||||
@@ -55,4 +71,68 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getEntrySignature = (html: string) => {
|
||||
const matches = html.match(/(?:src|href)="[^"]*\/assets\/[^""]+\.(?:js|css)"/g)
|
||||
return matches?.sort().join('|') || ''
|
||||
}
|
||||
|
||||
const getLoadedEntrySignature = () => {
|
||||
return Array.from(document.querySelectorAll<HTMLScriptElement | HTMLLinkElement>('script[src],link[href]'))
|
||||
.map((element) => ('src' in element ? element.src : element.href))
|
||||
.filter((url) => /\/assets\/.*\.(js|css)(\?|$)/.test(url))
|
||||
.sort()
|
||||
.join('|')
|
||||
}
|
||||
|
||||
const checkVersionUpdate = async () => {
|
||||
if (upgradePromptVisible || document.hidden) return
|
||||
|
||||
try {
|
||||
const response = await fetch(`${window.location.origin}${import.meta.env.BASE_URL}`, {
|
||||
cache: 'no-store'
|
||||
})
|
||||
const html = await response.text()
|
||||
const latestSignature = getEntrySignature(html)
|
||||
|
||||
if (!latestSignature) return
|
||||
|
||||
if (!currentEntrySignature) {
|
||||
currentEntrySignature = latestSignature
|
||||
return
|
||||
}
|
||||
|
||||
if (currentEntrySignature !== latestSignature) {
|
||||
upgradePromptVisible = true
|
||||
await ElMessageBox.alert('系统已发布新版本,请刷新页面后继续使用。', '发现新版本', {
|
||||
confirmButtonText: '立即刷新',
|
||||
type: 'warning',
|
||||
closeOnClickModal: false,
|
||||
closeOnPressEscape: false
|
||||
})
|
||||
window.location.reload()
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('检查版本更新失败:', error)
|
||||
} finally {
|
||||
upgradePromptVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
const startVersionUpdateCheck = () => {
|
||||
if (import.meta.env.DEV) return
|
||||
|
||||
currentEntrySignature = getLoadedEntrySignature()
|
||||
checkVersionUpdate()
|
||||
versionCheckTimer = window.setInterval(checkVersionUpdate, VERSION_CHECK_INTERVAL)
|
||||
const handleVisibilityChange = () => {
|
||||
if (!document.hidden) {
|
||||
checkVersionUpdate()
|
||||
}
|
||||
}
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange)
|
||||
removeVisibilityChangeListener = () => {
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user