Files
gongxue-base/apps/admin/src/api/index.ts
wangziqi b0f7883f33 feat(admin): remove all department/campus scoping from frontend
- Delete CampusSwitcher component and useCampus hook
- Delete Departments page and its route
- Remove campus header interceptor from API client
- Remove departmentId from Class interfaces
- Remove campusLocation from student profile
- Remove department permissions from test fixtures
- Remove currentCampusId from test cleanup

TypeScript compiles clean.
2026-07-09 17:40:57 +08:00

54 lines
1.6 KiB
TypeScript

import axios, { type AxiosRequestConfig } from 'axios';
const instance = axios.create({
baseURL: '/api',
timeout: 10000,
});
instance.interceptors.request.use((config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
instance.interceptors.response.use(
(res) => res.data,
(err) => {
if (err.response?.status === 401) {
localStorage.removeItem('token');
localStorage.removeItem('user');
localStorage.removeItem('permissions');
window.location.href = '/login';
}
if (err.response?.status === 403) {
const msg = err.response?.data?.message || '权限不足';
console.warn('[403]', msg);
}
return Promise.reject(err.response?.data || err);
},
);
const request = <T>(
method: string,
url: string,
data?: unknown,
config?: AxiosRequestConfig,
): Promise<T> => instance.request({ ...config, method, url, data }) as Promise<T>;
const api = {
get: <T>(url: string, config?: AxiosRequestConfig): Promise<T> =>
request<T>('get', url, undefined, config),
post: <T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T> =>
request<T>('post', url, data, config),
put: <T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T> =>
request<T>('put', url, data, config),
patch: <T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T> =>
request<T>('patch', url, data, config),
delete: <T>(url: string, config?: AxiosRequestConfig): Promise<T> =>
request<T>('delete', url, undefined, config),
};
export default api;