){
return
axios
(config)
export
default
https;
提取重复请求
export const failedReconnection=(error,axios)=> {
let config = error.config;
if (!config || !config.retry) return Promise.reject(error);
config.__retryCount = config.__retryCount || 0;
if (config.__retryCount >= config.retry) {
return Promise.reject(error);
config.__retryCount += 1;
console.log(config, "config ffff", config.url);
console.log(config.url + " 自动重试第" + config.__retryCount + "次");
var backoff = new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, config.retryDelay || 1000);
return backoff.then(function () {
return axios(config);
在utils/require.js中进行修改
import axios from "axios";
import { showToast } from "vant";
import { failedReconnection } from "./failedReconnection";
const https = axios.create({
baseURL: "http://47.93.101.203/api/",
timeout: 1000,
https.defaults.retry = 2;
https.defaults.retryDelay = 1000;
https.interceptors.request.use(
function (config) {
let token =
(JSON.parse(localStorage.getItem("userInfo")) &&
JSON.parse(localStorage.getItem("userInfo")).token) ||
if (token != "") {
config.headers["Authori-zation"] = "Bearer " + token;
return config;
function (error) {
return Promise.reject(error);
https.interceptors.response.use(
function (response) {
if (response.data.status == 410000) {
showToast("登录过期请登录");
setTimeout(() => {
location.href = "/login";
}, 500);
return response.data;
function (error) {
return failedReconnection(error, https);
阻止重复请求,取消请求
import axios from "axios";
import { showToast } from "vant";
import { failedReconnection } from "./failedReconnection";
const https = axios.create({
baseURL: "http://47.93.101.203/api/",
timeout: 1000,
let reqList = [];
https.defaults.retry = 2;
https.defaults.retryDelay = 1000;
https.interceptors.request.use(
function (config) {
let token =
(JSON.parse(localStorage.getItem("userInfo")) &&
JSON.parse(localStorage.getItem("userInfo")).token) ||
if (token != "") {
config.headers["Authori-zation"] = "Bearer " + token;
config.cancelToken = new axios.CancelToken(function (c) {
cancel = c;
stopRepeatRequest(reqList, config.url, cancel, `${config.url} 请求被中断`)
return config;
function (error) {
return Promise.reject(error);
https.interceptors.response.use(
function (response) {
if (response.data.status == 410000) {
showToast("登录过期请登录");
setTimeout(() => {
location.href = "/login";
}, 500);
return response.data;
function (error) {
setTimeout(() => {
allowRequest(reqList, error.config.url);
}, 2000);
return failedReconnection(error, https);
* 阻止重复请求
* @param {array} reqList - 请求缓存列表
* @param {string} url - 当前请求地址
* @param {function} cancel - 请求中断函数
* @param {string} errorMessage - 请求中断时需要显示的错误信息
const stopRepeatRequest = function (reqList, url, cancel, errorMessage) {
const errorMsg = errorMessage || "";
for (let i = 0; i < reqList.length; i++) {
if (reqList[i] === url) {
cancel(errorMsg);
return;
reqList.push(url);
* 允许某个请求可以继续进行
* @param {array} reqList 全部请求列表
* @param {string} url 请求地址
const allowRequest = function (reqList, url) {
for (let i = 0; i < reqList.length; i++) {
if (reqList[i] === url) {
reqList.splice(i, 1);
break;
export default https;
提取 取消重复请求方法
export let reqList = [];
* 阻止重复请求
* @param {array} reqList - 请求缓存列表
* @param {string} url - 当前请求地址
* @param {function} cancel - 请求中断函数
* @param {string} errorMessage - 请求中断时需要显示的错误信息
export const stopRepeatRequest = function (reqList, url, cancel, errorMessage) {
const errorMsg = errorMessage || "";
for (let i = 0; i < reqList.length; i++) {
if (reqList[i] === url) {
cancel(errorMsg);
return;
reqList.push(url);
* 允许某个请求可以继续进行
* @param {array} reqList 全部请求列表
* @param {string} url 请求地址
export const allowRequest = function (reqList, url) {
for (let i = 0; i < reqList.length; i++) {
if (reqList[i] === url) {
reqList.splice(i, 1);
break;
修改utils/require.js
import axios from "axios";
import { showToast } from "vant";
import { failedReconnection } from "./failedReconnection";
import { reqList, stopRepeatRequest, allowRequest,} from "./stopRepeatRequest.js";
const https = axios.create({
baseURL: "http://47.93.101.203/api/",
timeout: 1000,
https.defaults.retry = 2;
https.defaults.retryDelay = 1000;
https.interceptors.request.use(
function (config) {
let token =
(JSON.parse(localStorage.getItem("userInfo")) &&
JSON.parse(localStorage.getItem("userInfo")).token) ||
if (token != "") {
config.headers["Authori-zation"] = "Bearer " + token;
config.cancelToken = new axios.CancelToken(function (c) {
cancel = c;
stopRepeatRequest(reqList, config.url, cancel, `${config.url} 请求被中断`)
return config;
function (error) {
return Promise.reject(error);
https.interceptors.response.use(
function (response) {
if (response.data.status == 410000) {
showToast("登录过期请登录");
setTimeout(() => {
location.href = "/login";
}, 500);
return response.data;
function (error) {
setTimeout(() => {
allowRequest(reqList, error.config.url);
}, 2000);
return failedReconnection(error, https);
提取拦截器
import { failedReconnection } from "./failedReconnection";
import {
reqList,
stopRepeatRequest,
allowRequest,
} from "./stopRepeatRequest.js";
import request from "./request.js";
let { instance, axios } = request;
let time = 1000;
instance.interceptors.request.use(
function (config) {
console.log(config.retryDelay, "config.retryDelay");
if (config.retryDelay < time) {
config.retryDelay = time + 500;
let cancel;
config.cancelToken = new axios.CancelToken(function (c) {
cancel = c;
stopRepeatRequest(reqList, config.url, cancel, `${config.url} 请求被中断`);
return config;
function (error) {
return Promise.reject(error);
instance.interceptors.response.use(
function (response) {
setTimeout(() => {
allowRequest(reqList, response.config.url);
}, time);
let { data } = response;
return data;
function (error) {
if (axios.isCancel(error)) {
return Promise.reject(error.message);
setTimeout(() => {
allowRequest(reqList, error.config.url);
}, time);
return failedReconnection(error, instance);
export default instance;
修改utils/require.js
import axios from "axios";
let baseURL = import.meta.env.VITE_BSEL_URL;
const instance = axios.create({
baseURL,
instance.defaults.retry = 2;
instance.defaults.retryDelay = 1000;
export default { instance, axios };
复制代码
- 214
-
ChatGPT
Visual Studio Code