index.mock.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import Mock from 'mockjs'
  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. Mock.mock({
  35. id: toAnyString(),
  36. // timestamp: +Mock.Random.date('T'),
  37. author: '@first',
  38. title: '@title(5, 10)',
  39. content: baseContent,
  40. importance: '@integer(1, 3)',
  41. display_time: '@datetime',
  42. pageviews: '@integer(100, 500)',
  43. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)'),
  44. video_uri:
  45. '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4'
  46. })
  47. )
  48. }
  49. const treeList: TreeListProps[] = []
  50. for (let i = 0; i < count; i++) {
  51. treeList.push(
  52. Mock.mock({
  53. id: toAnyString(),
  54. // timestamp: +Mock.Random.date('T'),
  55. author: '@first',
  56. title: '@title(5, 10)',
  57. content: baseContent,
  58. importance: '@integer(1, 3)',
  59. display_time: '@datetime',
  60. pageviews: '@integer(300, 5000)',
  61. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)'),
  62. children: [
  63. {
  64. id: toAnyString(),
  65. // timestamp: +Mock.Random.date('T'),
  66. author: '@first',
  67. title: '@title(5, 10)',
  68. content: baseContent,
  69. importance: '@integer(1, 3)',
  70. display_time: '@datetime',
  71. pageviews: '@integer(300, 5000)',
  72. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)'),
  73. children: [
  74. {
  75. id: toAnyString(),
  76. // timestamp: +Mock.Random.date('T'),
  77. author: '@first',
  78. title: '@title(5, 10)',
  79. content: baseContent,
  80. importance: '@integer(1, 3)',
  81. display_time: '@datetime',
  82. pageviews: '@integer(300, 5000)',
  83. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)')
  84. },
  85. {
  86. id: toAnyString(),
  87. // timestamp: +Mock.Random.date('T'),
  88. author: '@first',
  89. title: '@title(5, 10)',
  90. content: baseContent,
  91. importance: '@integer(1, 3)',
  92. display_time: '@datetime',
  93. pageviews: '@integer(300, 5000)',
  94. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)')
  95. }
  96. ]
  97. },
  98. {
  99. id: toAnyString(),
  100. // timestamp: +Mock.Random.date('T'),
  101. author: '@first',
  102. title: '@title(5, 10)',
  103. content: baseContent,
  104. importance: '@integer(1, 3)',
  105. display_time: '@datetime',
  106. pageviews: '@integer(300, 5000)',
  107. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)')
  108. },
  109. {
  110. id: toAnyString(),
  111. // timestamp: +Mock.Random.date('T'),
  112. author: '@first',
  113. title: '@title(5, 10)',
  114. content: baseContent,
  115. importance: '@integer(1, 3)',
  116. display_time: '@datetime',
  117. pageviews: '@integer(300, 5000)',
  118. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)')
  119. },
  120. {
  121. id: toAnyString(),
  122. // timestamp: +Mock.Random.date('T'),
  123. author: '@first',
  124. title: '@title(5, 10)',
  125. content: baseContent,
  126. importance: '@integer(1, 3)',
  127. display_time: '@datetime',
  128. pageviews: '@integer(300, 5000)',
  129. image_uri: Mock.Random.image('@integer(100, 500)x@integer(100, 500)')
  130. }
  131. ]
  132. // image_uri
  133. })
  134. )
  135. }
  136. const cardList = [
  137. {
  138. logo: 'https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png',
  139. name: 'Alipay',
  140. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  141. },
  142. {
  143. logo: 'https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png',
  144. name: 'Angular',
  145. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  146. },
  147. {
  148. logo: 'https://gw.alipayobjects.com/zos/rmsportal/siCrBXXhmvTQGWPNLBow.png',
  149. name: 'Bootstrap',
  150. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  151. },
  152. {
  153. logo: 'https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png',
  154. name: 'React',
  155. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  156. },
  157. {
  158. logo: 'https://gw.alipayobjects.com/zos/rmsportal/ComBAopevLwENQdKWiIn.png',
  159. name: 'Vue',
  160. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  161. },
  162. {
  163. logo: 'https://gw.alipayobjects.com/zos/rmsportal/nxkuOJlFJuAUhzlMTCEe.png',
  164. name: 'Webpack',
  165. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  166. }
  167. ]
  168. export default [
  169. // 树形列表接口
  170. {
  171. url: '/mock/example/treeList',
  172. method: 'get',
  173. timeout,
  174. response: ({ query }) => {
  175. const { title, pageIndex, pageSize } = query
  176. const mockList = treeList.filter((item) => {
  177. if (title && item.title.indexOf(title) < 0) return false
  178. return true
  179. })
  180. const pageList = mockList.filter(
  181. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  182. )
  183. return {
  184. code: SUCCESS_CODE,
  185. data: {
  186. total: mockList.length,
  187. list: pageList
  188. }
  189. }
  190. }
  191. },
  192. // 列表接口
  193. {
  194. url: '/mock/example/list',
  195. method: 'get',
  196. timeout,
  197. response: ({ query }) => {
  198. const { title, pageIndex, pageSize } = query
  199. const mockList = List.filter((item) => {
  200. if (title && item.title.indexOf(title) < 0) return false
  201. return true
  202. })
  203. const pageList = mockList.filter(
  204. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  205. )
  206. return {
  207. code: SUCCESS_CODE,
  208. data: {
  209. total: mockList.length,
  210. list: pageList
  211. }
  212. }
  213. }
  214. },
  215. // 保存接口
  216. {
  217. url: '/mock/example/save',
  218. method: 'post',
  219. timeout,
  220. response: ({ body }) => {
  221. if (!body.id) {
  222. List = [
  223. Object.assign(body, {
  224. id: toAnyString()
  225. })
  226. ].concat(List)
  227. return {
  228. code: SUCCESS_CODE,
  229. data: 'success'
  230. }
  231. } else {
  232. List.map((item) => {
  233. if (item.id === body.id) {
  234. for (const key in item) {
  235. item[key] = body[key]
  236. }
  237. }
  238. })
  239. return {
  240. code: SUCCESS_CODE,
  241. data: 'success'
  242. }
  243. }
  244. }
  245. },
  246. // 详情接口
  247. {
  248. url: '/mock/example/detail',
  249. method: 'get',
  250. response: ({ query }) => {
  251. const { id } = query
  252. for (const example of List) {
  253. if (example.id === id) {
  254. return {
  255. code: SUCCESS_CODE,
  256. data: example
  257. }
  258. }
  259. }
  260. }
  261. },
  262. // 删除接口
  263. {
  264. url: '/mock/example/delete',
  265. method: 'post',
  266. response: ({ body }) => {
  267. const ids = body.ids
  268. if (!ids) {
  269. return {
  270. code: 500,
  271. message: '请选择需要删除的数据'
  272. }
  273. } else {
  274. let i = List.length
  275. while (i--) {
  276. if (ids.indexOf(List[i].id) !== -1) {
  277. List.splice(i, 1)
  278. }
  279. }
  280. return {
  281. code: SUCCESS_CODE,
  282. data: 'success'
  283. }
  284. }
  285. }
  286. },
  287. {
  288. url: '/mock/card/list',
  289. method: 'get',
  290. timeout,
  291. response: ({ query }) => {
  292. const { name, pageIndex, pageSize } = query
  293. const mockList = cardList.filter((item) => {
  294. if (name && item.name.indexOf(name) < 0) return false
  295. return true
  296. })
  297. const pageList = mockList.filter(
  298. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  299. )
  300. return {
  301. code: SUCCESS_CODE,
  302. data: {
  303. total: mockList.length,
  304. list: pageList
  305. }
  306. }
  307. }
  308. }
  309. ]