fix 请求信息封装常量
This commit is contained in:
@@ -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