// Request interceptor import axios from 'axios' import {Message} from 'element-ui' import router from '.. /router' axios.create({ timeout: / 10000 / request timeout}) / / request interceptor axios. Interceptors. Request. Use (config = > {/ / for local token let token = Localstorage.getitem ('tokendata') // Set let headers = 'application/json; Charset =UTF-8' // Whether tokenFlag = true // Whether to modify the request information if (config.opts) {// Obtain the tokenFlag = config.opts? Config.opts. Token: true // Headers = config.opts.Header? config.opts.Header : 'application/json; Head if (Head) {for (let key in Head) {config.headers[key] = Head[key] }} // add time if (config.url.indexof ('? ') < 1) { config.url += '? Time =' + new Date().getTime()} else {config.url += '&time=' + new Date().getTime()} If (token && tokenFlag) {// config.headers['token'] = token config.headers[' x-auth0-token '] = token} else {// config.headers[' x-auth0-token '] = token} else { // headers = 'application/x-www-form-urlencode'} Err = > {return Promise. Reject (err)}) / / HTTP response server response interceptor axios. Interceptors. Response. Use (response = > {the if (response.data.code === -401) { router.push({ name: 'login' }) } return response }, error => { if (error.response) { switch (error.response.status) { case 400: Error. Message = 'Error request: ${error.response.data}' break case 401: error. Message = 'Unauthorized, please log back' Router.replace ({path: '/ login, query: {redirect: router. CurrentRoute. FullPath} / /}) after the success of the login into browse the current page break case 403: Error. Message = 'access denied' break case 404: error. Message = 'Access denied' break case 405: Error. Message = 'request timed out' break case 408: error. Message = 'request timed out' break case 500: Error. Message = 'network failed' break case 501: error. Message = 'network failed' break case 503: Error. Message = 'network timeout' break case 505: error. Message = 'network timeout' break case 505: Error. Message = 'HTTP version does not support this request' break default: Error. Message = 'connection error ${error.response.status}'}} else {error. message = 'server exception'} message. error(error.message) return Promise.reject(error.response.data) } ) export default axiosCopy the code