feat: 添加版本更新检查功能
添加版本更新检查工具类Updater,用于定期检查前端版本更新 在App.vue中集成版本检查功能,检测到更新时提示用户刷新页面 移除不再使用的VanDatetimePicker组件声明
Showing
3 changed files
with
110 additions
and
2 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-20 19:53:12 | 2 | * @Date: 2025-03-20 19:53:12 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-05-08 09:50:42 | 4 | + * @LastEditTime: 2025-10-15 10:02:51 |
| 5 | * @FilePath: /mlaj/src/App.vue | 5 | * @FilePath: /mlaj/src/App.vue |
| 6 | * @Description: 入口文件 | 6 | * @Description: 入口文件 |
| 7 | --> | 7 | --> |
| ... | @@ -20,6 +20,8 @@ import { wxJsAPI } from "@/api/wx/config"; | ... | @@ -20,6 +20,8 @@ import { wxJsAPI } from "@/api/wx/config"; |
| 20 | import { apiList } from "@/api/wx/jsApiList.js"; | 20 | import { apiList } from "@/api/wx/jsApiList.js"; |
| 21 | import { wxInfo } from "@/utils/tools"; | 21 | import { wxInfo } from "@/utils/tools"; |
| 22 | 22 | ||
| 23 | +import { Updater } from '@/utils/versionUpdater'; | ||
| 24 | + | ||
| 23 | import 'vant/es/toast/style' | 25 | import 'vant/es/toast/style' |
| 24 | 26 | ||
| 25 | provideAuth(); // 提供全局认证状态 | 27 | provideAuth(); // 提供全局认证状态 |
| ... | @@ -47,6 +49,33 @@ const initWxConfig = async () => { | ... | @@ -47,6 +49,33 @@ const initWxConfig = async () => { |
| 47 | if (!import.meta.env.DEV && wxInfo().isWeiXin) { | 49 | if (!import.meta.env.DEV && wxInfo().isWeiXin) { |
| 48 | initWxConfig() | 50 | initWxConfig() |
| 49 | } | 51 | } |
| 52 | + | ||
| 53 | +// TAG:检查是否更新 | ||
| 54 | +let upDater = null; | ||
| 55 | +if (import.meta.env.PROD) { | ||
| 56 | + upDater = new Updater({ | ||
| 57 | + time: 30000 | ||
| 58 | + }) | ||
| 59 | + upDater.on('no-update', () => { | ||
| 60 | + // console.log('还没更新') | ||
| 61 | + }) | ||
| 62 | + upDater.on('update', () => { | ||
| 63 | + showConfirmDialog({ | ||
| 64 | + title: '温馨提示', | ||
| 65 | + message: '检测到新版本,是否刷新页面!', | ||
| 66 | + confirmButtonColor: styleColor.baseColor | ||
| 67 | + }).then(() => { | ||
| 68 | + window.location.reload(); | ||
| 69 | + }); | ||
| 70 | + }) | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +// 组件卸载时清理定时器 | ||
| 74 | +onUnmounted(() => { | ||
| 75 | + if (upDater) { | ||
| 76 | + upDater.destroy(); | ||
| 77 | + } | ||
| 78 | +}); | ||
| 50 | </script> | 79 | </script> |
| 51 | 80 | ||
| 52 | <template> | 81 | <template> | ... | ... |
| ... | @@ -44,7 +44,6 @@ declare module 'vue' { | ... | @@ -44,7 +44,6 @@ declare module 'vue' { |
| 44 | VanCol: typeof import('vant/es')['Col'] | 44 | VanCol: typeof import('vant/es')['Col'] |
| 45 | VanConfigProvider: typeof import('vant/es')['ConfigProvider'] | 45 | VanConfigProvider: typeof import('vant/es')['ConfigProvider'] |
| 46 | VanDatePicker: typeof import('vant/es')['DatePicker'] | 46 | VanDatePicker: typeof import('vant/es')['DatePicker'] |
| 47 | - VanDatetimePicker: typeof import('vant/es')['DatetimePicker'] | ||
| 48 | VanDialog: typeof import('vant/es')['Dialog'] | 47 | VanDialog: typeof import('vant/es')['Dialog'] |
| 49 | VanDivider: typeof import('vant/es')['Divider'] | 48 | VanDivider: typeof import('vant/es')['Divider'] |
| 50 | VanDropdownItem: typeof import('vant/es')['DropdownItem'] | 49 | VanDropdownItem: typeof import('vant/es')['DropdownItem'] | ... | ... |
src/utils/versionUpdater.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2024-02-06 11:38:13 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2025-09-30 22:33:40 | ||
| 5 | + * @FilePath: /data-table/src/utils/versionUpdater.js | ||
| 6 | + * @Description: | ||
| 7 | + */ | ||
| 8 | +/* eslint-disable */ | ||
| 9 | +/** | ||
| 10 | + * @description: 版本更新检查 | ||
| 11 | + * @param {*} time 阈值 | ||
| 12 | + * @return {*} | ||
| 13 | + */ | ||
| 14 | +export class Updater { | ||
| 15 | + constructor(options = {}) { | ||
| 16 | + this.oldScript = []; | ||
| 17 | + this.newScript = []; | ||
| 18 | + this.dispatch = {}; | ||
| 19 | + this.init(); //初始化 | ||
| 20 | + this.timing(options.time); //轮询 | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + async init() { | ||
| 24 | + const html = await this.getHtml(); | ||
| 25 | + this.oldScript = this.parserScript(html); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + async getHtml() { | ||
| 29 | + // TAG: html的位置需要动态修改 | ||
| 30 | + const html = await fetch(import.meta.env.VITE_BASE).then((res) => res.text()); //读取index html | ||
| 31 | + return html; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + parserScript(html) { | ||
| 35 | + const reg = new RegExp(/<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gi); //script正则 | ||
| 36 | + return html.match(reg); //匹配script标签 | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + //发布订阅通知 | ||
| 40 | + on(key, fn) { | ||
| 41 | + (this.dispatch[key] || (this.dispatch[key] = [])).push(fn); | ||
| 42 | + return this; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + compare(oldArr, newArr) { | ||
| 46 | + const base = oldArr.length; | ||
| 47 | + // 去重 | ||
| 48 | + const arr = Array.from(new Set(oldArr.concat(newArr))); | ||
| 49 | + //如果新旧length 一样无更新 | ||
| 50 | + if (arr.length === base) { | ||
| 51 | + this.dispatch['no-update'].forEach((fn) => { | ||
| 52 | + fn(); | ||
| 53 | + }); | ||
| 54 | + } else { | ||
| 55 | + //否则通知更新 | ||
| 56 | + this.dispatch['update'].forEach((fn) => { | ||
| 57 | + fn(); | ||
| 58 | + }); | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + timing(time = 10000) { | ||
| 63 | + //轮询 | ||
| 64 | + this.intervalId = setInterval(async () => { | ||
| 65 | + const newHtml = await this.getHtml(); | ||
| 66 | + this.newScript = this.parserScript(newHtml); | ||
| 67 | + this.compare(this.oldScript, this.newScript); | ||
| 68 | + }, time); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * 清理定时器 | ||
| 73 | + */ | ||
| 74 | + destroy() { | ||
| 75 | + if (this.intervalId) { | ||
| 76 | + clearInterval(this.intervalId); | ||
| 77 | + this.intervalId = null; | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | +} |
-
Please register or login to post a comment