Repository navigation
Expand file tree
/
Copy pathaxios.js
More file actions
50 lines (45 loc) · 1.29 KB
/
Copy pathaxios.js
File metadata and controls
50 lines (45 loc) · 1.29 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
49
50
import axios from 'axios'
import VueAxios from 'vue-axios'
import Vue from 'vue'
export default (ctx) => {
const customAxios = axios.create({
baseURL: process.env.AxiosBaseURL
})
Vue.use(VueAxios, customAxios)
customAxios.interceptors.request.use((config) => {
// clear cache
// if (config.method === 'get') {
// let char = '?'
// if (config.url.split('?').length > 1) {
// char = '&'
// }
// config.url += `${char}${(new Date()).getTime()}`
// }
return config
})
customAxios.interceptors.response.use((response) => {
if (response.config.method === 'get' || response.config.method === 'delete') {
// get and delete use snack tip
if (response.data.code === 0) {
return response.data.data
} else if(response.data.code == -2) {
window.location.href = `${ctx.env.Server}/start`
} else {
ctx.store.commit('setSnackBar', {
snackBar: true,
snackMsg: response.data.msg
})
}
} else {
// other, deal with yourself
return response.data
}
}, (error) => {
ctx.store.commit('setSnackBar', {
snackBar: true,
snackMsg: ctx.app.i18n.t('requestError', ctx.app.store.state.locale)
})
return Promise.reject(error)
})
return customAxios
}