美文网首页
axios 拦截器来截取取消重复请求

axios 拦截器来截取取消重复请求

作者: 啊笨猫 | 来源:发表于2021-03-29 11:04 被阅读0次

在实际项目中,我们可能需要对请求进行“防抖”处理。这里主要是为了阻止用户在某些情况下短时间内重复点击某个按钮,导致前端向后端重复发送多次请求,这里介绍一种使用Axios拦截器来取消重复的请求方法

import axios from 'axios'
import qs from 'qs'
import { notification, message } from 'ant-design-vue'
import router from '@/router/router'
import moment from 'moment'
import baseURL from './config'
let pending = {} // 网络请求记录
let CancelToken = axios.CancelToken

axios.defaults.timeout = 30000
axios.defaults.baseURL = baseURL.target
    
//设置请求拦截器
axios.interceptors.request.use(
  config => {
    const token = localStorage.getItem('ACCESS_TOKEN')
    const companyId = localStorage.getItem('companyId')
    const userId = localStorage.getItem('userId')
    if (token) {
      config.headers['AUTH-TOKEN'] = token
    }
    if (companyId) {
      config.headers['companyId'] = companyId
    }
    if (userId) {
      config.headers['userId'] = userId
    }
    console.log(config.url, 'config.url')
    let key = `${config.url}&${config.method}&${JSON.stringify(config.data)}`
    // 设置重复请求
    config.cancelToken = new CancelToken(c => {
      if (pending[key]) {
        if (Date.now() - pending[key] > 1000) {
          // 超过1s,删除对应的请求记录,重新发起请求
          delete pending[key]
        } else {
          // 1s以内的已发起请求,取消重复请求
          c('repeated')
        }
      }
      // 记录当前的请求,已存在则更新时间戳
      pending[key] = Date.now()
    })

    return config
  },
  error => {
    return Promise.error(error)
  })
  // response 拦截器
axios.interceptors.response.use(
  response => {
    let key = `${response.config.url.replace(baseURL.target, '')}&${response.config.method}&${response.config.data}`
    if (pending[key]) {
      // 请求结束,删除对应的请求记录
      delete pending[key]
    }
    if (response.data.code === 101) {
      localStorage.clear()
      router.replace('/login')
    }
    if (response.data.code === 102) {
      notification.error({
        message: response.data.msg
      })
    }
    if (response.data.code === 99) {
      message.error(response.data.msg)
      return response
    }
    return response
  },
  error => {
    if (error.response) {
      const data = error.response.data
      const token = localStorage.getItem('ACCESS_TOKEN')
      if (error.response.status === 403) {
        notification.error({
          message: '登录超时',
          description: data.msg
        })
        router.replace('/exception/403')
        return
      }
      if (error.response.status === 404) {
        notification.error({
          message: '客户端错误',
          description: data.msg
        })
        router.replace('/exception/404')
        return
      }
      if (error.response.status === 500) {
        notification.error({
          message: '服务器请求超时'
        })
        return
      }
    }
    return Promise.reject(error)
  }
)

相关文章

  • axios 拦截器来截取取消重复请求

    在实际项目中,我们可能需要对请求进行“防抖”处理。这里主要是为了阻止用户在某些情况下短时间内重复点击某个按钮,导致...

  • Axios 取消重复请求

    有什么用? 当用户频繁点击在短时间内发送多个 ajax 请求,但是由于网络原因服务器数据无法及时响应返回,这时候,...

  • axios取消重复请求

    在开发中,经常会遇到接口重复请求导致的各种问题。 对于重复的get请求,会导致页面更新多次,发生页面抖动的现象,影...

  • axios取消重复请求

    当第一个axios发起连续请求的时候,这时候我们就需要取消第二、第三个请求,因为他们都是同一个请求发起。 来看一下...

  • axios使用拦截器取消重复的请求

    由于接口响应速度等原因,在项目开发中经常需要处理重复点击导致多次调用接口的问题,针对每个接口调用单独处理相对繁琐,...

  • axios的封装

    Axios的拦截器介绍 axios的拦截器原理如下:image.pngaxios拦截器axios作为网络请求的第三...

  • axios安装与基本配置

    安装:$npm install axios --save get请求: post请求: 请求拦截器和响应拦截器

  • Axios如何取消重复请求

    在 Web 项目开发过程中,我们经常会遇到重复请求的场景,如果系统不对重复的请求进行处理,则可能会导致系统出现各种...

  • Axios如何取消重复请求

    在实际开发中,我们需要对用户发起的重复请求进行拦截处理,比如用户快速点击提交按钮。 对于重复的 get 请求,会导...

  • axios重复请求处理 CancelToken 取消请求

    CancelToken 介绍 ​ axios中用于取消请求的方法 使用 可以使用 CancelToken.so...

网友评论

      本文标题:axios 拦截器来截取取消重复请求

      本文链接:https://www.haomeiwen.com/subject/pkzvhltx.html