|
|
@@ -4,11 +4,15 @@ import { authApi } from '@/api/auth';
|
|
|
import { accountsApi } from '@/api/accounts';
|
|
|
import type { User, LoginRequest } from '@media-manager/shared';
|
|
|
import { useServerStore } from './server';
|
|
|
+import { useTaskQueueStore } from './taskQueue';
|
|
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
|
const user = ref<User | null>(null);
|
|
|
const accessToken = ref<string | null>(null);
|
|
|
const refreshToken = ref<string | null>(null);
|
|
|
+ let autoAccountSyncTimer: ReturnType<typeof setInterval> | null = null;
|
|
|
+ let autoAccountSyncTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
|
+ let autoAccountSyncRunning = false;
|
|
|
|
|
|
const isAuthenticated = computed(() => !!accessToken.value && !!user.value);
|
|
|
const isAdmin = computed(() => user.value?.role === 'admin');
|
|
|
@@ -38,6 +42,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
accessToken.value = null;
|
|
|
refreshToken.value = null;
|
|
|
user.value = null;
|
|
|
+ stopAutoAccountSync();
|
|
|
localStorage.removeItem(`${serverKey}_accessToken`);
|
|
|
localStorage.removeItem(`${serverKey}_refreshToken`);
|
|
|
}
|
|
|
@@ -48,25 +53,51 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
saveTokens(result.accessToken, result.refreshToken);
|
|
|
user.value = result.user;
|
|
|
|
|
|
- // 登录成功后自动刷新所有账号状态
|
|
|
- refreshAllAccountsInBackground();
|
|
|
+ startAutoAccountSync();
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- // 后台刷新所有账号状态(延迟执行,避免服务启动时网络不稳定导致误判)
|
|
|
- async function refreshAllAccountsInBackground(delay = 5000) {
|
|
|
- // 延迟执行,给后端服务足够的启动时间
|
|
|
- setTimeout(async () => {
|
|
|
- try {
|
|
|
- console.log('[Auth] Starting background account refresh...');
|
|
|
- await accountsApi.refreshAllAccounts();
|
|
|
- console.log('[Auth] Background account refresh completed');
|
|
|
- } catch (error) {
|
|
|
- // 刷新失败不应影响用户体验,静默处理
|
|
|
- console.warn('[Auth] Background account refresh failed:', error);
|
|
|
+ async function enqueueSyncAccountTasks(): Promise<void> {
|
|
|
+ if (!isAuthenticated.value || autoAccountSyncRunning) return;
|
|
|
+ autoAccountSyncRunning = true;
|
|
|
+
|
|
|
+ try {
|
|
|
+ const taskStore = useTaskQueueStore();
|
|
|
+ const accounts = await accountsApi.getAccounts();
|
|
|
+
|
|
|
+ for (const account of accounts) {
|
|
|
+ await taskStore.syncAccount(account.id, account.accountName);
|
|
|
}
|
|
|
- }, delay);
|
|
|
+ } catch (error) {
|
|
|
+ console.warn('[Auth] Auto account sync failed:', error);
|
|
|
+ } finally {
|
|
|
+ autoAccountSyncRunning = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function startAutoAccountSync(initialDelay = 5000) {
|
|
|
+ stopAutoAccountSync();
|
|
|
+
|
|
|
+ autoAccountSyncTimeout = setTimeout(() => {
|
|
|
+ enqueueSyncAccountTasks();
|
|
|
+ }, initialDelay);
|
|
|
+
|
|
|
+ const intervalMs = 10 * 60 * 1000;
|
|
|
+ autoAccountSyncTimer = setInterval(() => {
|
|
|
+ enqueueSyncAccountTasks();
|
|
|
+ }, intervalMs);
|
|
|
+ }
|
|
|
+
|
|
|
+ function stopAutoAccountSync() {
|
|
|
+ if (autoAccountSyncTimeout) {
|
|
|
+ clearTimeout(autoAccountSyncTimeout);
|
|
|
+ autoAccountSyncTimeout = null;
|
|
|
+ }
|
|
|
+ if (autoAccountSyncTimer) {
|
|
|
+ clearInterval(autoAccountSyncTimer);
|
|
|
+ autoAccountSyncTimer = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 注册
|
|
|
@@ -82,8 +113,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
try {
|
|
|
user.value = await authApi.getMe();
|
|
|
|
|
|
- // 应用启动时自动刷新所有账号状态
|
|
|
- refreshAllAccountsInBackground();
|
|
|
+ startAutoAccountSync();
|
|
|
|
|
|
return true;
|
|
|
} catch {
|
|
|
@@ -97,8 +127,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|
|
localStorage.setItem(`${serverKey}_accessToken`, result.accessToken);
|
|
|
user.value = await authApi.getMe();
|
|
|
|
|
|
- // Token 刷新成功后也刷新账号状态
|
|
|
- refreshAllAccountsInBackground();
|
|
|
+ startAutoAccountSync();
|
|
|
|
|
|
return true;
|
|
|
} catch {
|