qx
qx
2025-05-07 55955f56fdae8977e6372c333a13e0d60ecb3397
qx
2个文件已修改
1个文件已添加
82 ■■■■■ 已修改文件
src/main.js 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/AutoUpdate.js 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/login.vue 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main.js
@@ -14,6 +14,20 @@
import plugins from "./plugins"; // plugins
import { download } from "@/utils/request";
import Print from "vue-print-nb";
import Updater from "./utils/AutoUpdate.js";
//前端重新部署通知用户刷新网页
const AutoUpdate = new Updater()
AutoUpdate.on('update',()=>{
  setTimeout(async()=>{
      const result = confirm('当前版本已更新,请点击确定刷新页面体验');
      if(result){
        location.reload();
      }
  },500)
})
import JsonExcel from "vue-json-excel";
src/utils/AutoUpdate.js
New file
@@ -0,0 +1,66 @@
/**
 * 前端重新部署通知用户刷新网页
 */
class Updater {
    oldScript = []; // 存储第一次值也就是script 的hash 信息
    newScript = []; // 获取新的值 也就是新的script 的hash信息
    dispatch = {}; // 小型发布订阅通知用户更新了
    constructor() {
        this.oldScript = [];
        this.newScript = [];
        this.dispatch = {};
        this.init(); // 初始化
        this.timing();
    }
    async init() {
        const html = await this.getHtml();
        this.oldScript = this.parserScript(html);
    };
    async getHtml() {
        const html = await fetch('/').then(res => res.text());//读取index html
        return html
    };
    parserScript(html) {
        const reg = new RegExp(/<script(?:\s+[^>]*)?>(.*?)<\/script\s*>/ig) //script正则
        return html.match(reg) //匹配script标签
    }
    //发布订阅通知
    on(key, fn) {
        (this.dispatch[key] || (this.dispatch[key] = [])).push(fn)
        return this;
    }
    compare(oldArr, newArr) {
        const base = oldArr.length;
        const arr = Array.from(new Set(oldArr.concat(newArr)));
        //如果新旧length 一样无更新
        if (arr.length === base) {
            // this.dispatch['no-update'].forEach(fn => {
            //     fn();
            // })
        } else {
            // 否则通知更新
            this.dispatch['update'].forEach(fn => {
                fn();
            })
        }
    };
    async timing() {
        setInterval(async () => {
            const newHtml = await this.getHtml();
            this.newScript = this.parserScript(newHtml);
            this.compare(this.oldScript, this.newScript)
            //这边给的是默认值15000,也可以自定义秒数
        }, 15000);
    };
}
export default Updater;
src/views/login.vue
@@ -233,6 +233,8 @@
                }
              }
              this.$router.push({ path: this.redirect || "/" }).catch(() => { });
              console.log(11115555)
              location.reload();
            }
          }).catch(() => {