| import axios from "axios"; | |
| import { useNavigate } from "react-router"; | |
| // 为所有auth外请求添加token头 | |
| axios.interceptors.request.use((config) => { | |
| const requestUrl = config.url; | |
| if (requestUrl?.includes("/auth/")) { | |
| config.url = requestUrl.replace("/auth/","/"); | |
| } else { | |
| const token = localStorage.getItem('token'); | |
| if (!token) { | |
| const navigate = useNavigate(); | |
| navigate("/login") | |
| } | |
| config.headers['Authorization'] = `Bearer ${token}`; | |
| } | |
| return config; | |
| }, (error) => { | |
| return error; | |
| } ); |