hookehuyr

新增项目重新部署,页面提示刷新功能

1 <!-- 1 <!--
2 * @Date: 2023-06-13 13:26:46 2 * @Date: 2023-06-13 13:26:46
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-02-05 19:54:15 4 + * @LastEditTime: 2024-02-06 12:03:24
5 * @FilePath: /xysBooking/src/App.vue 5 * @FilePath: /xysBooking/src/App.vue
6 * @Description: 启动页 6 * @Description: 启动页
7 --> 7 -->
...@@ -21,7 +21,8 @@ import { setToastDefaultOptions } from 'vant'; ...@@ -21,7 +21,8 @@ import { setToastDefaultOptions } from 'vant';
21 import wx from 'weixin-js-sdk' 21 import wx from 'weixin-js-sdk'
22 import { wxJsAPI } from '@/api/wx/config' 22 import { wxJsAPI } from '@/api/wx/config'
23 import { apiList } from '@/api/wx/jsApiList.js' 23 import { apiList } from '@/api/wx/jsApiList.js'
24 - 24 +import { Updater } from '@/utils/versionUpdater'
25 +import { showConfirmDialog } from 'vant';
25 // 使用 include + pinia 状态管理动态缓存页面 26 // 使用 include + pinia 状态管理动态缓存页面
26 const store = mainStore() 27 const store = mainStore()
27 const keepPages = computed(() => store.getKeepPages) 28 const keepPages = computed(() => store.getKeepPages)
...@@ -47,9 +48,34 @@ onMounted(async () => { ...@@ -47,9 +48,34 @@ onMounted(async () => {
47 wx.showAllNonBaseMenuItem(); 48 wx.showAllNonBaseMenuItem();
48 }); 49 });
49 wx.error((err) => { 50 wx.error((err) => {
50 - console.warn('错误提示', err); 51 + console.warn(err);
52 + });
53 + // 正式环境 开启轮询 检查页面是否更新
54 + if (import.meta.env.PROD) {
55 + const upDater = new Updater({
56 + time: 5000
57 + })
58 + upDater.on('no-update', () => {
59 + // console.log('还没更新')
60 + })
61 + upDater.on('update', () => {
62 + // console.log('已经更新了,请刷新页面')
63 + showConfirmDialog({
64 + title: '温馨提示',
65 + message: '系统已经更新,请刷新页面?',
66 + confirmButtonColor: '#A67939',
67 + width: '80vw'
68 + })
69 + .then(() => {
70 + // on confirm
71 + window.location.reload();
72 + })
73 + .catch(() => {
74 + // on cancel
51 }); 75 });
52 -}) 76 + })
77 + }
78 +});
53 </script> 79 </script>
54 80
55 <style lang="less"> 81 <style lang="less">
......
1 /* 1 /*
2 * @Date: 2023-06-13 13:26:46 2 * @Date: 2023-06-13 13:26:46
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2024-02-05 21:13:21 4 + * @LastEditTime: 2024-02-06 12:05:22
5 * @FilePath: /xysBooking/src/route.js 5 * @FilePath: /xysBooking/src/route.js
6 * @Description: 路由列表 6 * @Description: 路由列表
7 */ 7 */
...@@ -21,7 +21,7 @@ export default [ ...@@ -21,7 +21,7 @@ export default [
21 }, 21 },
22 //路由的独享守卫 22 //路由的独享守卫
23 beforeEnter: (to,from,next) => { 23 beforeEnter: (to,from,next) => {
24 - console.warn(to, from); 24 + // console.warn(to, from);
25 next(); 25 next();
26 } 26 }
27 }, 27 },
......
1 +/*
2 + * @Date: 2024-02-06 11:38:13
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2024-02-06 12:01:32
5 + * @FilePath: /xysBooking/src/utils/versionUpdater.js
6 + * @Description: 文件描述
7 + */
8 +/* eslint-disable */
9 +export class Updater {
10 + constructor(options = {}) {
11 + this.oldScript = [];
12 + this.newScript = [];
13 + this.dispatch = {};
14 + this.init(); //初始化
15 + this.timing(options.time); //轮询
16 + }
17 +
18 + async init() {
19 + const html = await this.getHtml();
20 + this.oldScript = this.parserScript(html);
21 + }
22 +
23 + async getHtml() {
24 + // TAG: html的位置需要动态修改
25 + const html = await fetch(import.meta.env.VITE_BASE).then((res) => res.text()); //读取index html
26 + return html;
27 + }
28 +
29 + parserScript(html) {
30 + const reg = new RegExp(/<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/gi); //script正则
31 + return html.match(reg); //匹配script标签
32 + }
33 +
34 + //发布订阅通知
35 + on(key, fn) {
36 + (this.dispatch[key] || (this.dispatch[key] = [])).push(fn);
37 + return this;
38 + }
39 +
40 + compare(oldArr, newArr) {
41 + const base = oldArr.length;
42 + // 去重
43 + const arr = Array.from(new Set(oldArr.concat(newArr)));
44 + //如果新旧length 一样无更新
45 + if (arr.length === base) {
46 + this.dispatch['no-update'].forEach((fn) => {
47 + fn();
48 + });
49 + } else {
50 + //否则通知更新
51 + this.dispatch['update'].forEach((fn) => {
52 + fn();
53 + });
54 + }
55 + }
56 +
57 + timing(time = 10000) {
58 + //轮询
59 + setInterval(async () => {
60 + const newHtml = await this.getHtml();
61 + this.newScript = this.parserScript(newHtml);
62 + this.compare(this.oldScript, this.newScript);
63 + }, time);
64 + }
65 +}