feat: 接入权限接口
This commit is contained in:
@@ -1,9 +1,69 @@
|
||||
<script setup lang="ts">
|
||||
import { RouterView } from 'vue-router'
|
||||
import { onMounted, onUnmounted, watch } from 'vue'
|
||||
import { RouterView, useRoute, useRouter } from 'vue-router'
|
||||
|
||||
import Container from '@/components/layout/Container.vue'
|
||||
import Header from '@/components/layout/Header.vue'
|
||||
import Menu from '@/components/layout/Menu.vue'
|
||||
import { useLoginStore } from '@/stores/login'
|
||||
|
||||
const loginStore = useLoginStore()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
let authorizationTimer: ReturnType<typeof setInterval> | undefined
|
||||
let authorizationRefreshing = false
|
||||
|
||||
async function redirectIfUnauthorized() {
|
||||
const resource = loginStore.menuForPath(route.path)
|
||||
if (
|
||||
route.name === 'menu-resource' &&
|
||||
(!resource ||
|
||||
resource.resourceType !== 'MENU' ||
|
||||
resource.status !== 'ENABLED' ||
|
||||
!resource.code ||
|
||||
!loginStore.hasPermission(resource.code))
|
||||
) {
|
||||
await router.replace(loginStore.firstAuthorizedPath ?? '/forbidden')
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAuthorization() {
|
||||
if (authorizationRefreshing) return
|
||||
authorizationRefreshing = true
|
||||
try {
|
||||
await loginStore.refreshAuthorization()
|
||||
await redirectIfUnauthorized()
|
||||
} catch {
|
||||
// A transient refresh error should not interrupt an otherwise active session.
|
||||
} finally {
|
||||
authorizationRefreshing = false
|
||||
}
|
||||
}
|
||||
|
||||
function handleWindowFocus() {
|
||||
void refreshAuthorization()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
authorizationTimer = setInterval(() => void refreshAuthorization(), 15_000)
|
||||
window.addEventListener('focus', handleWindowFocus)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (authorizationTimer) {
|
||||
clearInterval(authorizationTimer)
|
||||
}
|
||||
window.removeEventListener('focus', handleWindowFocus)
|
||||
})
|
||||
|
||||
watch(
|
||||
() => [route.path, loginStore.authorization] as const,
|
||||
() => {
|
||||
if (loginStore.authorization) {
|
||||
void redirectIfUnauthorized()
|
||||
}
|
||||
},
|
||||
)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user