blob: c13c51697cf127c91ca596da8f073838e5338051 [file] [log] [blame]
San3yuan4d0e8032025-04-04 17:21:40 +08001import axios from 'axios'
2import type { AxiosRequestConfig, AxiosResponse } from 'axios'
3
4const instance = axios.create({
5 baseURL: process.env.API_BASE_URL,
6 timeout: 10000,
7 headers: {
8 'Content-Type': 'application/json'
9 }
10 })
11
12 // 请求拦截器
13 instance.interceptors.request.use(
14 (config) => {
San3yuana2ee30b2025-06-05 21:20:17 +080015 console.log('Request Config:', config)
San3yuan4d0e8032025-04-04 17:21:40 +080016 // 添加认证 token
San3yuan6f2ed692025-04-16 20:24:49 +080017 const token = localStorage.getItem('token')
San3yuan4d0e8032025-04-04 17:21:40 +080018 if (token) {
19 config.headers.Authorization = `Bearer ${token}`
20 }
21 return config
22 },
23 (error) => {
24 return Promise.reject(error)
25 }
26 )
27
28 // 响应拦截器
29 instance.interceptors.response.use(
30 (response) => {
San3yuan292794c2025-06-08 20:02:46 +080031 if(response.headers['content-type']!=='application/x-bittorrent')
32 {
33 if (response.status === 200) {
34 return response.data.data
35 }
36 return Promise.reject(response.data)
37 }else {
38 return response
San3yuan4d0e8032025-04-04 17:21:40 +080039 }
San3yuan4d0e8032025-04-04 17:21:40 +080040 },
41 (error) => {
San3yuan8166d1b2025-06-05 23:15:53 +080042 if(error.status===401){
San3yuanff75c542025-06-06 20:30:52 +080043 localStorage.removeItem('token');
44 // window.location.href = '/login';
San3yuan8166d1b2025-06-05 23:15:53 +080045 }
San3yuan4d0e8032025-04-04 17:21:40 +080046 // 统一错误处理
47 console.error('API Error:', error.response?.status, error.message)
48 return Promise.reject(error)
49 }
50 )
51
52export default instance