import store from '@/store' import axios from 'axios' import router from '@/router' import { getTimeStamp } from '@/utils/auth' import { Message } from 'element-ui' // import { MessageBox, Message } from 'element-ui' // import store from '@/store' // import { getToken } from '@/utils/auth' const timeout = 1 * 60 * 60 * 60 // create an axios instance const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests timeout: 5000 // request timeout }) // request interceptor // axios的请求拦截器, 两个参数 service.interceptors.request.use( async config => { // 请求拦截器需要token的时候添加token if (store.getters.token && !config.headers.Authorization) { // 有token并且没有请求头 // 先判断时间戳是否过期 // 如果时间戳已经过期, 1. 退出登录 2. 跳转登录页 3. 拦截剩下的请求 // 如果没过期, 给请求头添加token if (checkTimeout()) { await store.dispatch('user/logout') router.push('/login') // debugger return Promise.reject(new Error('登录已过期')) } else { config.headers.Authorization = `Bearer ${store.getters.token}` // 在请求头添加token } } return config }, err => { Message.error('请求失败') return Promise.reject(err) } ) // 校验时间戳函数 const checkTimeout = () => { const loginTimeStamp = getTimeStamp() const nowTimeStamp = Date.now() return nowTimeStamp - loginTimeStamp > timeout } // response interceptor // service.interceptors.response.use( // /** // * If you want to get http information such as headers or status // * Please return response => response // */ // /** // * Determine the request status by custom code // * Here is just an example // * You can also judge the status by HTTP Status Code // */ // response => { // const res = 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() // }) // }) // } // return Promise.reject(new Error(res.message || 'Error')) // } else { // return res // } // }, // error => { // console.log('err' + error) // for debug // Message({ // message: error.message, // type: 'error', // duration: 5 * 1000 // }) // return Promise.reject(error) // } // ) // 响应拦截器 service.interceptors.response.use(res => { const { message, data, success } = res.data if (success) { // 已经对res的成功与否进行处理, 直接返回data return data } else { Message.error(message || '系统错误') // 饿了么弹窗组件 return Promise.reject(message) // 这里的reject是为了可以继续链式调用, 比如跳到login/index handleLogin catch } }, err => { console.dir(err) // 提示错误 Message.error(err.message) // reject return Promise.reject(err) }) export default service