_util.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Interface data format used to return a unified format
  2. export function resultSuccess<T = Recordable>(result: T, { message = 'ok' } = {}) {
  3. return {
  4. code: 0,
  5. result,
  6. message,
  7. type: 'success',
  8. };
  9. }
  10. export function resultPageSuccess<T = any>(
  11. pageNo: number,
  12. pageSize: number,
  13. list: T[],
  14. { message = 'ok' } = {}
  15. ) {
  16. const pageData = pagination(pageNo, pageSize, list);
  17. return {
  18. ...resultSuccess({
  19. records: pageData,
  20. total: list.length,
  21. }),
  22. message,
  23. };
  24. }
  25. export function resultError(message = 'Request failed', { code = -1, result = null } = {}) {
  26. return {
  27. code,
  28. result,
  29. message,
  30. type: 'error',
  31. };
  32. }
  33. export function pagination<T = any>(pageNo: number, pageSize: number, array: T[]): T[] {
  34. const offset = (pageNo - 1) * Number(pageSize);
  35. const ret =
  36. offset + Number(pageSize) >= array.length
  37. ? array.slice(offset, array.length)
  38. : array.slice(offset, offset + Number(pageSize));
  39. return ret;
  40. }
  41. export interface requestParams {
  42. method: string;
  43. body: any;
  44. headers?: { authorization?: string };
  45. query: any;
  46. }
  47. /**
  48. * @description 本函数用于从request数据中获取token,请根据项目的实际情况修改
  49. *
  50. */
  51. export function getRequestToken({ headers }: requestParams): string | undefined {
  52. return headers?.authorization;
  53. }
  54. //TODO 接口父路径(写死不够灵活)
  55. export const baseUrl = '/jeecgboot/mock';