38 lines
1.0 KiB
TypeScript
Executable File
38 lines
1.0 KiB
TypeScript
Executable File
import {defineNuxtRouteMiddleware} from "#app";
|
|
import {useToast} from 'vue-toastification'
|
|
|
|
|
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
|
const toast = useToast();
|
|
const runtimeConfig = useRuntimeConfig();
|
|
const token = useCookie('token').value;
|
|
|
|
if (!token) {
|
|
toast.error('未登录', {timeout: 3000})
|
|
return navigateTo("/SignIn");
|
|
}
|
|
//如果导航到/
|
|
if (to.path === '/' && token) {
|
|
return navigateTo("/Home");
|
|
}
|
|
|
|
// 获取当前导航路径
|
|
const currentPath = to.path;
|
|
try {
|
|
await $fetch('/Api/Rote/RoteVerify', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + token
|
|
},
|
|
baseURL: runtimeConfig.public.baseUrl,
|
|
params: {'path': currentPath}
|
|
});
|
|
// 如果验证成功,继续导航
|
|
return true;
|
|
} catch (err) {
|
|
toast.error('权限不足', {timeout: 3000});
|
|
return navigateTo("/error/403");
|
|
}
|
|
});
|