master
parent
133c6a2c16
commit
a406e0b3ba
@ -1,95 +1,108 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { MessageBox, Message,Loading } from 'element-ui'
|
import {
|
||||||
import store from '@/store'
|
MessageBox,
|
||||||
import { getToken } from '@/utils/auth'
|
Message,
|
||||||
let loading;
|
Loading
|
||||||
|
} from 'element-ui'
|
||||||
// create an axios instance
|
import store from '@/store'
|
||||||
const service = axios.create({
|
import {
|
||||||
baseURL: process.env.VUE_APP_DOMIAN, // url = base url + request url
|
getToken
|
||||||
// withCredentials: true, // send cookies when cross-domain requests
|
} from '@/utils/auth'
|
||||||
timeout: 5000 // request timeout
|
let loading;
|
||||||
})
|
|
||||||
|
// create an axios instance
|
||||||
// request interceptor
|
const service = axios.create({
|
||||||
service.interceptors.request.use(
|
baseURL: process.env.VUE_APP_DOMIAN, // url = base url + request url
|
||||||
config => {
|
// withCredentials: true, // send cookies when cross-domain requests
|
||||||
// do something before request is sent
|
timeout: 500000 // request timeout
|
||||||
loading = Loading.service({
|
})
|
||||||
lock:true,
|
|
||||||
background:"rgba(0,0,0,0.4)",
|
// request interceptor
|
||||||
text:"正在加载中..."
|
service.interceptors.request.use(
|
||||||
})
|
config => {
|
||||||
|
|
||||||
if (store.getters.token) {
|
if (!config.noloading) {
|
||||||
// let each request carry token
|
// do something before request is sent
|
||||||
// ['X-Token'] is a custom headers key
|
loading = Loading.service({
|
||||||
// please modify it according to the actual situation
|
lock: true,
|
||||||
//config.headers['X-Token'] = getToken();
|
background: "rgba(0,0,0,0.4)",
|
||||||
|
text: "正在加载中..."
|
||||||
config.headers['Authorization'] ="Bearer "+getToken()
|
})
|
||||||
}
|
}
|
||||||
return config
|
if (store.getters.token) {
|
||||||
},
|
// let each request carry token
|
||||||
error => {
|
// ['X-Token'] is a custom headers key
|
||||||
// do something with request error
|
// please modify it according to the actual situation
|
||||||
console.log(error) // for debug
|
//config.headers['X-Token'] = getToken();
|
||||||
return Promise.reject(error)
|
|
||||||
}
|
config.headers['Authorization'] = "Bearer " + getToken()
|
||||||
)
|
}
|
||||||
|
return config
|
||||||
// response interceptor
|
},
|
||||||
service.interceptors.response.use(
|
error => {
|
||||||
/**
|
// do something with request error
|
||||||
* If you want to get http information such as headers or status
|
console.log(error) // for debug
|
||||||
* Please return response => response
|
return Promise.reject(error)
|
||||||
*/
|
}
|
||||||
|
)
|
||||||
/**
|
|
||||||
* Determine the request status by custom code
|
// response interceptor
|
||||||
* Here is just an example
|
service.interceptors.response.use(
|
||||||
* You can also judge the status by HTTP Status Code
|
/**
|
||||||
*/
|
* If you want to get http information such as headers or status
|
||||||
response => {
|
* Please return response => response
|
||||||
loading.close()
|
*/
|
||||||
const res = response.data
|
|
||||||
|
/**
|
||||||
// if the custom code is not 20000, it is judged as an error.
|
* Determine the request status by custom code
|
||||||
if (res.errcode) {
|
* Here is just an example
|
||||||
Message({
|
* You can also judge the status by HTTP Status Code
|
||||||
message: res.errmsg || 'Error',
|
*/
|
||||||
type: 'error',
|
response => {
|
||||||
duration: 5 * 1000
|
if (loading) {
|
||||||
})
|
loading.close()
|
||||||
|
}
|
||||||
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
const res = response.data
|
||||||
if (res.errcode === 50008 || res.errcode === 50012 || res.errcode === 50014) {
|
|
||||||
// to re-login
|
// if the custom code is not 20000, it is judged as an error.
|
||||||
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
|
if (res.errcode) {
|
||||||
confirmButtonText: 'Re-Login',
|
Message({
|
||||||
cancelButtonText: 'Cancel',
|
message: res.errmsg || 'Error',
|
||||||
type: 'warning'
|
type: 'error',
|
||||||
}).then(() => {
|
duration: 5 * 1000
|
||||||
store.dispatch('user/resetToken').then(() => {
|
})
|
||||||
location.reload()
|
|
||||||
})
|
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
||||||
})
|
if (res.errcode === 50008 || res.errcode === 50012 || res.errcode === 50014) {
|
||||||
}
|
// to re-login
|
||||||
return Promise.reject(new Error(res.errmsg || 'Error'))
|
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again',
|
||||||
} else {
|
'Confirm logout', {
|
||||||
return res
|
confirmButtonText: 'Re-Login',
|
||||||
}
|
cancelButtonText: 'Cancel',
|
||||||
},
|
type: 'warning'
|
||||||
error => {
|
}).then(() => {
|
||||||
loading.close()
|
store.dispatch('user/resetToken').then(() => {
|
||||||
console.log('err' + error) // for debug
|
location.reload()
|
||||||
Message({
|
})
|
||||||
message: error.message,
|
})
|
||||||
type: 'error',
|
}
|
||||||
duration: 5 * 1000
|
return Promise.reject(new Error(res.errmsg || 'Error'))
|
||||||
})
|
} else {
|
||||||
return Promise.reject(error)
|
return res
|
||||||
}
|
}
|
||||||
)
|
},
|
||||||
|
error => {
|
||||||
|
if (loading) {
|
||||||
|
loading.close()
|
||||||
|
}
|
||||||
|
console.log('err' + error) // for debug
|
||||||
|
Message({
|
||||||
|
message: error.message,
|
||||||
|
type: 'error',
|
||||||
|
duration: 5 * 1000
|
||||||
|
})
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
export default service
|
export default service
|
||||||
|
|||||||
Loading…
Reference in new issue