22301014 | 6abadd7 | 2025-06-05 14:21:13 +0800 | [diff] [blame^] | 1 | import axios from "axios"; |
| 2 | import { useNavigate } from "react-router"; |
| 3 | |
| 4 | // 为所有auth外请求添加token头 |
| 5 | axios.interceptors.request.use((config) => { |
| 6 | const requestUrl = config.url; |
| 7 | if (requestUrl?.includes("/auth/")) { |
| 8 | config.url = requestUrl.replace("/auth/","/"); |
| 9 | } else { |
| 10 | const token = localStorage.getItem('token'); |
| 11 | if (!token) { |
| 12 | const navigate = useNavigate(); |
| 13 | navigate("/login") |
| 14 | } |
| 15 | config.headers['Authorization'] = `Bearer ${token}`; |
| 16 | } |
| 17 | return config; |
| 18 | }, (error) => { |
| 19 | return error; |
| 20 | } ); |