loulijun2021
2022-06-30 7d0a954d23d21c0cdaff6ed20ad7291b3dbf889a
src/utils/request.js
@@ -1,25 +1,30 @@
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import { getCookie, getToken } from '@/utils/auth'
// 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
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 // request timeout
})
// axios.defaults.withCredentials = true
// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['X-Token'] = getToken()
    if (getCookie('admin')) {
    // let each request carry token
    // ['X-Token'] is a custom headers key
    // please modify it according to the actual situation
    // config.headers['X-Token'] = getToken()
    //   config.headers['Authorization'] = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   config.headers['Cookie'] = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   document.cookie = 'admin=' + getCookie('admin') + ';navTabId=' + getCookie('navTabId')
    //   document.cookie = 'admin'
    }
    return config
  },
@@ -35,7 +40,7 @@
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */
   */
  /**
   * Determine the request status by custom code
@@ -50,7 +55,7 @@
      Message({
        message: res.Message || 'Error',
        type: 'error',
        duration: 5 * 1000
        duration: 10 * 1000
      })
      // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
@@ -76,7 +81,7 @@
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
      duration: 10 * 1000
    })
    return Promise.reject(error)
  }