ow
This commit is contained in:
1 parent
9c40f3b173
commit
be496ab375
1 file changed
+13
-13
+13
-13
@@ -49,9 +49,7 @@ service.interceptors.request.use(
|
|||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => Promise.reject(error),
|
||||||
Promise.reject(error);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,10 +117,10 @@ service.interceptors.response.use(
|
|||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ① 只把「刷新令牌」放进 try。
|
||||||
|
// 刷新失败(RefreshToken 过期/被作废/网络异常)才需要清登录态。
|
||||||
try {
|
try {
|
||||||
await refreshTokenOnce();
|
await refreshTokenOnce();
|
||||||
//令牌成功刷新后,再次调用之前请求(请求拦截器会带上新令牌)
|
|
||||||
return await service.request(originalConfig);
|
|
||||||
} catch (refreshError) {
|
} catch (refreshError) {
|
||||||
// 刷新失败必须 reject:原来 return Promise.resolve() 会让业务拿到 undefined
|
// 刷新失败必须 reject:原来 return Promise.resolve() 会让业务拿到 undefined
|
||||||
// 当成功处理;这里清登录态跳登录页,并把错误抛给调用方。
|
// 当成功处理;这里清登录态跳登录页,并把错误抛给调用方。
|
||||||
@@ -131,9 +129,16 @@ service.interceptors.response.use(
|
|||||||
goLogin(!!(e && (e.status || e.response)));
|
goLogin(!!(e && (e.status || e.response)));
|
||||||
return Promise.reject(refreshError ?? error);
|
return Promise.reject(refreshError ?? error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ② 重试请求放在 try 之外。
|
||||||
|
// 它自身的失败(500/403/网络抖动等)只应让这一次业务请求失败,
|
||||||
|
// 不能当成「刷新失败」处理,否则会把已成功续期的用户莫名踢下线。
|
||||||
|
// 这里直接 return,让错误冒泡到外层 request() 的 catch 里,
|
||||||
|
// 复用已有的业务异常通知逻辑(ValidatedException / ServiceErrorException 等)。
|
||||||
|
return await service.request(originalConfig);
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,7 +149,7 @@ service.interceptors.response.use(
|
|||||||
*/
|
*/
|
||||||
export const request = async <T = any>(
|
export const request = async <T = any>(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
options: RequestOptions = {}
|
options: RequestOptions = {},
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
try {
|
try {
|
||||||
const { isGetDataDirectly = true } = options;
|
const { isGetDataDirectly = true } = options;
|
||||||
@@ -152,25 +157,21 @@ export const request = async <T = any>(
|
|||||||
const res = await service.request(config);
|
const res = await service.request(config);
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
res.data &&
|
res.data &&
|
||||||
(res.data.SourceCode == "CloudBuilder.Core.ValidatedException" ||
|
(res.data.SourceCode == "CloudBuilder.Core.ValidatedException" ||
|
||||||
res.data.SourceCode == "CloudBuilder.Core.ServiceErrorException")
|
res.data.SourceCode == "CloudBuilder.Core.ServiceErrorException")
|
||||||
) {
|
) {
|
||||||
|
|
||||||
let msg = res.data.Message;
|
let msg = res.data.Message;
|
||||||
if (res.data.SourceCode == "CloudBuilder.Core.ValidatedException") {
|
if (res.data.SourceCode == "CloudBuilder.Core.ValidatedException") {
|
||||||
warnNotificationWithIcon(msg);
|
warnNotificationWithIcon(msg);
|
||||||
}
|
}
|
||||||
if (res.data.SourceCode == "CloudBuilder.Core.ServiceErrorException") {
|
if (res.data.SourceCode == "CloudBuilder.Core.ServiceErrorException") {
|
||||||
|
|
||||||
errorNotificationWithIcon(msg);
|
errorNotificationWithIcon(msg);
|
||||||
}
|
}
|
||||||
return Promise.reject(res);
|
return Promise.reject(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// res 是 AxiosResponse,取 data 才是业务数据;这里断言成 T 消除类型报错(原代码就有)
|
// res 是 AxiosResponse,取 data 才是业务数据;这里断言成 T 消除类型报错(原代码就有)
|
||||||
return (isGetDataDirectly ? res.data : res) as T;
|
return (isGetDataDirectly ? res.data : res) as T;
|
||||||
} else {
|
} else {
|
||||||
@@ -285,7 +286,7 @@ export const download = async (d: {
|
|||||||
link.href = downloadUrl;
|
link.href = downloadUrl;
|
||||||
link.setAttribute(
|
link.setAttribute(
|
||||||
"download",
|
"download",
|
||||||
d.filename + dayjs().format("YYYYMMDDHHmmss") + ".xlsx"
|
d.filename + dayjs().format("YYYYMMDDHHmmss") + ".xlsx",
|
||||||
);
|
);
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
@@ -299,7 +300,6 @@ export const download = async (d: {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const errorNotificationWithIcon = (message: string) => {
|
const errorNotificationWithIcon = (message: string) => {
|
||||||
notification.error({
|
notification.error({
|
||||||
message: t("request.notification.errorMessage"),
|
message: t("request.notification.errorMessage"),
|
||||||
|
|||||||
Reference in new issue
Block a user