wanshenmean
15 小时以前 f288ccc545f8cc32bc922c96dfb3cab9a1f92ec6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import axios from 'axios';
import type { AxiosInstance, AxiosResponse } from 'axios';
 
const baseURL = import.meta.env.VITE_APP_API_URL || 'http://localhost:9291';
 
const http: AxiosInstance = axios.create({
  baseURL,
  timeout: 30000,
});
 
http.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);
 
http.interceptors.response.use(
  (response: AxiosResponse) => response.data,
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token');
      window.location.href = '/login';
    }
    return Promise.reject(error);
  }
);
 
export interface LoginRequest {
  username: string;
  password: string;
}
 
export interface LoginResponse {
  success: boolean;
  message?: string;
  data?: {
    token: string;
  };
}
 
export const login = (data: LoginRequest): Promise<LoginResponse> => {
  return http.post('/api/Login', data);
};
 
export default http;