Files
MoviePilot-Frontend/src/api/index.ts
2023-06-24 16:25:23 +08:00

38 lines
763 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import router from '@/router'
import axios from 'axios'
// 创建axios实例
const api = axios.create({
baseURL: 'http://localhost:3001/api/v1',
})
// 添加请求拦截器
api.interceptors.request.use(config => {
const token = localStorage.getItem('token')
// 在请求头中添加token
if (token) {
config.headers['Authorization'] = `Bearer ${token}`
}
return config
})
// 添加响应拦截器
api.interceptors.response.use(response => {
return response.data
}, error => {
if (! error.response) {
// 请求超时
return Promise.reject(error)
} else if (error.response.status === 403) {
// token验证失败跳转到登录页面
router.push('/login')
}
return Promise.reject(error)
})
export default api