Przeglądaj źródła

刷新过的不再刷新,避免刷新不成功但一直刷新

lifanagju_citu 6 miesięcy temu
rodzic
commit
a0db7eeed9
1 zmienionych plików z 16 dodań i 13 usunięć
  1. 16 13
      src/App.vue

+ 16 - 13
src/App.vue

@@ -4,9 +4,8 @@ import { ref, onMounted, onUnmounted, nextTick } from 'vue'
 import Confirm from '@/plugins/confirm'
 import axios from 'axios'
 
-const openCheckVersion = true // 开启检测版本
 const timer = ref(null)
-const setIntervalTime = 3000 // 300000 五分钟
+const setIntervalTime = 3000 // 接口调用间隔时间
 
 function open () {
   if (timer.value) clearInterval(timer.value)
@@ -14,9 +13,9 @@ function open () {
 }
 onMounted(() => {
   nextTick(() => {
-    console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:', import.meta.env?.VITE_VERSION, process?.env?.NODE_ENV) // 打印
+    console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:') // 打印
     const process_ENV = process?.env?.NODE_ENV || ''
-    if ((process_ENV === 'production' || process_ENV === 'development') && openCheckVersion) {
+    if (process_ENV === 'production' || process_ENV === 'development') {
       open()
     }
   })
@@ -29,26 +28,30 @@ let ConfirmDone = false
 // 检查版本号
 const checkVersion = () => {
   const baseUrl = import.meta.env.VITE_BASE_URL || ''
-  const version = import.meta.env?.VITE_VERSION || ''
+  const env_v = import.meta.env?.VITE_VERSION || ''
   const tenantId = import.meta.env?.VITE_TENANTCODE || ''
+  const get_v = localStorage.getItem('RES_VERSION') || ''
   // 
-  if (!baseUrl || !version || !tenantId) return
+  if (!baseUrl || !env_v || !tenantId) return
   axios.get(`${baseUrl}/app-api/menduner/system/get/version`, {
     headers: { ['tenant-id']: tenantId },
     cache: 'no-store' // 禁用缓存
   }).then((res) => {
-    console.log('res-v', res?.data?.data, 'version', version)
-    // 接口报错和版本一致不弹Confirm
-    if (!res?.data?.data || res.data.data === version) {
+    const res_v = res?.data?.data || ''
+    if (!res_v || res_v === env_v ) { // 接口报错和版本一致不弹Confirm
       return
     }
     if (ConfirmDone) {
       return
     }
-    ConfirmDone = true
-    // Confirm('系统提示', '发现新版本,将立即刷新页面', { hideCancelBtn: true }).then(() => {
-    //   window.location.reload()
-    // })
+    if (res_v === get_v) { // 刷新过的不再刷新,避免刷新不成功但一直刷新
+      return
+    }
+    ConfirmDone = true  // 后续不弹Confirm
+    Confirm('系统提示', '发现新版本,将立即刷新页面', { hideCancelBtn: true }).then(() => {
+      localStorage.setItem('RES_VERSION', res_v)
+      window.location.reload()
+    })
   }).catch(err => {
     console.log('checkVersion-err', err)
   })