index.mock.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import { faker } from '@faker-js/faker'
  2. import { SUCCESS_CODE } from '@/constants'
  3. import { toAnyString } from '@/utils'
  4. const timeout = 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. const cardList = [
  171. {
  172. logo: 'https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png',
  173. name: 'Alipay',
  174. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  175. },
  176. {
  177. logo: 'https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png',
  178. name: 'Angular',
  179. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  180. },
  181. {
  182. logo: 'https://gw.alipayobjects.com/zos/rmsportal/siCrBXXhmvTQGWPNLBow.png',
  183. name: 'Bootstrap',
  184. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  185. },
  186. {
  187. logo: 'https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png',
  188. name: 'React',
  189. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  190. },
  191. {
  192. logo: 'https://gw.alipayobjects.com/zos/rmsportal/ComBAopevLwENQdKWiIn.png',
  193. name: 'Vue',
  194. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  195. },
  196. {
  197. logo: 'https://gw.alipayobjects.com/zos/rmsportal/nxkuOJlFJuAUhzlMTCEe.png',
  198. name: 'Webpack',
  199. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  200. }
  201. ]
  202. export default [
  203. // 树形列表接口
  204. {
  205. url: '/mock/example/treeList',
  206. method: 'get',
  207. timeout,
  208. response: ({ query }) => {
  209. const { title, pageIndex, pageSize } = query
  210. const mockList = treeList.filter((item) => {
  211. if (title && item.title.indexOf(title) < 0) return false
  212. return true
  213. })
  214. const pageList = mockList.filter(
  215. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  216. )
  217. return {
  218. code: SUCCESS_CODE,
  219. data: {
  220. total: mockList.length,
  221. list: pageList
  222. }
  223. }
  224. }
  225. },
  226. // 列表接口
  227. {
  228. url: '/mock/example/list',
  229. method: 'get',
  230. timeout,
  231. response: ({ query }) => {
  232. const { title, pageIndex, pageSize } = query
  233. const mockList = List.filter((item) => {
  234. if (title && item.title.indexOf(title) < 0) return false
  235. return true
  236. })
  237. const pageList = mockList.filter(
  238. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  239. )
  240. return {
  241. code: SUCCESS_CODE,
  242. data: {
  243. total: mockList.length,
  244. list: pageList
  245. }
  246. }
  247. }
  248. },
  249. // 保存接口
  250. {
  251. url: '/mock/example/save',
  252. method: 'post',
  253. timeout,
  254. response: ({ body }) => {
  255. if (!body.id) {
  256. List = [
  257. Object.assign(body, {
  258. id: toAnyString()
  259. })
  260. ].concat(List)
  261. return {
  262. code: SUCCESS_CODE,
  263. data: 'success'
  264. }
  265. } else {
  266. List.map((item) => {
  267. if (item.id === body.id) {
  268. for (const key in item) {
  269. item[key] = body[key]
  270. }
  271. }
  272. })
  273. return {
  274. code: SUCCESS_CODE,
  275. data: 'success'
  276. }
  277. }
  278. }
  279. },
  280. // 详情接口
  281. {
  282. url: '/mock/example/detail',
  283. method: 'get',
  284. response: ({ query }) => {
  285. const { id } = query
  286. for (const example of List) {
  287. if (example.id === id) {
  288. return {
  289. code: SUCCESS_CODE,
  290. data: example
  291. }
  292. }
  293. }
  294. }
  295. },
  296. // 删除接口
  297. {
  298. url: '/mock/example/delete',
  299. method: 'post',
  300. response: ({ body }) => {
  301. const ids = body.ids
  302. if (!ids) {
  303. return {
  304. code: 500,
  305. message: '请选择需要删除的数据'
  306. }
  307. } else {
  308. let i = List.length
  309. while (i--) {
  310. if (ids.indexOf(List[i].id) !== -1) {
  311. List.splice(i, 1)
  312. }
  313. }
  314. return {
  315. code: SUCCESS_CODE,
  316. data: 'success'
  317. }
  318. }
  319. }
  320. },
  321. {
  322. url: '/mock/card/list',
  323. method: 'get',
  324. timeout,
  325. response: ({ query }) => {
  326. const { name, pageIndex, pageSize } = query
  327. const mockList = cardList.filter((item) => {
  328. if (name && item.name.indexOf(name) < 0) return false
  329. return true
  330. })
  331. const pageList = mockList.filter(
  332. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  333. )
  334. return {
  335. code: SUCCESS_CODE,
  336. data: {
  337. total: mockList.length,
  338. list: pageList
  339. }
  340. }
  341. }
  342. }
  343. ]