|
@@ -117,37 +117,53 @@ let configLoaded = false;
|
|
|
router.beforeEach(async (to, _from, next) => {
|
|
router.beforeEach(async (to, _from, next) => {
|
|
|
const authStore = useAuthStore();
|
|
const authStore = useAuthStore();
|
|
|
const serverStore = useServerStore();
|
|
const serverStore = useServerStore();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 加载服务器配置(异步,仅执行一次)
|
|
// 加载服务器配置(异步,仅执行一次)
|
|
|
if (!configLoaded) {
|
|
if (!configLoaded) {
|
|
|
configLoaded = true;
|
|
configLoaded = true;
|
|
|
await serverStore.loadConfig();
|
|
await serverStore.loadConfig();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 检查服务器配置
|
|
// 检查服务器配置
|
|
|
if (!serverStore.isConfigured && to.name !== 'ServerConfig') {
|
|
if (!serverStore.isConfigured && to.name !== 'ServerConfig') {
|
|
|
next({ name: 'ServerConfig' });
|
|
next({ name: 'ServerConfig' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 首次进入需要认证的页面时,先检查认证状态
|
|
|
|
|
- if (!authInitialized && serverStore.isConfigured) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 如果服务未就绪,等待服务就绪(不阻止 /login 和 /register 页面)
|
|
|
|
|
+ if (!serverStore.servicesReady && !['Login', 'Register', 'ServerConfig'].includes(to.name as string)) {
|
|
|
|
|
+ // 等待服务就绪后再导航
|
|
|
|
|
+ const waitForServices = () => {
|
|
|
|
|
+ return new Promise<void>((resolve) => {
|
|
|
|
|
+ const checkInterval = setInterval(() => {
|
|
|
|
|
+ if (serverStore.servicesReady) {
|
|
|
|
|
+ clearInterval(checkInterval);
|
|
|
|
|
+ resolve();
|
|
|
|
|
+ }
|
|
|
|
|
+ }, 100);
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
+ await waitForServices();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 首次进入需要认证的页面时,先检查认证状态(仅在服务就绪后)
|
|
|
|
|
+ if (!authInitialized && serverStore.isConfigured && serverStore.servicesReady) {
|
|
|
authInitialized = true;
|
|
authInitialized = true;
|
|
|
await authStore.checkAuth();
|
|
await authStore.checkAuth();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 检查认证
|
|
// 检查认证
|
|
|
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
|
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
|
|
next({ name: 'Login' });
|
|
next({ name: 'Login' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 已登录用户不能访问登录/注册页
|
|
// 已登录用户不能访问登录/注册页
|
|
|
if (authStore.isAuthenticated && ['Login', 'Register'].includes(to.name as string)) {
|
|
if (authStore.isAuthenticated && ['Login', 'Register'].includes(to.name as string)) {
|
|
|
next({ name: 'Dashboard' });
|
|
next({ name: 'Dashboard' });
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
next();
|
|
next();
|
|
|
});
|
|
});
|
|
|
|
|
|