From 1348cb94ec89a22cdea6a018daffc7472557f5fa Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Thu, 14 Sep 2023 14:52:19 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=AF=B7=E6=B1=82=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/result-constants.ts | 23 ++++++++++++++++ src/plugins/http-fetch.ts | 44 ++++++++++++++++++------------- 2 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/constants/result-constants.ts diff --git a/src/constants/result-constants.ts b/src/constants/result-constants.ts new file mode 100644 index 00000000..9db18e1f --- /dev/null +++ b/src/constants/result-constants.ts @@ -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!'; \ No newline at end of file diff --git a/src/plugins/http-fetch.ts b/src/plugins/http-fetch.ts index a28eae01..f95135c9 100644 --- a/src/plugins/http-fetch.ts +++ b/src/plugins/http-fetch.ts @@ -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 { // 风格处理 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 { ? 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 { /** * 判断状态码处理结果信息(不可处理) * @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) {