From 4ba37b3cfe00df516d714f529203093a3c6274f5 Mon Sep 17 00:00:00 2001 From: scout <1134087124@qq.com> Date: Mon, 11 Mar 2024 11:28:51 +0800 Subject: [PATCH] add autoupdate --- src/main.js | 2 +- src/utils/auto-update.js | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/utils/auto-update.js diff --git a/src/main.js b/src/main.js index 2cfa29a..1a7fe23 100644 --- a/src/main.js +++ b/src/main.js @@ -8,7 +8,7 @@ import Antd from "ant-design-vue"; import "ant-design-vue/dist/reset.css"; import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' - +import './utils/auto-update' async function bootstrap() { const app = createApp(App) setupAssets() diff --git a/src/utils/auto-update.js b/src/utils/auto-update.js new file mode 100644 index 0000000..19b1c5b --- /dev/null +++ b/src/utils/auto-update.js @@ -0,0 +1,60 @@ +import {ElMessage, ElMessageBox} from 'element-plus' + +let lastSrcs; + +const scriptReg = /\[^"']+)/gm; + +// 获取最新的 script src +async function extractNewScripts() { + const html = await fetch("/?_timestamp=" + Date.now()).then((res) => + res.text() + ); + scriptReg.lastIndex = 0; + let result = []; + let match; + while ((match = scriptReg.exec(html))) { + result.push(match.groups.src); + + } + return result; +} +// 判断是否需要更新 +async function needUpdate() { + const newScripts = await extractNewScripts(); + if (!lastSrcs) { + lastSrcs = newScripts; + return false; + } + let result = false; + if (newScripts.length !== lastSrcs.length) { + result = true; + } else { + for (let i = 0; i < newScripts.length; i++) { + if (newScripts[i] !== lastSrcs[i]) { + result = true; + break; + } + } + } + lastSrcs = newScripts; + return result; +} +// 间隔五分钟 +const DURATION = 100 * 6 * 5; +function autoRefresh() { + setTimeout(async () => { + const willUpdate = await needUpdate(); + if (willUpdate) { + ElMessageBox.confirm('点击确定刷新页面,取消则继续使用旧版本', '提示', { + confirmButtonText: '确定', + cancelButtonText: '取消', + type: 'warning' + }).then(async () => { + + }) + } + autoRefresh(); + }, DURATION); +} + +autoRefresh();