fix: 自动更新提示bug
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m55s

This commit is contained in:
sexygoat
2026-05-29 12:11:03 +08:00
parent 76a526643e
commit f62a8d98a4
2 changed files with 63 additions and 166 deletions

View File

@@ -27,6 +27,7 @@
}
const VERSION_CHECK_INTERVAL = 5 * 60 * 1000
const VERSION_PROMPT_STORAGE_KEY = 'admin-xm-iot:confirmed-version-update'
let versionCheckTimer: number | undefined
let currentEntrySignature = ''
let upgradePromptVisible = false
@@ -78,18 +79,69 @@
}
const getLoadedEntrySignature = () => {
return Array.from(document.querySelectorAll<HTMLScriptElement | HTMLLinkElement>('script[src],link[href]'))
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 getStringHash = (value: string) => {
let hash = 0
for (let i = 0; i < value.length; i++) {
hash = (hash << 5) - hash + value.charCodeAt(i)
hash |= 0
}
return Math.abs(hash).toString(36)
}
const getBrowserFingerprint = () => {
return getStringHash(
[
navigator.userAgent,
navigator.language,
navigator.platform,
Intl.DateTimeFormat().resolvedOptions().timeZone,
`${screen.width}x${screen.height}x${screen.colorDepth}`
].join('|')
)
}
const getConfirmedVersionKey = (signature: string) => {
return `${getBrowserFingerprint()}:${getStringHash(signature)}`
}
const getConfirmedVersionUpdate = () => {
try {
return localStorage.getItem(VERSION_PROMPT_STORAGE_KEY)
} catch {
return null
}
}
const setConfirmedVersionUpdate = (signature: string) => {
try {
localStorage.setItem(VERSION_PROMPT_STORAGE_KEY, getConfirmedVersionKey(signature))
} catch {
// localStorage may be unavailable in private mode.
}
}
const reloadWithCacheBuster = () => {
const url = new URL(window.location.href)
url.searchParams.set('_t', Date.now().toString())
window.location.replace(url.toString())
}
const checkVersionUpdate = async () => {
if (upgradePromptVisible || document.hidden) return
try {
const response = await fetch(`${window.location.origin}${import.meta.env.BASE_URL}`, {
const response = await fetch(import.meta.env.BASE_URL, {
cache: 'no-store'
})
const html = await response.text()
@@ -103,14 +155,21 @@
}
if (currentEntrySignature !== latestSignature) {
if (getConfirmedVersionUpdate() === getConfirmedVersionKey(latestSignature)) {
currentEntrySignature = latestSignature
return
}
upgradePromptVisible = true
await ElMessageBox.alert('系统已发布新版本,请刷新页面后继续使用。', '发现新版本', {
confirmButtonText: '立即新',
confirmButtonText: '立即新',
type: 'warning',
closeOnClickModal: false,
closeOnPressEscape: false
})
window.location.reload()
setConfirmedVersionUpdate(latestSignature)
currentEntrySignature = latestSignature
reloadWithCacheBuster()
}
} catch (error) {
console.warn('检查版本更新失败:', error)