// request interceptorservice.interceptors.request.use( config => {// do something before request is sentif (store.getters.token) {// let each request carry token// ['X-Token'] is a custom headers key// please modify it according to the actual situationconfig.headers['X-Token'] =getToken() //设置header中的token }return config }, error => {// do something with request errorconsole.log(error) // for debugreturnPromise.reject(error) })
service.interceptors.response.use( response => {constres=response.data// if the custom code is not 20000, it is judged as an error.if (res.code !==20000) {Message({ message:res.message ||'Error', type:'error', duration:5*1000 })// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;if (res.code ===50008||res.code ===50012||res.code ===50014) {// to re-login MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText:'Re-Login', cancelButtonText:'Cancel', type:'warning' }).then(() => {store.dispatch('user/resetToken').then(() => {location.reload() }) }) }returnPromise.reject(newError(res.message ||'Error')) } else { return res } }, error => {console.log('err'+ error) // for debugMessage({ message:error.message, type:'error', duration:5*1000 })returnPromise.reject(error) })exportdefault service
createError.js?2d83:16Uncaught (in promise) Error: Request failed with status code 404 at createError (createError.js?2d83:16) at settle (settle.js?467f:18) at XMLHttpRequest.handleLoad (xhr.js?b50d:59)