From 1795cf84f31ceace3f315d73a4fd09b717203f4c Mon Sep 17 00:00:00 2001 From: xumin Date: Thu, 20 Jul 2023 19:20:42 +0800 Subject: [PATCH] new shared and DataTableResult * new transform data to options function * new Service Result with Pagination info --- src/constants/_shared.ts | 15 +++++++++++++++ src/service/request/instance.ts | 11 ++++++++--- src/typings/system.d.ts | 11 ++++++++++- src/utils/service/handler.ts | 32 +++++++++++++++++++++++++++----- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/constants/_shared.ts b/src/constants/_shared.ts index 92d009db..d51e6160 100644 --- a/src/constants/_shared.ts +++ b/src/constants/_shared.ts @@ -4,3 +4,18 @@ export function transformObjectToOption(obj: T) { label })) as Common.OptionWithKey[]; } + +/** + * 基于远程数据对象转换为select options + * @param obj 数据 + * @param valueKey 值对应的字段名 + * @param labelKey 标签对应的字段名 + */ +export function transformDataToOption(obj: T, valueKey: string, labelKey: string) { + return Object.entries(obj).map(item => { + return { + label: item[1][labelKey], + value: item[1][valueKey] + }; + }) as Common.OptionWithKey[]; +} diff --git a/src/service/request/instance.ts b/src/service/request/instance.ts index d625927f..4b085e4e 100644 --- a/src/service/request/instance.ts +++ b/src/service/request/instance.ts @@ -37,6 +37,7 @@ export default class CustomAxiosInstance { codeKey: 'code', dataKey: 'data', msgKey: 'message', + pageKey: 'page', successCode: 200 } ) { @@ -63,7 +64,7 @@ export default class CustomAxiosInstance { }, (axiosError: AxiosError) => { const error = handleAxiosError(axiosError); - return handleServiceResult(error, null); + return handleServiceResult(error, null, null); } ); this.instance.interceptors.response.use( @@ -71,10 +72,14 @@ export default class CustomAxiosInstance { const { status, config } = response; if (status === 200 || status < 300 || status === 304) { const backend = response.data; - const { codeKey, dataKey, successCode } = this.backendConfig; + const { codeKey, pageKey, dataKey, successCode } = this.backendConfig; // 请求成功 if (backend[codeKey] === successCode) { - return handleServiceResult(null, backend[dataKey]); + // 分页处理 + if (backend[pageKey]) { + return handleServiceResult(null, backend[pageKey], backend[dataKey]); + } + return handleServiceResult(null, null, backend[dataKey]); } // token失效, 刷新token diff --git a/src/typings/system.d.ts b/src/typings/system.d.ts index b01f459d..c4ecdc35 100644 --- a/src/typings/system.d.ts +++ b/src/typings/system.d.ts @@ -26,6 +26,8 @@ declare namespace Service { dataKey: string; /** 表示后端消息的属性字段 */ msgKey: string; + /** 分页数据的属性字段 */ + pageKey: string; /** 后端业务上定义的成功请求的状态 */ successCode: number | string; } @@ -38,6 +40,13 @@ declare namespace Service { data: T; } + /** 带分页的请求成功结果 */ + interface DataTablResult { + error: null; + data: T; + page: any; + } + /** 自定义的请求失败结果 */ interface FailedResult { /** 请求错误 */ @@ -47,7 +56,7 @@ declare namespace Service { } /** 自定义的请求结果 */ - type RequestResult = SuccessResult | FailedResult; + type RequestResult = SuccessResult | DataTablResult | FailedResult; /** 多个请求数据结果 */ type MultiRequestResult = T extends [infer First, ...infer Rest] diff --git a/src/utils/service/handler.ts b/src/utils/service/handler.ts index 235ed706..b3ca1669 100644 --- a/src/utils/service/handler.ts +++ b/src/utils/service/handler.ts @@ -1,5 +1,5 @@ /** 统一失败和成功的请求结果的数据类型 */ -export async function handleServiceResult(error: Service.RequestError | null, data: any) { +export async function handleServiceResult(error: Service.RequestError | null, page: any, data: any) { if (error) { const fail: Service.FailedResult = { error, @@ -7,6 +7,15 @@ export async function handleServiceResult(error: Service.RequestError | }; return fail; } + if (page) { + const pageresult: Service.DataTablResult = { + error: null, + page: page, + data + }; + return pageresult; + } + const success: Service.SuccessResult = { error: null, data @@ -34,10 +43,23 @@ export function adapter( if (!hasError) { const adapterFunArgs = args.map(item => item.data); - result = { - error: null, - data: adapterFun(...adapterFunArgs) - }; + const hasPage = args.some(item => { + if (item.page) { + result = { + error: null, + page: item.page, + data: adapterFun(...adapterFunArgs) + }; + return true; + } + return false; + }); + if(!hasPage){ + result = { + error: null, + data: adapterFun(...adapterFunArgs) + }; + } } return result!;