let whitelist = ['/login'] // 拦截器白名单,登录时不添加 token // 请求拦截 axios.interceptors.request.use( config => { // 拦截白名单以及添加token if (whitelist.includes(config.url)) { return config } else { let token = localStorage.token config.headers.token = token return config } }, err => { return err } ) // 响应拦截 axios.interceptors.response.use( response => { return response }, err => { // 添加超时处理 let isTimeout = err.toString().includes(`timeout of ${axios.defaults.timeout}ms exceeded`) if (isTimeout) { return Promise.reject(Message.error('请求超时,请重新尝试')) } // 添加404和500 switch (err.response.status) { case 404: return Promise.reject(Message.error('服务器中查找不到该资源')) case 500: return Promise.reject(Message.error('请求失败,服务端错误,请重新尝试')) } return Promise.reject(err) } )