feat: 更新超级管理员登录逻辑,添加路由守卫和用户统计功能

This commit is contained in:
2025-12-23 16:28:21 +08:00
parent 3db41f4b0c
commit d09a9374ee
9 changed files with 508 additions and 180 deletions

View File

@@ -1,5 +1,6 @@
import AppLayout from '@/layout/AppLayout.vue';
import { createRouter, createWebHistory } from 'vue-router';
import { clearSuperAuthToken, hasSuperAuthToken, refreshSuperToken } from '@/service/auth';
const router = createRouter({
history: createWebHistory('/super/'),
@@ -7,6 +8,7 @@ const router = createRouter({
{
path: '/',
component: AppLayout,
meta: { requiresAuth: true },
children: [
{
path: '/',
@@ -153,4 +155,34 @@ const router = createRouter({
]
});
let tokenValidated = false;
let tokenValidationPromise = null;
router.beforeEach(async (to) => {
if (to.meta?.requiresAuth !== true) return true;
const isAuthed = hasSuperAuthToken();
if (!isAuthed) {
return {
name: 'login',
query: { redirect: to.fullPath }
};
}
if (!tokenValidated) {
tokenValidationPromise ??= refreshSuperToken();
try {
await tokenValidationPromise;
tokenValidated = true;
} catch {
clearSuperAuthToken();
tokenValidated = false;
tokenValidationPromise = null;
return { name: 'login', query: { redirect: to.fullPath } };
}
}
return true;
});
export default router;