feat: 统一接口请求错误response拦截逻辑
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
* @Author: 田鑫
|
||||
* @Date: 2023-02-17 11:58:44
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @LastEditTime: 2025-06-23 05:51:32
|
||||
* @LastEditTime: 2025-07-05 17:59:59
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
@ -16,7 +16,7 @@ const contentType = 'application/json';
|
||||
const requestTimeout = 30000;
|
||||
|
||||
enum HttpStatusCode {
|
||||
OK = 200,
|
||||
Success = 200,
|
||||
BadRequest = 400, // 请求参数错误
|
||||
Unauthorized = 401, // token 无效或过期
|
||||
NotFound = 404,
|
||||
@ -62,22 +62,30 @@ export class Request {
|
||||
(res: AxiosResponse) => {
|
||||
const { data } = res;
|
||||
switch (data.code) {
|
||||
case HttpStatusCode.OK:
|
||||
case HttpStatusCode.Success:
|
||||
return data;
|
||||
default:
|
||||
return Promise.reject(data);
|
||||
}
|
||||
},
|
||||
(err: any) => {
|
||||
const { message, code } = err.response?.data ?? {};
|
||||
AMessage.error(message ?? err.message);
|
||||
const { response } = err;
|
||||
const status = response?.status;
|
||||
let errMessage = response?.data?.message ?? err.message;
|
||||
|
||||
switch (code) {
|
||||
switch (status) {
|
||||
case HttpStatusCode.InternalServerError:
|
||||
errMessage = '系统繁忙,请稍后再试或联系管理员。';
|
||||
break;
|
||||
case HttpStatusCode.NotFound:
|
||||
errMessage = '接口不存在';
|
||||
break;
|
||||
case HttpStatusCode.Unauthorized:
|
||||
goUserLogin();
|
||||
handleUserLogout();
|
||||
break;
|
||||
}
|
||||
|
||||
AMessage.error(errMessage);
|
||||
return Promise.reject(err.response);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user