mock.hbs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { MockMethod } from 'vite-plugin-mock'
  2. import { handleRandomImage } from '../utils/index.ts'
  3. const List: {
  4. uuid: string
  5. id: string
  6. title: string
  7. description: string
  8. 'status|1': string[]
  9. author: string
  10. datetime: string
  11. count: string
  12. image: string
  13. switch: string
  14. percent: string
  15. 'rate|1': number[]
  16. 'type|1': number[]
  17. percentage: string
  18. }[] = []
  19. const count = 50
  20. for (let i = 0; i < count; i++) {
  21. List.push({
  22. uuid: '@uuid',
  23. id: '@id',
  24. title: '@ctitle(5, 10)',
  25. description: '@cparagraph',
  26. 'status|1': ['published', 'draft', 'deleted'],
  27. author: '@cname',
  28. datetime: '@datetime',
  29. count: '@integer(300, 5000)',
  30. image: handleRandomImage(),
  31. switch: '@boolean',
  32. percent: '@integer(80,99)',
  33. 'rate|1': [1, 2, 3, 4, 5],
  34. 'type|1': [0, 1],
  35. percentage: '@integer(0,100)',
  36. })
  37. }
  38. export default [
  39. {
  40. url: '/{{name}}/getList',
  41. method: 'get',
  42. response: ({ query }: any) => {
  43. const { title, pageNo = 1, pageSize = 20 } = query
  44. const mockList = List.filter((item: { title: string | any[] }) => !(title && item.title.indexOf(title) < 0))
  45. const list = mockList.filter((item: any, index: number) => index < pageSize * pageNo && index >= pageSize * (pageNo - 1))
  46. return {
  47. code: 200,
  48. msg: 'success',
  49. data: { list, ...{ total: mockList.length } },
  50. }
  51. },
  52. },
  53. {
  54. url: '/{{name}}/doEdit',
  55. method: 'post',
  56. response: () => {
  57. return {
  58. code: 200,
  59. msg: '模拟保存成功',
  60. }
  61. },
  62. },
  63. {
  64. url: '/{{name}}/doDelete',
  65. method: 'post',
  66. response: () => {
  67. return {
  68. code: 200,
  69. msg: '模拟删除成功',
  70. }
  71. },
  72. },
  73. ] as MockMethod[]