初步增加 web 界面
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 30000,
|
||||
});
|
||||
|
||||
// 请求拦截:携带 token
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// 响应拦截:统一错误处理
|
||||
api.interceptors.response.use(
|
||||
(response) => response.data,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
const msg = error.response?.data?.detail || error.message || '请求失败';
|
||||
return Promise.reject(new Error(msg));
|
||||
}
|
||||
);
|
||||
|
||||
export default api;
|
||||
@@ -0,0 +1,60 @@
|
||||
import api from './index';
|
||||
|
||||
export interface LoginResult {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
role: string;
|
||||
username: string;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export interface UserInfo {
|
||||
id: number;
|
||||
username: string;
|
||||
role: string;
|
||||
is_active: boolean;
|
||||
remark: string;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export const authApi = {
|
||||
login: (username: string, password: string) =>
|
||||
api.post<any, LoginResult>('/auth/login', { username, password }),
|
||||
|
||||
me: () => api.get<any, any>('/auth/me'),
|
||||
|
||||
logout: () => api.post<any, any>('/auth/logout'),
|
||||
};
|
||||
|
||||
export const userApi = {
|
||||
list: () => api.get<any, UserInfo[]>('/users'),
|
||||
create: (data: { username: string; password: string; role: string; remark?: string }) =>
|
||||
api.post<any, UserInfo>('/users', data),
|
||||
update: (id: number, data: { password?: string; role?: string; is_active?: boolean; remark?: string }) =>
|
||||
api.put<any, UserInfo>(`/users/${id}`, data),
|
||||
delete: (id: number) => api.delete<any, any>(`/users/${id}`),
|
||||
};
|
||||
|
||||
export const accountApi = {
|
||||
list: (assigned_only?: boolean) =>
|
||||
api.get<any, any[]>('/accounts', { params: assigned_only ? { assigned_only: true } : {} }),
|
||||
import: (text: string) => api.post<any, any>('/accounts/import', { text }),
|
||||
assign: (id: number, assigned_to: number | null) =>
|
||||
api.put<any, any>(`/accounts/${id}/assign`, { assigned_to }),
|
||||
delete: (id: number) => api.delete<any, any>(`/accounts/${id}`),
|
||||
};
|
||||
|
||||
export const loginApi = {
|
||||
createBatch: (account_ids: number[], max_geetest_retries?: number) =>
|
||||
api.post<any, any>('/login/batch', { account_ids, max_geetest_retries }),
|
||||
listTasks: (batch_id?: string) =>
|
||||
api.get<any, any[]>('/login/tasks', { params: batch_id ? { batch_id } : {} }),
|
||||
stop: (batch_id: string) => api.post<any, any>(`/login/stop/${batch_id}`),
|
||||
};
|
||||
|
||||
export const proxyApi = {
|
||||
get: () => api.get<any, any>('/proxy'),
|
||||
update: (data: any) => api.put<any, any>('/proxy', data),
|
||||
test: () => api.post<any, any>('/proxy/test'),
|
||||
testWhitelist: () => api.post<any, any>('/proxy/whitelist/test'),
|
||||
};
|
||||
Reference in New Issue
Block a user