index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import config from '@/config/axios/config'
  2. import { MockMethod } from 'vite-plugin-mock'
  3. import { toAnyString } from '@/utils'
  4. import Mock from 'mockjs'
  5. const { code } = config
  6. const timeout = 1000
  7. const count = 100
  8. const baseContent =
  9. '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
  10. let List: {
  11. id: string
  12. author: string
  13. title: string
  14. content: string
  15. importance: number
  16. display_time: string
  17. pageviews: number
  18. }[] = []
  19. for (let i = 0; i < count; i++) {
  20. List.push(
  21. Mock.mock({
  22. id: toAnyString(),
  23. // timestamp: +Mock.Random.date('T'),
  24. author: '@first',
  25. title: '@title(5, 10)',
  26. content: baseContent,
  27. importance: '@integer(1, 3)',
  28. display_time: '@datetime',
  29. pageviews: '@integer(300, 5000)'
  30. // image_uri
  31. })
  32. )
  33. }
  34. export default [
  35. // 列表接口
  36. {
  37. url: '/example/list',
  38. method: 'get',
  39. timeout,
  40. response: ({ query }) => {
  41. const { title, pageIndex, pageSize } = query
  42. const mockList = List.filter((item) => {
  43. if (title && item.title.indexOf(title) < 0) return false
  44. return true
  45. })
  46. const pageList = mockList.filter(
  47. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  48. )
  49. return {
  50. data: {
  51. code: code,
  52. data: {
  53. total: mockList.length,
  54. list: pageList
  55. }
  56. }
  57. }
  58. }
  59. },
  60. // 保存接口
  61. {
  62. url: '/example/save',
  63. method: 'post',
  64. timeout,
  65. response: ({ body }) => {
  66. if (!body.id) {
  67. List = [
  68. Object.assign(body, {
  69. id: toAnyString()
  70. })
  71. ].concat(List)
  72. return {
  73. data: {
  74. code: code,
  75. data: 'success'
  76. }
  77. }
  78. } else {
  79. List.map((item) => {
  80. if (item.id === body.id) {
  81. for (const key in item) {
  82. item[key] = body[key]
  83. }
  84. }
  85. })
  86. return {
  87. data: {
  88. code: code,
  89. data: 'success'
  90. }
  91. }
  92. }
  93. }
  94. },
  95. // 详情接口
  96. {
  97. url: '/example/detail',
  98. method: 'get',
  99. response: ({ query }) => {
  100. const { id } = query
  101. for (const example of List) {
  102. if (example.id === id) {
  103. return {
  104. data: {
  105. code: code,
  106. data: example
  107. }
  108. }
  109. }
  110. }
  111. }
  112. },
  113. // 删除接口
  114. {
  115. url: '/example/delete',
  116. method: 'post',
  117. response: ({ body }) => {
  118. const ids = body.ids
  119. if (!ids) {
  120. return {
  121. code: '500',
  122. message: '请选择需要删除的数据'
  123. }
  124. } else {
  125. let i = List.length
  126. while (i--) {
  127. if (ids.indexOf(List[i].id) !== -1) {
  128. List.splice(i, 1)
  129. }
  130. }
  131. return {
  132. data: {
  133. code: code,
  134. data: 'success'
  135. }
  136. }
  137. }
  138. }
  139. }
  140. ] as MockMethod[]