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_VERSION,
|
||||
} 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 = {
|
||||
/**响应码 */
|
||||
code: number | 200 | 500;
|
||||
code: number | 1 | 0;
|
||||
/**信息 */
|
||||
msg: string;
|
||||
/**数据 */
|
||||
@@ -78,8 +88,6 @@ const FATCH_OPTIONS: OptionsType = {
|
||||
headers: {
|
||||
[APP_REQUEST_HEADER_CODE]: import.meta.env.VITE_APP_CODE,
|
||||
[APP_REQUEST_HEADER_VERSION]: import.meta.env.VITE_APP_VERSION,
|
||||
// 使用mock.apifox.cn时开启
|
||||
// apifoxToken: '8zCzh3vipdEwd1ukv9lQEuTekdWIH7xN',
|
||||
},
|
||||
dataType: 'json',
|
||||
responseType: 'json',
|
||||
@@ -176,15 +184,15 @@ function interceptorResponse(res: ResultType): ResultType | Promise<any> {
|
||||
// 风格处理
|
||||
if (!Reflect.has(res, 'code')) {
|
||||
return Promise.resolve({
|
||||
code: 1,
|
||||
msg: 'success',
|
||||
code: RESULT_CODE_SUCCESS,
|
||||
msg: RESULT_MSG_SUCCESS,
|
||||
data: res,
|
||||
});
|
||||
}
|
||||
if (Reflect.has(res, 'error')) {
|
||||
return Promise.resolve({
|
||||
code: 0,
|
||||
msg: 'error',
|
||||
code: RESULT_CODE_ERROR,
|
||||
msg: RESULT_MSG_ERROR,
|
||||
data: res.error,
|
||||
});
|
||||
}
|
||||
@@ -260,24 +268,24 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
||||
? await res.blob()
|
||||
: await res.arrayBuffer();
|
||||
return {
|
||||
code: 1,
|
||||
msg: '成功',
|
||||
code: RESULT_CODE_SUCCESS,
|
||||
msg: RESULT_MSG_SUCCESS,
|
||||
data: data,
|
||||
status: res.status,
|
||||
headers: res.headers,
|
||||
};
|
||||
default:
|
||||
return {
|
||||
code: 0,
|
||||
msg: '未知响应数据类型',
|
||||
code: RESULT_CODE_ERROR,
|
||||
msg: RESULT_MSG_NOT_TYPE,
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
// 请求被终止时
|
||||
if (error.name === 'AbortError') {
|
||||
return {
|
||||
code: 0,
|
||||
msg: '网络连接超时!',
|
||||
code: RESULT_CODE_ERROR,
|
||||
msg: RESULT_MSG_TIMEOUT,
|
||||
};
|
||||
}
|
||||
throw error;
|
||||
@@ -289,19 +297,19 @@ export async function request(options: OptionsType): Promise<ResultType> {
|
||||
/**
|
||||
* 判断状态码处理结果信息(不可处理)
|
||||
* @param res 请求结果
|
||||
* @returns
|
||||
* @returns
|
||||
*/
|
||||
function stateCode(res: Response) {
|
||||
if (res.status === 500) {
|
||||
return {
|
||||
code: 0,
|
||||
msg: '服务器连接出错!',
|
||||
code: RESULT_CODE_ERROR,
|
||||
msg: RESULT_MSG_SERVER_ERROR,
|
||||
};
|
||||
}
|
||||
if (res.status === 404) {
|
||||
return {
|
||||
code: 0,
|
||||
msg: '请求地址错误',
|
||||
code: RESULT_CODE_ERROR,
|
||||
msg: RESULT_MSG_URL_NOTFOUND,
|
||||
};
|
||||
}
|
||||
if (res.status === 401) {
|
||||
|
||||
Reference in New Issue
Block a user