fix 请求信息封装常量
This commit is contained in:
23
src/constants/result-constants.ts
Normal file
23
src/constants/result-constants.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/**响应-code正常成功 */
|
||||||
|
export const RESULT_CODE_SUCCESS = 1;
|
||||||
|
|
||||||
|
/**响应-msg正常成功 */
|
||||||
|
export const RESULT_MSG_SUCCESS = 'success';
|
||||||
|
|
||||||
|
/**响应-code错误失败 */
|
||||||
|
export const RESULT_CODE_ERROR = 0;
|
||||||
|
|
||||||
|
/**响应-msg错误失败 */
|
||||||
|
export const RESULT_MSG_ERROR = 'error';
|
||||||
|
|
||||||
|
/**响应-网络连接超时 */
|
||||||
|
export const RESULT_MSG_TIMEOUT = 'Network connection timeout!';
|
||||||
|
|
||||||
|
/**响应-未知响应数据类型 */
|
||||||
|
export const RESULT_MSG_NOT_TYPE = 'Unknown response data type!';
|
||||||
|
|
||||||
|
/**响应-服务器连接出错 */
|
||||||
|
export const RESULT_MSG_SERVER_ERROR = 'Server connection error!';
|
||||||
|
|
||||||
|
/**响应-请求地址未找到 */
|
||||||
|
export const RESULT_MSG_URL_NOTFOUND = 'Request address not found!';
|
||||||
@@ -6,11 +6,21 @@ import {
|
|||||||
APP_REQUEST_HEADER_CODE,
|
APP_REQUEST_HEADER_CODE,
|
||||||
APP_REQUEST_HEADER_VERSION,
|
APP_REQUEST_HEADER_VERSION,
|
||||||
} from '@/constants/app-constants';
|
} from '@/constants/app-constants';
|
||||||
|
import {
|
||||||
|
RESULT_CODE_ERROR,
|
||||||
|
RESULT_CODE_SUCCESS,
|
||||||
|
RESULT_MSG_ERROR,
|
||||||
|
RESULT_MSG_NOT_TYPE,
|
||||||
|
RESULT_MSG_SERVER_ERROR,
|
||||||
|
RESULT_MSG_SUCCESS,
|
||||||
|
RESULT_MSG_TIMEOUT,
|
||||||
|
RESULT_MSG_URL_NOTFOUND,
|
||||||
|
} from '@/constants/result-constants';
|
||||||
|
|
||||||
/**响应结果类型 */
|
/**响应结果类型 */
|
||||||
export type ResultType = {
|
export type ResultType = {
|
||||||
/**响应码 */
|
/**响应码 */
|
||||||
code: number | 200 | 500;
|
code: number | 1 | 0;
|
||||||
/**信息 */
|
/**信息 */
|
||||||
msg: string;
|
msg: string;
|
||||||
/**数据 */
|
/**数据 */
|
||||||
@@ -78,8 +88,6 @@ const FATCH_OPTIONS: OptionsType = {
|
|||||||
headers: {
|
headers: {
|
||||||
[APP_REQUEST_HEADER_CODE]: import.meta.env.VITE_APP_CODE,
|
[APP_REQUEST_HEADER_CODE]: import.meta.env.VITE_APP_CODE,
|
||||||
[APP_REQUEST_HEADER_VERSION]: import.meta.env.VITE_APP_VERSION,
|
[APP_REQUEST_HEADER_VERSION]: import.meta.env.VITE_APP_VERSION,
|
||||||
// 使用mock.apifox.cn时开启
|
|
||||||
// apifoxToken: '8zCzh3vipdEwd1ukv9lQEuTekdWIH7xN',
|
|
||||||
},
|
},
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
@@ -176,15 +184,15 @@ function interceptorResponse(res: ResultType): ResultType | Promise<any> {
|
|||||||
// 风格处理
|
// 风格处理
|
||||||
if (!Reflect.has(res, 'code')) {
|
if (!Reflect.has(res, 'code')) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
code: 1,
|
code: RESULT_CODE_SUCCESS,
|
||||||
msg: 'success',
|
msg: RESULT_MSG_SUCCESS,
|
||||||
data: res,
|
data: res,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (Reflect.has(res, 'error')) {
|
if (Reflect.has(res, 'error')) {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
code: 0,
|
code: RESULT_CODE_ERROR,
|
||||||
msg: 'error',
|
msg: RESULT_MSG_ERROR,
|
||||||
data: res.error,
|
data: res.error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -260,24 +268,24 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
? await res.blob()
|
? await res.blob()
|
||||||
: await res.arrayBuffer();
|
: await res.arrayBuffer();
|
||||||
return {
|
return {
|
||||||
code: 1,
|
code: RESULT_CODE_SUCCESS,
|
||||||
msg: '成功',
|
msg: RESULT_MSG_SUCCESS,
|
||||||
data: data,
|
data: data,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
headers: res.headers,
|
headers: res.headers,
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
code: 0,
|
code: RESULT_CODE_ERROR,
|
||||||
msg: '未知响应数据类型',
|
msg: RESULT_MSG_NOT_TYPE,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// 请求被终止时
|
// 请求被终止时
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
return {
|
return {
|
||||||
code: 0,
|
code: RESULT_CODE_ERROR,
|
||||||
msg: '网络连接超时!',
|
msg: RESULT_MSG_TIMEOUT,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
@@ -294,14 +302,14 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
|||||||
function stateCode(res: Response) {
|
function stateCode(res: Response) {
|
||||||
if (res.status === 500) {
|
if (res.status === 500) {
|
||||||
return {
|
return {
|
||||||
code: 0,
|
code: RESULT_CODE_ERROR,
|
||||||
msg: '服务器连接出错!',
|
msg: RESULT_MSG_SERVER_ERROR,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (res.status === 404) {
|
if (res.status === 404) {
|
||||||
return {
|
return {
|
||||||
code: 0,
|
code: RESULT_CODE_ERROR,
|
||||||
msg: '请求地址错误',
|
msg: RESULT_MSG_URL_NOTFOUND,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (res.status === 401) {
|
if (res.status === 401) {
|
||||||
|
|||||||
Reference in New Issue
Block a user