32 lines
820 B
JavaScript
32 lines
820 B
JavaScript
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 = `${token}`
|
|
}
|
|
|
|
return config
|
|
}, (error)=> {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
// 添加响应拦截器
|
|
http.interceptors.response.use((response)=> {
|
|
// 2xx 范围内的状态码都会触发该函数。
|
|
// 对响应数据做点什么
|
|
return response
|
|
}, (error)=> {
|
|
// 超出 2xx 范围的状态码都会触发该函数。
|
|
// 对响应错误做点什么
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
export { http } |