大佬们问一下,我这更新token的时候把其他请求的接口存起来后
重新请求浏览器是返回是正常的,
但是响应那边会走error函数里面console.log("222会是响应吗", error);
这是为什么,会报错。按着错误提示如果请求参数缺少那浏览器上的请求根本不能成功啊
TypeError: Cannot read properties of undefined (reading 'protocol')
let isRefreshing = false; // 标志位,是否正在刷新 Token
let requestQueue: any = []; // 请求队列,用于缓存需要重新发送的请求
//request拦截器;
Axios.interceptors.request.use(
async config => {
const userStore = UserStore();
const newTokenUrl = "/user/getNewToken";
//加密
config.data = !config.data ? {} : config.data;
const rawData = config.data;
if (needApiLog) {
config["debugData"] = rawData;
}
if (import.meta.env.MODE !== "development") {
config.data = {
param: encrypt(JSON.stringify(config.data)),
};
}
config.headers.TraceId = FiltrationFun.randomizedString() + moment().valueOf();
config.headers.Authorization =
newTokenUrl !== config.url
? userStore.userLoginInfo.token.accessToken
: userStore.userLoginInfo.token.refreshToken;
if (userStore.isLogin && userStore.userLoginInfo.token.accessToken !== "") {
//判断是token是否快要过期
//const expiresIn = userStore.userLoginInfo.token.expiresIn;
const expiresIn = 1736318715;
const currentTime = moment().unix(); //当前时间
if (expiresIn - currentTime <= 300 && expiresIn - currentTime > 0) {
if (!isRefreshing) {
// console.log("没有来吗");
isRefreshing = true; // 标志位设置为 true,表示正在刷新 Token
try {
const response = await fetch(config.baseURL + newTokenUrl, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
traceid: FiltrationFun.randomizedString() + moment().valueOf(),
Authorization: userStore.userLoginInfo.token.refreshToken,
// 其他需要的 headers
},
body: JSON.stringify({
param: encrypt(
JSON.stringify({
device: device,
deviceId: FiltrationFun.getDeviceId(),
}),
),
}),
});
if (!response.ok) {
isRefreshing = false; // 重置标志位
router.push(RouterEnums.LOGIN);
}
const resdata = await response.json();
const getData =
import.meta.env.MODE !== "development"
? JSON.parse(decrypt(resdata.data))
: resdata.data;
userStore.setNewToken(getData);
} catch (err) {
isRefreshing = false;
router.push(RouterEnums.LOGIN);
} finally {
isRefreshing = false; // 重置标志位
// 重新发送所有拦截的请求
requestQueue.forEach(cb => cb());
requestQueue = []; // 清空请求队列
}
} else {
//返回一个新的 Promise,缓存当前请求到队列中
config.headers["Content-Type"] = "application/json";
config.data = JSON.stringify(config.data);
config["debugRequestData"] = rawData;
return new Promise(resolve => {
requestQueue.push(() => {
resolve(axios(config));
});
});
}
}
}
return config;
},
error => {
return Promise.reject(error);
},
);
Axios.interceptors.response.use(
response => {
console.log("进来吗", response);
if (needApiLog) {
console.log(response.config.url, "request ===>", response.config["debugRequestData"]);
//console.log("有data吗", response.data);
}
const res = response.data;
const { code } = res;
const userStore = UserStore();
if (code !== 200) {
if (code === 1001) {
// PubSub.publish(PubSubEnum.SHOW_ERROR_TIP, {
// text: t("responseTip.systemError"),
// });
//系统错误
showToast(t("responseTip.systemError"));
}else if (code === 3024) {
// PubSub.publish(PubSubEnum.SHOW_ERROR_TIP, {
// text: t("responseTip.levelActivityCollectionFail"),
// });
//活动领取失败
showToast(t("responseTip.levelActivityCollectionFail"));
}
return Promise.reject(res);
}
// const getData = import.meta.env.MODE !== "development" ? decrypt(res.data) : res.data;
// const rawData = JSON.parse(getData);
const getData =
import.meta.env.MODE !== "development" ? JSON.parse(decrypt(res.data)) : res.data;
const rawData = getData;
if (code === 200) {
return rawData;
}
return Promise.reject(res);
},
error => {
// PubSub.publish(PubSubEnum.SHOW_ERROR_TIP, {
// text: t("responseTip.unableToConnectToSystem"),
// });
showToast(t("responseTip.unableToConnectToSystem"));
console.log("222会是响应吗", error);
return Promise.reject(error);
},
);
我问了GPT 和搜索引擎都没有解决
Request 拦截器应该永远返回一个
AxiosRequestConfig
或Promise<AxiosRequestConfig>
对象,你这 resolve 回去的是个啥?