token失效重新存储发起请求

复制代码
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import Router from '@/router'
import { getCookie, setToken, setCookie } from './auth'

// 因为后端环境区分v1 v2 剔除测试盛传的环境配置,并添加统一前缀
const url = process.env.VUE_APP_BASE_API
// axios.defaults.baseURL = 'http://192.168.24.48:8080/vwdata'

// create an axios instance
const service = axios.create({
  baseURL: url, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 60000, // request timeout
  crossDomain: true
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.state.user.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      // config.headers['token'] = getToken()
      config.headers['token'] = getCookie('user_token')
    }
    // The request parameter defaults to object
    config.data = config.data || {}
    // Determine whether global loading is needed according to the request parameter s s s needload field
    return config
  },
  error => {
    // error  reset loading
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

window.islogin = true
// 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.
    // 后台返回0失败1成功
    // if (!getToken()) {
    //   Router.push('/login')
    // }
    if (res.code === 0 || res.code === 1) {
      return res
    } else if (res.code === -1) {

      if (window.islogin) {
        window.islogin = false
        MessageBox.alert(res.message, '登录失效', {
          confirmButtonText: '去登录',
          showClose: false
        }).then(() => {
          Router.push('/login')
        })
      }
    } else if (res.code === 401) {
      Router.push('/err.401')
    } else if (res.code === 1001) {
      setCookie('user_token', res.data.token)
      response.config.headers['token'] = res.data.token

      return axios.request(response.config)
    }
    else {
      if(res.message){
          Message({
        message: res.message,
        type: 'error',
        duration: 5000
      })
      }else{
        return response

      }
    
    }


  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.msg || '网络异常',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
相关推荐
GISer_Jing3 小时前
Three.js着色器编译机制深度解析
javascript·webgl·着色器
丷丩3 小时前
MapLibre GL JS第22课:查看本地GeoJSON
前端·javascript·map·mapbox·maplibre gl js
AI玫瑰助手3 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车3 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋3 小时前
C++14特性
开发语言·c++·c++14特性
JAVA社区5 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
弥树子5 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
z落落5 小时前
C# ToCharArray + foreach遍历 + String与StringBuilder
开发语言·c#
学代码的真由酱6 小时前
Java多用户一对一网页聊天室-测试报告
java·开发语言·功能测试·测试
人道领域6 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法