
import { MsgConfirm } from '@/utils/message'
// @ts-ignore
const cachedBuildTime = window.__BUILD_TIME__;
let flag = true;

console.info('[ 版本检测已加载... ]');

setInterval(async () => {
  try {
    let url = window.location.origin+window.location.pathname
    if(url.indexOf('index.html') === -1){
      url+=`index.html?t=${Date.now()}`
    } else {
      url+=`?t=${Date.now()}`
    }
    const response = await fetch(url); // 加时间戳避免缓存
    const html = await response.text();
    const newBuildTime = html.match(/window\.__BUILD_TIME__ = '(.*?)'/)?.[1];
    if (newBuildTime && newBuildTime !== cachedBuildTime && flag) {
      flag = false
      // 触发更新提示逻辑
      console.log('检测到新版本，请刷新页面');
      MsgConfirm(
        // @ts-ignore
        `系统版本更新通知`,
        '检测到系统有新版本发布，是否立即刷新页面？',
        {
          confirmButtonText: '立即刷新',
          cancelButtonText: '稍后再说',
        }
      )
        .then(() => {
          window.location.reload() 
        })
        .catch(() => {})
    }
  } catch (error) {
    console.error('版本检测失败', error);
  }
}, 1000 * 60); // 每1分钟检测一次
