import axios from 'axios'
const api = axios.create({
baseURL: 'https://api.example.com',
timeout: 5000
})
// 缓存请求信息
let cache = {}
// 请求拦截器
api.interceptors.request.use(config => {
// 获取请求的url和请求⽅法
// api1
const { url, method } = config
// 判断是否存在该请求
if (cache[url] && cache[url].method === method) {
const now = Date.now()
const diff = now - cache[url].time
// 如果请求时间间隔⼩于1000毫秒,则认为是重复请求
if (diff < 1000) {
return Promise.reject('重复请求')
}
}
// 缓存请求信息
cache[url] = { method, time: Date.now() }
return config
}, error => {
return Promise.reject(error)
})
// 响应拦截器
api.interceptors.response.use(response => {
return response
}, error => {
return Promise.reject(error)
})