Skip to content

Commit

Permalink
fixbug: 修改请求问题
Browse files Browse the repository at this point in the history
  • Loading branch information
jsxiaosi committed Mar 16, 2022
1 parent c55ed0f commit 91e3668
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/hooks/web/useMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElMessageBox, ElMessageBoxOptions } from 'element-plus';
import { ElMessage, ElMessageBox, ElMessageBoxOptions } from 'element-plus';
import { useI18n } from '@/hooks/web/useI18n';

const { t } = useI18n();
Expand All @@ -18,8 +18,13 @@ function createErrorModal(message: string) {
});
}

function createErrorMsg(message: string) {
ElMessage.error(message);
}

export function useMessage() {
return {
createErrorModal,
createErrorMsg,
};
}
7 changes: 4 additions & 3 deletions src/utils/axios/axiosStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorMessageMode } from '#/axios';
import i18n from '@/locales';
import { ElMessage } from 'element-plus';
import { useMessage } from '@/hooks/web/useMessage';
const { createErrorModal, createErrorMsg } = useMessage();

export function checkStatus(
status: number,
Expand Down Expand Up @@ -53,9 +54,9 @@ export function checkStatus(
if (errMessage) {
if (errorMessageMode === 'modal') {
console.log('进来这里了吗?');
ElMessage.error(msg);
createErrorModal(msg);
} else if (errorMessageMode === 'message') {
ElMessage.error(errMessage);
createErrorMsg(errMessage);
}
}
}
25 changes: 19 additions & 6 deletions src/utils/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@ import { checkStatus } from './axiosStatus';
import { isString } from 'lodash';
import { useMessage } from '@/hooks/web/useMessage';

const { createErrorModal } = useMessage();
const { createErrorModal, createErrorMsg } = useMessage();

/**
* @description:一下所有拦截器请根据自身使用场景更改
*/
const interceptor: AxiosInterceptor = {
/**
* @description: 处理请求数据。如果数据不是预期格式,可直接抛出错误
*/
requestHook: (res, options) => {
/**
* 此处方法是对请求回来的数据进行处理,
* 根据自己的使用场景更改
*/
const { data } = res;
const { isShowData, errorMessageMode } = options;

if (!data) return res;
if (!data.data) return res;

if (data.code === -1) {
createErrorModal(data.message);
if (errorMessageMode === 'modal') {
console.log('进来这里了吗?');
createErrorModal(data.errMsg);
} else if (errorMessageMode === 'message') {
createErrorMsg(data.errMsg);
}
}

if (options.isShowData) return data;
if (isShowData) return data;

return res;
},
Expand Down Expand Up @@ -76,7 +89,7 @@ const interceptor: AxiosInterceptor = {
responseInterceptorsCatch: (error: any) => {
const { response, message, config } = error || {};
const errorMessageMode = config.requestOptions.errorMessageMode || 'none';
checkStatus(response.status, message, errorMessageMode);
checkStatus(response ? response.status : 404, message, errorMessageMode);
return error;
},
};
Expand All @@ -90,7 +103,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
// (拦截器)数据处理方式
interceptor,
headers: { 'Content-Type': 'application/json' },
// 配置项,下面的选项都可以在独立的接口请求中覆盖
// 配置项(需要在拦截器中做的处理),下面的选项都可以在独立的接口请求中覆盖
requestOptions: {
withToken: true,
errorMessageMode: 'message',
Expand Down
2 changes: 1 addition & 1 deletion types/axios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export interface RequestOptions {
}
export interface Result<T = any> {
code: number;
message: string;
errMsg: string;
data: T;
}

0 comments on commit 91e3668

Please sign in to comment.