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
相关推荐
驭渊的小故事10 分钟前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
Sakura_j_h_1 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
微笑星河q1 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
code2cat2 小时前
【随笔】MCP长任务:查询已经返回,为什么任务还没有完成
javascript·人工智能·后端·mcp
Eider19982 小时前
Python基础语法之并发相关3
开发语言·python
FRing032 小时前
vue2干货总结11(短信验证、登录功能、统一处理错误(响应拦截器)、将token信息存入vuex、vuex持久化处理)
开发语言·前端·javascript·vue.js·vue2
泯泷2 小时前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
afumu3 小时前
免费 API 满地都是,为什么还有人做「让网页 AI 干活」这种东西
javascript·chrome·浏览器
云起SAAS3 小时前
本地生活同城多商户商城系统源码源码 合伙人分红加区域代理加排队返现
前端·javascript·后端
CS_Zero3 小时前
DeepSeek V4.1 Flash 1017 (Batch) 新手极速上手指南
开发语言·batch·deepseek