map/src/utils/http.js

32 lines
827 B
JavaScript
Raw Normal View History

2023-08-30 13:55:59 +08:00
import axios from "axios";
import { getToken } from "./token";
const http = axios.create({
baseURL: 'http://8.142.119.212',
timeout: 5000
})
// 添加请求拦截器
http.interceptors.request.use((config)=> {
// 请求拦截注入token
const token = getToken()
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
}, (error)=> {
return Promise.reject(error)
})
// 添加响应拦截器
http.interceptors.response.use((response)=> {
// 2xx 范围内的状态码都会触发该函数。
// 对响应数据做点什么
return response
}, (error)=> {
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么
return Promise.reject(error)
})
export { http }