@_lan/web-libs
Version:
<div align="center"> <img src="./public/favicon.svg" width="160" /> <h1>SoybeanAdmin AntDesign</h1> <span>中文 | <a href="./README.en_US.md">English</a></span> </div>
106 lines (86 loc) • 2.77 kB
text/typescript
import { h } from 'vue';
import type { App } from 'vue';
import { Button } from 'ant-design-vue';
import { $t } from '@/locales';
export function setupAppErrorHandle(app: App) {
app.config.errorHandler = (err, vm, info) => {
// eslint-disable-next-line no-console
console.error(err, vm, info);
};
}
export function setupAppVersionNotification() {
// Update check interval in milliseconds
const UPDATE_CHECK_INTERVAL = 3 * 60 * 1000;
const canAutoUpdateApp = import.meta.env.VITE_AUTOMATICALLY_DETECT_UPDATE === 'Y' && import.meta.env.PROD;
if (!canAutoUpdateApp) return;
let isShow = false;
let updateInterval: ReturnType<typeof setInterval> | undefined;
const checkForUpdates = async () => {
if (isShow) return;
const buildTime = await getHtmlBuildTime();
// If build time hasn't changed, no update is needed
if (buildTime === BUILD_TIME) {
return;
}
isShow = true;
const key = `open${Date.now()}`;
window.$notification?.open({
key,
message: $t('system.updateTitle'),
description: $t('system.updateContent'),
btn() {
return h('div', { style: { display: 'flex', justifyContent: 'end', gap: '12px', width: '325px' } }, [
h(
Button,
{
onClick() {
window.$notification?.destroy(key);
isShow = false;
}
},
() => $t('system.updateCancel')
),
h(
Button,
{
type: 'primary',
onClick() {
location.reload();
}
},
() => $t('system.updateConfirm')
)
]);
},
onClose() {
isShow = false;
}
});
};
const startUpdateInterval = () => {
if (updateInterval) {
clearInterval(updateInterval);
}
updateInterval = setInterval(checkForUpdates, UPDATE_CHECK_INTERVAL);
};
// If updates should be checked, set up the visibility change listener and start the update interval
if (!isShow && document.visibilityState === 'visible') {
// Check for updates when the document is visible
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
checkForUpdates();
startUpdateInterval();
}
});
// Start the update interval
startUpdateInterval();
}
}
async function getHtmlBuildTime() {
const baseUrl = import.meta.env.VITE_BASE_URL || '/';
const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`);
const html = await res.text();
const match = html.match(/<meta name="buildTime" content="(.*)">/);
const buildTime = match?.[1] || '';
return buildTime;
}