PublicAxiosInstance.js
1021 Bytes
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
import axios from 'axios'
import router from '@/router/index'
import Cookies from "js-cookie"
import {getTimestampString} from "@/PublicUtil/PublicUtil"
// 创建 axios 的一个实例
const axiosInstance = axios.create(
{
baseURL: window._baseUrl
}
)
// 请求拦截器
axiosInstance.interceptors.request.use(
config => {
if (window.location.hostname === 'localhost')
{
config.headers.Authorization = '68e02bdc-5815-44e1-94c2-8a01d3270c4c'
}
else
{
const authorization = Cookies.get('atoken')
if (authorization !== undefined)
{
config.headers.Authorization = authorization
}
}
return config
}
)
// 响应拦截器
axiosInstance.interceptors.response.use(
(r) => {
const responseData = r.data
return responseData
},
(e) => {
const responseData = e.response
return responseData
}
)
export default axiosInstance