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

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
// Interface data format used to return a unified format
2021-03-23 23:03:29 +08:00
export function resultSuccess<T = Recordable>(result: T, { message = 'ok' } = {}) {
2020-09-28 20:19:10 +08:00
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[],
2021-08-24 22:41:48 +08:00
{ message = 'ok' } = {},
2020-10-08 01:35:05 +08:00
) {
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
}
export interface requestParams {
method: string;
body: any;
headers?: { authorization?: string };
query: any;
}
/**
* @description request数据中获取token
*
*/
export function getRequestToken({ headers }: requestParams): string | undefined {
return headers?.authorization;
}