index.mock.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { faker } from '@faker-js/faker'
  2. import { SUCCESS_CODE } from '@/constants'
  3. import { toAnyString } from '@/utils'
  4. const delay = 1000
  5. const count = 100
  6. const baseContent =
  7. '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>'
  8. interface ListProps {
  9. id: string
  10. author: string
  11. title: string
  12. content: string
  13. importance: number
  14. display_time: any
  15. pageviews: number
  16. image_uri: string
  17. video_uri?: string
  18. }
  19. interface TreeListProps {
  20. id: string
  21. author: string
  22. title: string
  23. content: string
  24. importance: number
  25. display_time: any
  26. image_uri: string
  27. pageviews: number
  28. video_uri?: string
  29. children?: TreeListProps[]
  30. }
  31. let List: ListProps[] = []
  32. for (let i = 0; i < count; i++) {
  33. List.push({
  34. id: toAnyString(),
  35. // timestamp: +Mock.Random.date('T'),
  36. author: faker.person.firstName(),
  37. title: faker.lorem.sentence(),
  38. content: baseContent,
  39. importance: faker.number.int({ min: 1, max: 3 }),
  40. display_time: faker.date.anytime(),
  41. pageviews: faker.number.int({ min: 300, max: 5000 }),
  42. image_uri: faker.image.url({
  43. width: faker.number.int({ min: 200, max: 400 }),
  44. height: faker.number.int({ min: 200, max: 400 })
  45. }),
  46. video_uri:
  47. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  48. })
  49. }
  50. const treeList: TreeListProps[] = []
  51. for (let i = 0; i < count; i++) {
  52. treeList.push({
  53. id: toAnyString(),
  54. // timestamp: +Mock.Random.date('T'),
  55. author: faker.person.firstName(),
  56. title: faker.lorem.sentence(),
  57. content: baseContent,
  58. importance: faker.number.int({ min: 1, max: 3 }),
  59. display_time: faker.date.anytime(),
  60. pageviews: faker.number.int({ min: 300, max: 5000 }),
  61. image_uri: faker.image.url({
  62. width: faker.number.int({ min: 200, max: 400 }),
  63. height: faker.number.int({ min: 200, max: 400 })
  64. }),
  65. video_uri:
  66. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4',
  67. children: [
  68. {
  69. id: toAnyString(),
  70. // timestamp: +Mock.Random.date('T'),
  71. author: faker.person.firstName(),
  72. title: faker.lorem.sentence(),
  73. content: baseContent,
  74. importance: faker.number.int({ min: 1, max: 3 }),
  75. display_time: faker.date.anytime(),
  76. pageviews: faker.number.int({ min: 300, max: 5000 }),
  77. image_uri: faker.image.url({
  78. width: faker.number.int({ min: 200, max: 400 }),
  79. height: faker.number.int({ min: 200, max: 400 })
  80. }),
  81. video_uri:
  82. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4',
  83. children: [
  84. {
  85. id: toAnyString(),
  86. // timestamp: +Mock.Random.date('T'),
  87. author: faker.person.firstName(),
  88. title: faker.lorem.sentence(),
  89. content: baseContent,
  90. importance: faker.number.int({ min: 1, max: 3 }),
  91. display_time: faker.date.anytime(),
  92. pageviews: faker.number.int({ min: 300, max: 5000 }),
  93. image_uri: faker.image.url({
  94. width: faker.number.int({ min: 200, max: 400 }),
  95. height: faker.number.int({ min: 200, max: 400 })
  96. }),
  97. video_uri:
  98. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  99. },
  100. {
  101. id: toAnyString(),
  102. // timestamp: +Mock.Random.date('T'),
  103. author: faker.person.firstName(),
  104. title: faker.lorem.sentence(),
  105. content: baseContent,
  106. importance: faker.number.int({ min: 1, max: 3 }),
  107. display_time: faker.date.anytime(),
  108. pageviews: faker.number.int({ min: 300, max: 5000 }),
  109. image_uri: faker.image.url({
  110. width: faker.number.int({ min: 200, max: 400 }),
  111. height: faker.number.int({ min: 200, max: 400 })
  112. }),
  113. video_uri:
  114. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  115. }
  116. ]
  117. },
  118. {
  119. id: toAnyString(),
  120. // timestamp: +Mock.Random.date('T'),
  121. author: faker.person.firstName(),
  122. title: faker.lorem.sentence(),
  123. content: baseContent,
  124. importance: faker.number.int({ min: 1, max: 3 }),
  125. display_time: faker.date.anytime(),
  126. pageviews: faker.number.int({ min: 300, max: 5000 }),
  127. image_uri: faker.image.url({
  128. width: faker.number.int({ min: 200, max: 400 }),
  129. height: faker.number.int({ min: 200, max: 400 })
  130. }),
  131. video_uri:
  132. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  133. },
  134. {
  135. id: toAnyString(),
  136. // timestamp: +Mock.Random.date('T'),
  137. author: faker.person.firstName(),
  138. title: faker.lorem.sentence(),
  139. content: baseContent,
  140. importance: faker.number.int({ min: 1, max: 3 }),
  141. display_time: faker.date.anytime(),
  142. pageviews: faker.number.int({ min: 300, max: 5000 }),
  143. image_uri: faker.image.url({
  144. width: faker.number.int({ min: 200, max: 400 }),
  145. height: faker.number.int({ min: 200, max: 400 })
  146. }),
  147. video_uri:
  148. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  149. },
  150. {
  151. id: toAnyString(),
  152. // timestamp: +Mock.Random.date('T'),
  153. author: faker.person.firstName(),
  154. title: faker.lorem.sentence(),
  155. content: baseContent,
  156. importance: faker.number.int({ min: 1, max: 3 }),
  157. display_time: faker.date.anytime(),
  158. pageviews: faker.number.int({ min: 300, max: 5000 }),
  159. image_uri: faker.image.url({
  160. width: faker.number.int({ min: 200, max: 400 }),
  161. height: faker.number.int({ min: 200, max: 400 })
  162. }),
  163. video_uri:
  164. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  165. }
  166. ]
  167. // image_uri
  168. })
  169. }
  170. export default [
  171. // 树形列表接口
  172. {
  173. url: '/mock/example/treeList',
  174. method: 'GET',
  175. delay,
  176. body: ({ query }) => {
  177. const { title, pageIndex, pageSize } = query
  178. const mockList = treeList.filter((item) => {
  179. if (title && item.title.indexOf(title) < 0) return false
  180. return true
  181. })
  182. const pageList = mockList.filter(
  183. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  184. )
  185. return {
  186. code: SUCCESS_CODE,
  187. data: {
  188. total: mockList.length,
  189. list: pageList
  190. }
  191. }
  192. }
  193. },
  194. // 列表接口
  195. {
  196. url: '/mock/example/list',
  197. method: 'GET',
  198. delay,
  199. body: ({ query }) => {
  200. const { title, pageIndex, pageSize } = query
  201. const mockList = List.filter((item) => {
  202. if (title && item.title.indexOf(title) < 0) return false
  203. return true
  204. })
  205. const pageList = mockList.filter(
  206. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  207. )
  208. return {
  209. code: SUCCESS_CODE,
  210. data: {
  211. total: mockList.length,
  212. list: pageList
  213. }
  214. }
  215. }
  216. },
  217. // 保存接口
  218. {
  219. url: '/mock/example/save',
  220. method: 'POST',
  221. delay,
  222. body: ({ body }) => {
  223. if (!body.id) {
  224. List = [
  225. Object.assign(body, {
  226. id: toAnyString()
  227. })
  228. ].concat(List)
  229. return {
  230. code: SUCCESS_CODE,
  231. data: 'success'
  232. }
  233. } else {
  234. List.map((item) => {
  235. if (item.id === body.id) {
  236. for (const key in item) {
  237. item[key] = body[key]
  238. }
  239. }
  240. })
  241. return {
  242. code: SUCCESS_CODE,
  243. data: 'success'
  244. }
  245. }
  246. }
  247. },
  248. // 详情接口
  249. {
  250. url: '/mock/example/detail',
  251. method: 'GET',
  252. body: ({ query }) => {
  253. const { id } = query
  254. for (const example of List) {
  255. if (example.id === id) {
  256. return {
  257. code: SUCCESS_CODE,
  258. data: example
  259. }
  260. }
  261. }
  262. }
  263. },
  264. // 删除接口
  265. {
  266. url: '/mock/example/delete',
  267. method: 'POST',
  268. body: ({ body }) => {
  269. const ids = body.ids
  270. if (!ids) {
  271. return {
  272. code: 500,
  273. message: '请选择需要删除的数据'
  274. }
  275. } else {
  276. let i = List.length
  277. while (i--) {
  278. if (ids.indexOf(List[i].id) !== -1) {
  279. List.splice(i, 1)
  280. }
  281. }
  282. return {
  283. code: SUCCESS_CODE,
  284. data: 'success'
  285. }
  286. }
  287. }
  288. }
  289. ]