Browse Source

feat: 退出前清理评卷任务

chenhao 2 years ago
parent
commit
fa9507a34c
3 changed files with 20 additions and 2 deletions
  1. 6 0
      src/bootstrap.ts
  2. 1 1
      src/plugins/request/index.ts
  3. 13 1
      src/utils/shared.ts

+ 6 - 0
src/bootstrap.ts

@@ -1,3 +1,9 @@
+import { clearTasks } from '@/utils/shared'
+
 export default () => {
   console.log('bootstrap!')
+  /** 不一定正常退出, 补偿处理 */
+  window.addEventListener('beforeunload', () => {
+    clearTasks()
+  })
 }

+ 1 - 1
src/plugins/request/index.ts

@@ -62,7 +62,7 @@ request.interceptors.response.use(
           error.response &&
           ([401, 403].includes(error.response.status) || error.response.data?.code?.toString()?.startsWith('401'))
         ) {
-          logout()
+          logout(false)
           ElMessage.error({ message: '登录状态已过期', key: 'token-valid' })
           return Promise.reject(error)
         }

+ 13 - 1
src/utils/shared.ts

@@ -1,8 +1,20 @@
 import router from '@/router'
 import { sessionStorage } from '@/plugins/storage'
+import useFetch from '@/hooks/useFetch'
+
+/** 清理评卷任务 */
+export const clearTasks = () => {
+  const loginResult = sessionStorage.get('LOGIN_RESULT')
+  if (loginResult?.role === 'MARKER') {
+    useFetch('clearCachedTasks')
+  }
+}
 
 /** 退出登录 */
-export const logout = () => {
+export const logout = (clear = true) => {
+  if (clear) {
+    clearTasks()
+  }
   sessionStorage.remove('LOGIN_RESULT')
   router.replace({ name: 'Login' })
 }