PublicAxiosInstance.js
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import axios from 'axios'
import router from '@/router/index'
import Cookies from "js-cookie"
import {getTimestampString} from "@/PublicUtil/PublicUtil"
import {ElMessage} from 'element-plus'
// 创建 axios 的一个实例
const axiosInstance = axios.create(
{
baseURL: ''
}
)
// 请求拦截器
axiosInstance.interceptors.request.use(
config => {
// Cookies.set('atoken','90eeca8f-7d7e-4dfa-8d7b-468e033dd048')
config.headers.Authorization = Cookies.get('atoken')
return config
}
)
// 响应拦截器
axiosInstance.interceptors.response.use(
(r) => {
const responseData = r.data
if(responseData.ecode && responseData.ecode == '401'){
ElMessage({
message: `登录过期,请重新登录`,
type: 'warning'
})
// setTimeout(()=>{
// let url = ['localhost', '192.168.1.174'].includes(window.location.hostname) ? 'http://36.112.68.214:33333/' : window.location.origin
// window.location.href=url;
// },300)
return
}
return responseData
},
(e) => {
const responseData = e.response
return responseData
}
)
export default axiosInstance