fix: 状态码拦截处理
This commit is contained in:
@@ -181,7 +181,7 @@ function interceptorResponse(res: ResultType): ResultType | Promise<any> {
|
|||||||
data: res,
|
data: res,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (Reflect.has(res, 'error')){
|
if (Reflect.has(res, 'error')) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
code: 0,
|
code: 0,
|
||||||
msg: 'error',
|
msg: 'error',
|
||||||
@@ -225,11 +225,11 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(options.url, options);
|
const res = await fetch(options.url, options);
|
||||||
// console.log('请求结果:', res);
|
// console.log('请求结果:', res);
|
||||||
if (res.status === 500) {
|
|
||||||
return {
|
// 状态码拦截处理
|
||||||
code: 0,
|
const reqNot = stateCode(res);
|
||||||
msg: '服务器连接出错!',
|
if (reqNot != false) {
|
||||||
};
|
return reqNot;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据响应数据类型返回
|
// 根据响应数据类型返回
|
||||||
@@ -285,3 +285,28 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
clearTimeout(timeoutId); // 请求成功,清除超时计时器
|
clearTimeout(timeoutId); // 请求成功,清除超时计时器
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断状态码处理结果信息(不可处理)
|
||||||
|
* @param res 请求结果
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
function stateCode(res: Response) {
|
||||||
|
if (res.status === 500) {
|
||||||
|
return {
|
||||||
|
code: 0,
|
||||||
|
msg: '服务器连接出错!',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (res.status === 404) {
|
||||||
|
return {
|
||||||
|
code: 0,
|
||||||
|
msg: '请求地址错误',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (res.status === 401) {
|
||||||
|
removeToken();
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user