|
| 1 | +import axios from 'axios' |
| 2 | + |
| 3 | +// Create Axios instance with base configuration |
| 4 | +const axiosInstance = axios.create({ |
| 5 | + baseURL: process.env.APP_BASE_URL || '', // Default base URL |
| 6 | + timeout: 10000, |
| 7 | + headers: { |
| 8 | + 'Content-Type': 'application/json', |
| 9 | + }, |
| 10 | +}) |
| 11 | + |
| 12 | +// Request interceptor |
| 13 | +axiosInstance.interceptors.request.use( |
| 14 | + (config) => { |
| 15 | + // const token = localStorage.getItem('token'); |
| 16 | + // if (token) { |
| 17 | + // config.headers.Authorization = `Bearer ${token}`; |
| 18 | + // } |
| 19 | + return config |
| 20 | + }, |
| 21 | + (error) => { |
| 22 | + return Promise.reject(error) |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +// Response interceptor |
| 27 | +axiosInstance.interceptors.response.use( |
| 28 | + (response) => { |
| 29 | + return response.data |
| 30 | + }, |
| 31 | + (error) => { |
| 32 | + if (error.response) { |
| 33 | + // Handle different status codes |
| 34 | + switch (error.response.status) { |
| 35 | + case 401: |
| 36 | + // Handle unauthorized access |
| 37 | + localStorage.removeItem('token') |
| 38 | + // You might want to redirect to login page here |
| 39 | + break |
| 40 | + case 403: |
| 41 | + // Handle forbidden access |
| 42 | + break |
| 43 | + case 404: |
| 44 | + // Handle not found |
| 45 | + case 500: |
| 46 | + // Handle server error |
| 47 | + break |
| 48 | + } |
| 49 | + return Promise.reject(error.response.data) |
| 50 | + } |
| 51 | + return Promise.reject(error) |
| 52 | + } |
| 53 | +) |
| 54 | + |
| 55 | +// 保存文件到本地 |
| 56 | +const saveFile = (blob, filename) => { |
| 57 | + const url = window.URL.createObjectURL(blob) |
| 58 | + const link = document.createElement('a') |
| 59 | + link.href = url |
| 60 | + link.setAttribute('download', filename) |
| 61 | + document.body.appendChild(link) |
| 62 | + link.click() |
| 63 | + document.body.removeChild(link) |
| 64 | +} |
| 65 | + |
| 66 | +// 下载文件 |
| 67 | +export const downloadFile = async (url, data, onDownloadProgress, setProgress, setProgressStatus, filename) => { |
| 68 | + try { |
| 69 | + setProgressStatus('default') |
| 70 | + setProgress(0) |
| 71 | + const response = await axios({ |
| 72 | + method: 'POST', |
| 73 | + url: process.env.APP_BASE_URL + url, |
| 74 | + responseType: 'blob', |
| 75 | + data: data, |
| 76 | + onDownloadProgress: (progressEvent) => { |
| 77 | + if (progressEvent.lengthComputable) { |
| 78 | + const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total) |
| 79 | + setProgress(progress) |
| 80 | + } else { |
| 81 | + console.log('Download progress: unknown') |
| 82 | + } |
| 83 | + if (onDownloadProgress) { |
| 84 | + onDownloadProgress(progressEvent) |
| 85 | + } |
| 86 | + }, |
| 87 | + }) |
| 88 | + setProgressStatus('success') |
| 89 | + saveFile(response.data, filename) |
| 90 | + } catch (error) { |
| 91 | + setProgressStatus('exception') |
| 92 | + } finally { |
| 93 | + setProgress(100) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export default axiosInstance |
0 commit comments