This commit is contained in:
Baobhan Sith
2025-04-14 22:51:05 +08:00
parent 286492fc63
commit a974b8b1d9
49 changed files with 13954 additions and 0 deletions
@@ -0,0 +1,82 @@
import { defineStore } from 'pinia';
import axios from 'axios';
import router from '../router'; // 引入 router 用于重定向
// 用户信息接口 (不含敏感信息)
interface UserInfo {
id: number;
username: string;
}
// Auth Store State 接口
interface AuthState {
isAuthenticated: boolean;
user: UserInfo | null;
isLoading: boolean;
error: string | null;
}
export const useAuthStore = defineStore('auth', {
state: (): AuthState => ({
isAuthenticated: false, // 初始为未登录
user: null,
isLoading: false,
error: null,
}),
getters: {
// 可以添加一些 getter,例如获取用户名
loggedInUser: (state) => state.user?.username,
},
actions: {
// 登录 Action
async login(credentials: { username: string; password: string }) {
this.isLoading = true;
this.error = null;
try {
const response = await axios.post<{ message: string; user: UserInfo }>('/api/v1/auth/login', credentials);
// 登录成功
this.isAuthenticated = true;
this.user = response.data.user;
console.log('登录成功:', this.user);
// 登录成功后重定向到连接管理页面 (或仪表盘)
await router.push({ name: 'Connections' }); // 使用 await 确保导航完成
return true;
} catch (err: any) {
console.error('登录失败:', err);
this.isAuthenticated = false;
this.user = null;
this.error = err.response?.data?.message || err.message || '登录时发生未知错误。';
return false;
} finally {
this.isLoading = false;
}
},
// 登出 Action (占位符)
async logout() {
this.isLoading = true;
this.error = null;
try {
// TODO: 调用后端的登出 API (如果需要)
// await axios.post('/api/v1/auth/logout');
// 清除本地状态
this.isAuthenticated = false;
this.user = null;
console.log('已登出');
// 登出后重定向到登录页
await router.push({ name: 'Login' });
} catch (err: any) {
console.error('登出失败:', err);
this.error = err.response?.data?.message || err.message || '登出时发生未知错误。';
} finally {
this.isLoading = false;
}
},
// TODO: 添加检查登录状态的 Action (例如应用启动时调用)
// async checkAuthStatus() { ... }
},
// 可选:开启持久化 (例如使用 pinia-plugin-persistedstate)
// persist: true,
});
@@ -0,0 +1,74 @@
import { defineStore } from 'pinia';
import axios from 'axios'; // 引入 axios
// 定义连接信息接口 (与后端对应,不含敏感信息)
export interface ConnectionInfo {
id: number;
name: string;
host: string;
port: number;
username: string;
auth_method: 'password';
created_at: number;
updated_at: number;
last_connected_at: number | null;
}
// 定义 Store State 的接口
interface ConnectionsState {
connections: ConnectionInfo[];
isLoading: boolean;
error: string | null;
}
// 定义 Pinia Store
export const useConnectionsStore = defineStore('connections', {
state: (): ConnectionsState => ({
connections: [],
isLoading: false,
error: null,
}),
actions: {
// 获取连接列表 Action
async fetchConnections() {
this.isLoading = true;
this.error = null;
try {
// 注意:axios 默认会携带 cookie,因此如果用户已登录,会话 cookie 会被发送
const response = await axios.get<ConnectionInfo[]>('/api/v1/connections');
this.connections = response.data;
} catch (err: any) {
console.error('获取连接列表失败:', err);
this.error = err.response?.data?.message || err.message || '获取连接列表时发生未知错误。';
// 如果是 401 未授权,可能需要触发重新登录逻辑
if (err.response?.status === 401) {
// TODO: 处理未授权情况,例如跳转到登录页
console.warn('未授权,需要登录才能获取连接列表。');
}
} finally {
this.isLoading = false;
}
},
// 添加新连接 Action
async addConnection(newConnectionData: { name: string; host: string; port: number; username: string; password: string }) {
this.isLoading = true; // 可以为添加操作单独设置加载状态,或共用 isLoading
this.error = null;
try {
const response = await axios.post<{ message: string; connection: ConnectionInfo }>('/api/v1/connections', newConnectionData);
// 添加成功后,将新连接添加到列表前面 (或重新获取整个列表)
this.connections.unshift(response.data.connection);
return true; // 表示成功
} catch (err: any) {
console.error('添加连接失败:', err);
this.error = err.response?.data?.message || err.message || '添加连接时发生未知错误。';
if (err.response?.status === 401) {
console.warn('未授权,需要登录才能添加连接。');
}
return false; // 表示失败
} finally {
this.isLoading = false;
}
},
},
});