29 lines
598 B
TypeScript
29 lines
598 B
TypeScript
|
|
import axios from "axios";
|
|||
|
|
|
|||
|
|
const http = axios.create({
|
|||
|
|
baseURL: "",
|
|||
|
|
timeout: 15000
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
http.interceptors.request.use((config) => {
|
|||
|
|
const token = localStorage.getItem("accessToken");
|
|||
|
|
if (token) {
|
|||
|
|
config.headers = config.headers || {};
|
|||
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|||
|
|
}
|
|||
|
|
return config;
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
http.interceptors.response.use(
|
|||
|
|
(resp) => {
|
|||
|
|
const body = resp.data;
|
|||
|
|
if (body && body.code !== "0") {
|
|||
|
|
return Promise.reject(new Error(body.msg || "请求失败"));
|
|||
|
|
}
|
|||
|
|
return resp;
|
|||
|
|
},
|
|||
|
|
(error) => Promise.reject(error)
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
export default http;
|