feat: 重构前端
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
import apiClient from './index';
|
||||
|
||||
export interface LoginRequest {
|
||||
auth_key: string;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
success: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export const login = async (authKey: string): Promise<LoginResponse> => {
|
||||
const response = await apiClient.post<LoginResponse>('/auth/login', {
|
||||
auth_key: authKey
|
||||
});
|
||||
return response.data;
|
||||
};
|
@@ -1,11 +0,0 @@
|
||||
import request from './index';
|
||||
import type { DashboardStats } from '@/types/models';
|
||||
|
||||
export const getDashboardData = (timeRange: string, groupId: number | null): Promise<DashboardStats> => {
|
||||
const params = new URLSearchParams();
|
||||
params.append('time_range', timeRange);
|
||||
if (groupId) {
|
||||
params.append('group_id', groupId.toString());
|
||||
}
|
||||
return request.get(`/dashboard/data?${params.toString()}`);
|
||||
};
|
@@ -1,92 +0,0 @@
|
||||
import apiClient from "./index";
|
||||
import type { Group } from "../types/models";
|
||||
|
||||
/**
|
||||
* 获取所有分组列表
|
||||
*/
|
||||
export const fetchGroups = (): Promise<Group[]> => {
|
||||
return apiClient.get("/groups").then((res) => {
|
||||
const groups = res.data.data;
|
||||
// 将后端返回的 config 字符串解析为对象
|
||||
return groups.map((group: any) => ({
|
||||
...group,
|
||||
config:
|
||||
typeof group.config === "string"
|
||||
? JSON.parse(group.config)
|
||||
: group.config,
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取单个分组的详细信息
|
||||
* @param id 分组ID
|
||||
*/
|
||||
export const fetchGroup = (id: string): Promise<Group> => {
|
||||
return apiClient.get(`/groups/${id}`).then((res) => {
|
||||
const group = res.data.data;
|
||||
// 将后端返回的 config 字符串解析为对象
|
||||
return {
|
||||
...group,
|
||||
config:
|
||||
typeof group.config === "string"
|
||||
? JSON.parse(group.config)
|
||||
: group.config,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 创建一个新的分组
|
||||
* @param groupData 新分组的数据
|
||||
*/
|
||||
export const createGroup = (
|
||||
groupData: Omit<Group, "id" | "created_at" | "updated_at" | "api_keys">
|
||||
): Promise<Group> => {
|
||||
// 将 config 对象转换为 JSON 字符串,匹配后端期望的格式
|
||||
const requestData = {
|
||||
...groupData,
|
||||
config:
|
||||
typeof groupData.config === "object"
|
||||
? JSON.stringify(groupData.config)
|
||||
: groupData.config,
|
||||
};
|
||||
|
||||
console.log("createGroup - Original data:", groupData);
|
||||
console.log("createGroup - Request data:", requestData);
|
||||
console.log("createGroup - Config type:", typeof requestData.config);
|
||||
|
||||
return apiClient.post("/groups", requestData).then((res) => res.data.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 更新一个已存在的分组
|
||||
* @param id 分组ID
|
||||
* @param groupData 要更新的数据
|
||||
*/
|
||||
export const updateGroup = (
|
||||
id: string,
|
||||
groupData: Partial<
|
||||
Omit<Group, "id" | "created_at" | "updated_at" | "api_keys">
|
||||
>
|
||||
): Promise<Group> => {
|
||||
// 将 config 对象转换为 JSON 字符串,匹配后端期望的格式
|
||||
const requestData = {
|
||||
...groupData,
|
||||
config:
|
||||
groupData.config && typeof groupData.config === "object"
|
||||
? JSON.stringify(groupData.config)
|
||||
: groupData.config,
|
||||
};
|
||||
return apiClient
|
||||
.put(`/groups/${id}`, requestData)
|
||||
.then((res) => res.data.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 删除一个分组
|
||||
* @param id 分组ID
|
||||
*/
|
||||
export const deleteGroup = (id: string): Promise<void> => {
|
||||
return apiClient.delete(`/groups/${id}`).then((res) => res.data);
|
||||
};
|
@@ -1,50 +0,0 @@
|
||||
import axios from "axios";
|
||||
import { useAuthStore } from "@/stores/authStore";
|
||||
import router from "@/router";
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: "/api",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
// 请求拦截器:自动添加认证头
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
const authStore = useAuthStore();
|
||||
const authKey = authStore.getAuthKey();
|
||||
|
||||
if (authKey) {
|
||||
config.headers.Authorization = `Bearer ${authKey}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器:处理401认证失败
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// 认证失败,清除登录状态并跳转到登录页
|
||||
const authStore = useAuthStore();
|
||||
authStore.logout();
|
||||
|
||||
// 跳转到登录页(如果不在登录页的话)
|
||||
if (router.currentRoute.value.path !== '/login') {
|
||||
router.push('/login');
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default apiClient;
|
@@ -1,71 +0,0 @@
|
||||
import apiClient from "./index";
|
||||
import type { Key } from "../types/models";
|
||||
|
||||
/**
|
||||
* 获取指定分组下的所有密钥列表
|
||||
* @param groupId 分组ID
|
||||
*/
|
||||
export const fetchKeysInGroup = (groupId: string): Promise<Key[]> => {
|
||||
return apiClient.get(`/groups/${groupId}/keys`).then((res) => res.data.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 在指定分组下创建一个新的密钥
|
||||
* @param groupId 分组ID
|
||||
* @param keyData 新密钥的数据
|
||||
*/
|
||||
export const createKey = (
|
||||
groupId: string,
|
||||
keyData: Omit<
|
||||
Key,
|
||||
| "id"
|
||||
| "group_id"
|
||||
| "created_at"
|
||||
| "updated_at"
|
||||
| "request_count"
|
||||
| "failure_count"
|
||||
>
|
||||
): Promise<Key> => {
|
||||
return apiClient
|
||||
.post(`/groups/${groupId}/keys`, keyData)
|
||||
.then((res) => res.data.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 更新一个已存在的密钥
|
||||
* @param id 密钥ID
|
||||
* @param keyData 要更新的数据
|
||||
*/
|
||||
export const updateKey = (id: string, keyData: Partial<Key>): Promise<Key> => {
|
||||
return apiClient.put(`/keys/${id}`, keyData).then((res) => res.data.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 删除一个密钥
|
||||
* @param id 密钥ID
|
||||
*/
|
||||
export const deleteKey = (id: string): Promise<void> => {
|
||||
return apiClient.delete(`/keys/${id}`).then((res) => res.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 批量更新密钥
|
||||
* @param ids 密钥ID列表
|
||||
* @param data 要更新的数据
|
||||
*/
|
||||
export const batchUpdateKeys = (
|
||||
ids: string[],
|
||||
data: Partial<Key>
|
||||
): Promise<void> => {
|
||||
return apiClient
|
||||
.post("/keys/batch-update", { ids, data })
|
||||
.then((res) => res.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* 批量删除密钥
|
||||
* @param ids 密钥ID列表
|
||||
*/
|
||||
export const batchDeleteKeys = (ids: string[]): Promise<void> => {
|
||||
return apiClient.post("/keys/batch-delete", { ids }).then((res) => res.data);
|
||||
};
|
@@ -1,24 +0,0 @@
|
||||
import request from './index';
|
||||
import type { RequestLog } from '@/types/models';
|
||||
|
||||
export type { RequestLog };
|
||||
|
||||
export interface LogQuery {
|
||||
page?: number;
|
||||
size?: number;
|
||||
group_id?: number;
|
||||
start_time?: string;
|
||||
end_time?: string;
|
||||
status_code?: number;
|
||||
}
|
||||
|
||||
export interface PaginatedLogs {
|
||||
total: number;
|
||||
page: number;
|
||||
size: number;
|
||||
data: RequestLog[];
|
||||
}
|
||||
|
||||
export const getLogs = (query: LogQuery): Promise<PaginatedLogs> => {
|
||||
return request.get('/logs', { params: query });
|
||||
};
|
@@ -1,22 +0,0 @@
|
||||
import request from './index';
|
||||
import type { SettingCategory, SystemSettings } from '@/types/models';
|
||||
|
||||
// A generic function to get settings for a specific category
|
||||
export function getSettings<T>(category: SettingCategory) {
|
||||
// The backend API would need to support this, e.g., /api/settings/system
|
||||
return request.get<T>(`/settings/${category}`);
|
||||
}
|
||||
|
||||
// A generic function to update settings for a specific category
|
||||
export function updateSettings<T>(category: SettingCategory, settings: T) {
|
||||
return request.put(`/settings/${category}`, settings);
|
||||
}
|
||||
|
||||
// Specific functions for system settings as an example
|
||||
export function getSystemSettings() {
|
||||
return getSettings<SystemSettings>('system');
|
||||
}
|
||||
|
||||
export function updateSystemSettings(settings: SystemSettings) {
|
||||
return updateSettings('system', settings);
|
||||
}
|
Reference in New Issue
Block a user