mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-30 23:26:41 +08:00
new shared and DataTableResult
* new transform data to options function * new Service Result with Pagination info
This commit is contained in:
parent
458e387b68
commit
1795cf84f3
@ -4,3 +4,18 @@ export function transformObjectToOption<T extends object>(obj: T) {
|
|||||||
label
|
label
|
||||||
})) as Common.OptionWithKey<keyof T>[];
|
})) as Common.OptionWithKey<keyof T>[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于远程数据对象转换为select options
|
||||||
|
* @param obj 数据
|
||||||
|
* @param valueKey 值对应的字段名
|
||||||
|
* @param labelKey 标签对应的字段名
|
||||||
|
*/
|
||||||
|
export function transformDataToOption<T extends object>(obj: T, valueKey: string, labelKey: string) {
|
||||||
|
return Object.entries(obj).map(item => {
|
||||||
|
return {
|
||||||
|
label: item[1][labelKey],
|
||||||
|
value: item[1][valueKey]
|
||||||
|
};
|
||||||
|
}) as Common.OptionWithKey<keyof T>[];
|
||||||
|
}
|
||||||
|
@ -37,6 +37,7 @@ export default class CustomAxiosInstance {
|
|||||||
codeKey: 'code',
|
codeKey: 'code',
|
||||||
dataKey: 'data',
|
dataKey: 'data',
|
||||||
msgKey: 'message',
|
msgKey: 'message',
|
||||||
|
pageKey: 'page',
|
||||||
successCode: 200
|
successCode: 200
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
@ -63,7 +64,7 @@ export default class CustomAxiosInstance {
|
|||||||
},
|
},
|
||||||
(axiosError: AxiosError) => {
|
(axiosError: AxiosError) => {
|
||||||
const error = handleAxiosError(axiosError);
|
const error = handleAxiosError(axiosError);
|
||||||
return handleServiceResult(error, null);
|
return handleServiceResult(error, null, null);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.instance.interceptors.response.use(
|
this.instance.interceptors.response.use(
|
||||||
@ -71,10 +72,14 @@ export default class CustomAxiosInstance {
|
|||||||
const { status, config } = response;
|
const { status, config } = response;
|
||||||
if (status === 200 || status < 300 || status === 304) {
|
if (status === 200 || status < 300 || status === 304) {
|
||||||
const backend = response.data;
|
const backend = response.data;
|
||||||
const { codeKey, dataKey, successCode } = this.backendConfig;
|
const { codeKey, pageKey, dataKey, successCode } = this.backendConfig;
|
||||||
// 请求成功
|
// 请求成功
|
||||||
if (backend[codeKey] === successCode) {
|
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
|
// token失效, 刷新token
|
||||||
|
11
src/typings/system.d.ts
vendored
11
src/typings/system.d.ts
vendored
@ -26,6 +26,8 @@ declare namespace Service {
|
|||||||
dataKey: string;
|
dataKey: string;
|
||||||
/** 表示后端消息的属性字段 */
|
/** 表示后端消息的属性字段 */
|
||||||
msgKey: string;
|
msgKey: string;
|
||||||
|
/** 分页数据的属性字段 */
|
||||||
|
pageKey: string;
|
||||||
/** 后端业务上定义的成功请求的状态 */
|
/** 后端业务上定义的成功请求的状态 */
|
||||||
successCode: number | string;
|
successCode: number | string;
|
||||||
}
|
}
|
||||||
@ -38,6 +40,13 @@ declare namespace Service {
|
|||||||
data: T;
|
data: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 带分页的请求成功结果 */
|
||||||
|
interface DataTablResult<T = any> {
|
||||||
|
error: null;
|
||||||
|
data: T;
|
||||||
|
page: any;
|
||||||
|
}
|
||||||
|
|
||||||
/** 自定义的请求失败结果 */
|
/** 自定义的请求失败结果 */
|
||||||
interface FailedResult {
|
interface FailedResult {
|
||||||
/** 请求错误 */
|
/** 请求错误 */
|
||||||
@ -47,7 +56,7 @@ declare namespace Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 自定义的请求结果 */
|
/** 自定义的请求结果 */
|
||||||
type RequestResult<T = any> = SuccessResult<T> | FailedResult;
|
type RequestResult<T = any> = SuccessResult<T> | DataTablResult<T> | FailedResult;
|
||||||
|
|
||||||
/** 多个请求数据结果 */
|
/** 多个请求数据结果 */
|
||||||
type MultiRequestResult<T extends any[]> = T extends [infer First, ...infer Rest]
|
type MultiRequestResult<T extends any[]> = T extends [infer First, ...infer Rest]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/** 统一失败和成功的请求结果的数据类型 */
|
/** 统一失败和成功的请求结果的数据类型 */
|
||||||
export async function handleServiceResult<T = any>(error: Service.RequestError | null, data: any) {
|
export async function handleServiceResult<T = any>(error: Service.RequestError | null, page: any, data: any) {
|
||||||
if (error) {
|
if (error) {
|
||||||
const fail: Service.FailedResult = {
|
const fail: Service.FailedResult = {
|
||||||
error,
|
error,
|
||||||
@ -7,6 +7,15 @@ export async function handleServiceResult<T = any>(error: Service.RequestError |
|
|||||||
};
|
};
|
||||||
return fail;
|
return fail;
|
||||||
}
|
}
|
||||||
|
if (page) {
|
||||||
|
const pageresult: Service.DataTablResult = {
|
||||||
|
error: null,
|
||||||
|
page: page,
|
||||||
|
data
|
||||||
|
};
|
||||||
|
return pageresult;
|
||||||
|
}
|
||||||
|
|
||||||
const success: Service.SuccessResult<T> = {
|
const success: Service.SuccessResult<T> = {
|
||||||
error: null,
|
error: null,
|
||||||
data
|
data
|
||||||
@ -34,10 +43,23 @@ export function adapter<T extends Service.ServiceAdapter>(
|
|||||||
|
|
||||||
if (!hasError) {
|
if (!hasError) {
|
||||||
const adapterFunArgs = args.map(item => item.data);
|
const adapterFunArgs = args.map(item => item.data);
|
||||||
result = {
|
const hasPage = args.some(item => {
|
||||||
error: null,
|
if (item.page) {
|
||||||
data: adapterFun(...adapterFunArgs)
|
result = {
|
||||||
};
|
error: null,
|
||||||
|
page: item.page,
|
||||||
|
data: adapterFun(...adapterFunArgs)
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
if(!hasPage){
|
||||||
|
result = {
|
||||||
|
error: null,
|
||||||
|
data: adapterFun(...adapterFunArgs)
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result!;
|
return result!;
|
||||||
|
Loading…
Reference in New Issue
Block a user