You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
import axios from 'axios'
|
|
// 创建axios实例
|
|
const service = axios.create({
|
|
baseURL: import.meta.env.VITE_API_BASE,
|
|
timeout: 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
// 设置请求头,指定请求体的格式为 JSON
|
|
// 设置默认请求方法为 POST 这行对应的代码搁哪呢?
|
|
})
|
|
|
|
// 请求拦截器
|
|
service.interceptors.request.use(config => {
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
if (config.data) {
|
|
config.data.token = token;
|
|
} else {
|
|
config.data = { token };
|
|
}
|
|
// config.headers.Authorization = `${token}`
|
|
}
|
|
return config
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
// 响应拦截器
|
|
service.interceptors.response.use(
|
|
response => {
|
|
return response
|
|
},
|
|
error => {
|
|
// const { response } = error
|
|
// if (response && response.status === 401) {
|
|
// const machineId = localStorage.getItem('machineId')
|
|
// localStorage.removeItem('token')
|
|
// window.location.href = `/login?machineId=${machineId}`
|
|
// return Promise.resolve({ needsLogin: true })
|
|
// }
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
export default service
|