Browse Source

在线状态的定时请求的bug修复

刘洋 11 months ago
parent
commit
298d579930
2 changed files with 52 additions and 6 deletions
  1. 19 6
      src/App.vue
  2. 33 0
      src/utils/onlineInterval.worker.ts

+ 19 - 6
src/App.vue

@@ -7,10 +7,14 @@ import LockScreen from '@/layout/LockScreen.vue'
 import PauseMarker from '@/layout/PauseMarker.vue'
 import LoadingFlag from '@/components/common/LoadingFlag.vue'
 import RowNextBottomDialog from '@/components/common/RowNextBottomDialog.vue'
-import { onMounted, watch } from 'vue'
+import { onMounted, watch, ref } from 'vue'
 import { useRoute } from 'vue-router'
 import useFetch from './hooks/useFetch'
-import { useIntervalFn } from '@vueuse/core'
+// import { useIntervalFn } from '@vueuse/core'
+import OnlineIntervalWorker from '@/utils/onlineInterval.worker?worker'
+
+const onlineIntervalWorker = new OnlineIntervalWorker()
+const isActive = ref(false)
 const route = useRoute()
 const mainStore = useMainStore()
 const mainLayoutStore = useMainLayoutStore()
@@ -22,14 +26,23 @@ if (location.pathname == '/marking/mark') {
 const onlineCheckInterval = () => {
   useFetch('onlineCheck').fetch()
 }
-const { pause, isActive, resume } = useIntervalFn(onlineCheckInterval, 5000, { immediate: false })
+onlineIntervalWorker.onmessage = (e: any) => {
+  const data = e.data
+  console.log('data', data)
+  if (typeof e.data === 'boolean') {
+    isActive.value = !!data
+  } else if (e.data === '') {
+    onlineCheckInterval()
+  }
+}
+
 watch(
   () => mainStore.loginInfo,
   (val) => {
     if (val) {
-      resume()
+      onlineIntervalWorker.postMessage(true)
     } else {
-      pause()
+      onlineIntervalWorker.postMessage(false)
     }
   }
 )
@@ -39,7 +52,7 @@ if (mainStore.loginInfo) {
   mainStore.getUserMarkConfig()
   mainLayoutStore.getRenderMenuList()
   if (!isActive.value) {
-    resume()
+    onlineIntervalWorker.postMessage(true)
   }
 }
 window.addEventListener('online', () => {

+ 33 - 0
src/utils/onlineInterval.worker.ts

@@ -0,0 +1,33 @@
+class OnlineInterval {
+  timer: any = null
+  send() {
+    postMessage('')
+  }
+  isActive = false
+
+  pause() {
+    if (this.timer) {
+      clearInterval(this.timer)
+      this.timer = null
+    }
+    this.isActive = false
+    postMessage(false)
+  }
+  resume() {
+    const func = this.send
+    this.timer = setInterval(func, 5000)
+    this.isActive = true
+    postMessage(true)
+  }
+}
+
+const onlineInterval = new OnlineInterval()
+
+addEventListener('message', (e: any) => {
+  const data = e.data as boolean
+  if (data) {
+    onlineInterval.resume()
+  } else {
+    onlineInterval.pause()
+  }
+})