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
相关推荐
我是坏垠23 分钟前
Crypto、Cipher与Password:Java加密开发的三个核心概念
java·开发语言·python
white_ant43 分钟前
JDK LTS 版本升级迁移注意事项大全
java·开发语言
Bobolink_1 小时前
跨境业务网络环境评估,“干净、一致、独享”三个关键指标
开发语言·网络·php·跨境网络·网络环境
你怎么知道我是队长1 小时前
JavaScript的变量和数据类型介绍
开发语言·javascript·ecmascript
xingke1 小时前
C 语言多文件编程:(一)头文件、extern、编译链接
c语言·开发语言·多文件
戮漠summer2 小时前
Missing Semester 计算机教育中缺失的一课 Lecture 01 Shell
开发语言·后端·scala
jinyishu_2 小时前
C++ string使用方法
开发语言·c++
EW Frontier2 小时前
三级跳突破864维动作空间——QMIX-Hierarchical多无人机协同通信方法全解析【附python代码】
开发语言·python·无人机·强化学习·通信资源分配
名字还没想好☜2 小时前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司2 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能