feat: check version

This commit is contained in:
yangsy
2025-09-08 16:39:58 +08:00
parent 632d7acf59
commit f56a10dfcd
8 changed files with 85 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
export * from './alarm';
export * from './device';
export * from './station';
export * from './system';

View File

@@ -0,0 +1 @@
export * from './use-version-check-query';

View File

@@ -0,0 +1,53 @@
import { useQuery } from '@tanstack/vue-query';
import axios from 'axios';
import { ref, watch } from 'vue';
export function useVersionCheckQuery() {
interface VersionInfo {
version: string;
buildTime: string;
}
const localVersionInfo = ref<VersionInfo>();
const dialogShow = ref<boolean>(false);
const { data: remoteVersionInfo, dataUpdatedAt } = useQuery({
queryKey: ['version-check'],
queryFn: async () => {
const { data } = await axios.get<VersionInfo>(`/version.json?t=${Date.now()}`);
return data;
},
refetchInterval: 10 * 1000,
});
watch(dataUpdatedAt, () => {
const newVersionInfo = remoteVersionInfo.value;
if (!newVersionInfo) return;
if (!localVersionInfo.value) {
localVersionInfo.value = newVersionInfo;
return;
}
if (localVersionInfo.value.version !== newVersionInfo.version && !dialogShow.value) {
dialogShow.value = true;
window.$dialog.info({
title: '发现新版本',
content: '请刷新网页更新',
positiveText: '刷新页面',
onPositiveClick: () => {
window.location.reload();
},
negativeText: '稍后检查',
onNegativeClick: () => {
// window.$dialog.destroyAll();
dialogShow.value = false;
},
onClose: () => {
// window.$dialog.destroyAll();
dialogShow.value = false;
},
});
}
});
}