This commit is contained in:
Baobhan Sith
2025-04-19 09:09:38 +08:00
parent c18db0546a
commit 1b7a2abb5c
11 changed files with 643 additions and 168 deletions
+20 -2
View File
@@ -31,6 +31,7 @@ interface AuthState {
entries: any[]; // TODO: Define a proper type for blacklist entries
total: number;
};
needsSetup: boolean; // 新增:是否需要初始设置
}
export const useAuthStore = defineStore('auth', {
@@ -41,6 +42,7 @@ export const useAuthStore = defineStore('auth', {
error: null,
loginRequires2FA: false, // 初始为不需要
ipBlacklist: { entries: [], total: 0 }, // 初始化黑名单状态
needsSetup: false, // 初始假设不需要设置
}),
getters: {
// 可以添加一些 getter,例如获取用户名
@@ -127,8 +129,8 @@ export const useAuthStore = defineStore('auth', {
this.error = null;
this.loginRequires2FA = false; // 重置 2FA 状态
try {
// TODO: 调用后端的登出 API
// await axios.post('/api/v1/auth/logout');
// 调用后端的登出 API
await axios.post('/api/v1/auth/logout');
// 清除本地状态
this.isAuthenticated = false;
@@ -277,6 +279,22 @@ export const useAuthStore = defineStore('auth', {
this.isLoading = false;
}
},
// 新增:检查是否需要初始设置
async checkSetupStatus() {
// 不需要设置 isLoading,这个检查应该在后台快速完成
try {
const response = await axios.get<{ needsSetup: boolean }>('/api/v1/auth/needs-setup');
this.needsSetup = response.data.needsSetup;
console.log(`[AuthStore] Needs setup status: ${this.needsSetup}`);
return this.needsSetup; // 返回状态给调用者
} catch (error: any) {
console.error('检查设置状态失败:', error.response?.data?.message || error.message);
// 如果检查失败,保守起见假设不需要设置,以避免卡在设置页面
this.needsSetup = false;
return false;
}
},
},
persist: true, // 使用默认持久化配置 (localStorage, 持久化所有 state)
});
@@ -83,7 +83,10 @@ export const useSettingsStore = defineStore('settings', () => {
} catch (err: any) {
console.error('加载通用设置失败:', err);
error.value = err.response?.data?.message || err.message || '加载设置失败';
setLocale(defaultLng); // 出错时使用默认语言
// 出错时(例如未登录),根据浏览器语言设置回退语言
const navigatorLang = navigator.language?.split('-')[0];
const fallbackLang = navigatorLang === 'zh' ? 'zh' : defaultLng;
setLocale(fallbackLang);
} finally {
isLoading.value = false;
}