home.fake.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { faker } from "@faker-js/faker/locale/zh_CN";
  2. import { defineFakeRoute } from "vite-plugin-fake-server/client";
  3. import { resultSuccess } from "./utils";
  4. const home = {
  5. cardList: [],
  6. };
  7. export default defineFakeRoute([
  8. {
  9. url: "/home",
  10. timeout: 1000,
  11. method: "get",
  12. response: () => resultSuccess(home),
  13. },
  14. {
  15. url: "/home/pie",
  16. timeout: 1000,
  17. method: "get",
  18. // statusCode: 401,
  19. response: () => {
  20. const pie = [
  21. { value: faker.number.int({ min: 10, max: 100 }), code: "electronics" },
  22. { value: faker.number.int({ min: 10, max: 100 }), code: "home_goods" },
  23. {
  24. value: faker.number.int({ min: 10, max: 100 }),
  25. code: "apparel_accessories",
  26. },
  27. {
  28. value: faker.number.int({ min: 10, max: 100 }),
  29. code: "food_beverages",
  30. },
  31. {
  32. value: faker.number.int({ min: 10, max: 100 }),
  33. code: "beauty_skincare",
  34. },
  35. ];
  36. return resultSuccess(pie);
  37. },
  38. },
  39. {
  40. url: "/home/line",
  41. timeout: 1000,
  42. method: "post",
  43. response: ({ body }) => {
  44. if (body.range === "week") {
  45. return resultSuccess(
  46. Array.from({ length: 7 }).map(() =>
  47. faker.number.int({ min: 100, max: 1000 }),
  48. ),
  49. );
  50. }
  51. if (body.range === "month") {
  52. const currentDate = new Date();
  53. const currentDay = currentDate.getDate();
  54. return resultSuccess(
  55. Array.from({ length: currentDay }).map(() =>
  56. faker.number.int({ min: 100, max: 1000 }),
  57. ),
  58. );
  59. }
  60. if (body.range === "year") {
  61. const currentDate = new Date();
  62. const currentMonth = currentDate.getMonth();
  63. const currentYear = currentDate.getFullYear();
  64. const daysBeforeCurrentMonth = [];
  65. for (let month = 0; month < currentMonth; month++) {
  66. const daysInMonth = new Date(currentYear, month + 1, 0).getDate();
  67. for (let day = 1; day <= daysInMonth; day++) {
  68. daysBeforeCurrentMonth.push(day);
  69. }
  70. }
  71. return resultSuccess(
  72. Array.from({ length: daysBeforeCurrentMonth.length }).map(() =>
  73. faker.number.int({ min: 100, max: 1000 }),
  74. ),
  75. );
  76. }
  77. return resultSuccess([]);
  78. },
  79. },
  80. ]);