diff --git a/src/plugins/http-fetch.ts b/src/plugins/http-fetch.ts index bc573770..a28eae01 100644 --- a/src/plugins/http-fetch.ts +++ b/src/plugins/http-fetch.ts @@ -181,7 +181,7 @@ function interceptorResponse(res: ResultType): ResultType | Promise { data: res, }); } - if (Reflect.has(res, 'error')){ + if (Reflect.has(res, 'error')) { return Promise.resolve({ code: 0, msg: 'error', @@ -225,11 +225,11 @@ export async function request(options: OptionsType): Promise { try { const res = await fetch(options.url, options); // console.log('请求结果:', res); - if (res.status === 500) { - return { - code: 0, - msg: '服务器连接出错!', - }; + + // 状态码拦截处理 + const reqNot = stateCode(res); + if (reqNot != false) { + return reqNot; } // 根据响应数据类型返回 @@ -285,3 +285,28 @@ export async function request(options: OptionsType): Promise { 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; +}