axios全局请求参数设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import qs from 'qs'
const axios = window.axios
axios.interceptors.request.use(function (config) {
let noche = new Date().getTime()
if (config.method === 'post') {
let data = qs.parse(config.data)
config.data = qs.stringify({
noche,
...data
})
} else if (config.method === 'get') {
config.params = {
noche,
...config.params
}
}
return config
}, function (error) {
return Promise.reject(error)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
axios.interceptors.response.use(function (response) {
// token 已过期,重定向到登录页面
if (response.data.code == 4){
localStorage.clear()
router.replace({
path: '/signin',
query: {redirect: router.currentRoute.fullPath}
})
}
return response
}, function (error) {
// Do something with response error
return Promise.reject(error)