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,7 +1,7 @@
import { tgz, zip } from 'compressing'; import { tgz, zip } from 'compressing';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import packageJson from '../../package.json'; import packageJson from '../package.json';
const now = dayjs(); const now = dayjs();

17
build/pre-build.ts Normal file
View File

@@ -0,0 +1,17 @@
import { writeFile } from 'fs/promises';
import dayjs from 'dayjs';
import packageJson from '../package.json';
const now = dayjs();
const versionInfo = {
version: packageJson.version,
buildTime: now.format('YYYY-MM-DD HH:mm:ss'),
};
try {
await writeFile('./public/version.json', JSON.stringify(versionInfo, null, 2));
} catch (error) {
console.error('写入版本信息失败:', error);
}

View File

@@ -1,6 +1,6 @@
{ {
"name": "ndm-web-client-v", "name": "ndm-web-client-v",
"version": "0.0.0", "version": "0.1.0",
"private": true, "private": true,
"type": "module", "type": "module",
"engines": { "engines": {
@@ -8,9 +8,9 @@
}, },
"scripts": { "scripts": {
"dev": "vite --host", "dev": "vite --host",
"build": "run-p type-check \"build-only {@}\" --", "build": "tsx build/pre-build.ts && vite build && tsx build/post-build.ts",
"preview": "vite preview", "preview": "vite preview",
"build-only": "vite build && tsx src/utils/post-build.ts", "build-only": "vite build",
"type-check": "vue-tsc --build", "type-check": "vue-tsc --build",
"lint": "eslint . --fix", "lint": "eslint . --fix",
"format": "prettier --write src/" "format": "prettier --write src/"

View File

@@ -16,9 +16,12 @@ import { defineComponent } from 'vue';
import { useThemeStore } from '@/stores/theme'; import { useThemeStore } from '@/stores/theme';
import { storeToRefs } from 'pinia'; import { storeToRefs } from 'pinia';
import { VueQueryDevtools } from '@tanstack/vue-query-devtools'; import { VueQueryDevtools } from '@tanstack/vue-query-devtools';
import { useVersionCheckQuery } from './composables/query';
const themeStore = useThemeStore(); const themeStore = useThemeStore();
const { themeMode } = storeToRefs(themeStore); const { themeMode } = storeToRefs(themeStore);
useVersionCheckQuery();
</script> </script>
<template> <template>

View File

@@ -1,3 +1,4 @@
export * from './alarm'; export * from './alarm';
export * from './device'; export * from './device';
export * from './station'; 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;
},
});
}
});
}

View File

@@ -6,14 +6,16 @@
"cypress.config.*", "cypress.config.*",
"nightwatch.conf.*", "nightwatch.conf.*",
"playwright.config.*", "playwright.config.*",
"eslint.config.*" "eslint.config.*",
"build/**/*"
], ],
"compilerOptions": { "compilerOptions": {
"noEmit": true, "noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"module": "ESNext", "module": "ESNext",
"moduleResolution": "Bundler", "moduleResolution": "Bundler",
"types": ["node"] "types": [
"node"
]
} }
} }