vben-admin-thin-next/mock/_util.ts

46 lines
981 B
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
// Interface data format used to return a unified format
export function resultSuccess<T = any>(result: T, { message = 'ok' } = {}) {
return {
code: 0,
result,
message,
type: 'success',
};
}
2020-10-08 01:35:05 +08:00
export function resultPageSuccess<T = any>(
page: number,
pageSize: number,
list: T[],
{ message = 'ok' } = {}
) {
const pageData = pagination(page, pageSize, list);
2020-09-28 20:19:10 +08:00
return {
2021-02-25 20:17:08 +08:00
...resultSuccess({
2020-10-08 01:35:05 +08:00
items: pageData,
total: list.length,
2021-02-25 20:17:08 +08:00
}),
2020-09-28 20:19:10 +08:00
message,
};
}
export function resultError(message = 'Request failed', { code = -1, result = null } = {}) {
return {
code,
result,
message,
type: 'error',
};
}
export function pagination<T = any>(pageNo: number, pageSize: number, array: T[]): T[] {
2021-02-09 23:47:14 +08:00
const offset = (pageNo - 1) * Number(pageSize);
2020-11-01 11:30:58 +08:00
const ret =
offset + Number(pageSize) >= array.length
? array.slice(offset, array.length)
: array.slice(offset, offset + Number(pageSize));
return ret;
2020-09-28 20:19:10 +08:00
}