index.mock.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // image_uri
  63. })
  64. )
  65. }
  66. const cardList = [
  67. {
  68. logo: 'https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png',
  69. name: 'Alipay',
  70. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  71. },
  72. {
  73. logo: 'https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png',
  74. name: 'Angular',
  75. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  76. },
  77. {
  78. logo: 'https://gw.alipayobjects.com/zos/rmsportal/siCrBXXhmvTQGWPNLBow.png',
  79. name: 'Bootstrap',
  80. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  81. },
  82. {
  83. logo: 'https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png',
  84. name: 'React',
  85. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  86. },
  87. {
  88. logo: 'https://gw.alipayobjects.com/zos/rmsportal/ComBAopevLwENQdKWiIn.png',
  89. name: 'Vue',
  90. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  91. },
  92. {
  93. logo: 'https://gw.alipayobjects.com/zos/rmsportal/nxkuOJlFJuAUhzlMTCEe.png',
  94. name: 'Webpack',
  95. desc: '在中台产品的研发过程中,会出现不同的设计规范和实现方式,但其中往往存在很多类似的页面和组件,这些类似的组件会被抽离成一套标准规范。'
  96. }
  97. ]
  98. export default [
  99. // 树形列表接口
  100. {
  101. url: '/mock/example/treeList',
  102. method: 'get',
  103. timeout,
  104. response: ({ query }) => {
  105. const { title, pageIndex, pageSize } = query
  106. const mockList = treeList.filter((item) => {
  107. if (title && item.title.indexOf(title) < 0) return false
  108. return true
  109. })
  110. const pageList = mockList.filter(
  111. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  112. )
  113. return {
  114. code: SUCCESS_CODE,
  115. data: {
  116. total: mockList.length,
  117. list: pageList
  118. }
  119. }
  120. }
  121. },
  122. // 列表接口
  123. {
  124. url: '/mock/example/list',
  125. method: 'get',
  126. timeout,
  127. response: ({ query }) => {
  128. const { title, pageIndex, pageSize } = query
  129. const mockList = List.filter((item) => {
  130. if (title && item.title.indexOf(title) < 0) return false
  131. return true
  132. })
  133. const pageList = mockList.filter(
  134. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  135. )
  136. return {
  137. code: SUCCESS_CODE,
  138. data: {
  139. total: mockList.length,
  140. list: pageList
  141. }
  142. }
  143. }
  144. },
  145. // 保存接口
  146. {
  147. url: '/mock/example/save',
  148. method: 'post',
  149. timeout,
  150. response: ({ body }) => {
  151. if (!body.id) {
  152. List = [
  153. Object.assign(body, {
  154. id: toAnyString()
  155. })
  156. ].concat(List)
  157. return {
  158. code: SUCCESS_CODE,
  159. data: 'success'
  160. }
  161. } else {
  162. List.map((item) => {
  163. if (item.id === body.id) {
  164. for (const key in item) {
  165. item[key] = body[key]
  166. }
  167. }
  168. })
  169. return {
  170. code: SUCCESS_CODE,
  171. data: 'success'
  172. }
  173. }
  174. }
  175. },
  176. // 详情接口
  177. {
  178. url: '/mock/example/detail',
  179. method: 'get',
  180. response: ({ query }) => {
  181. const { id } = query
  182. for (const example of List) {
  183. if (example.id === id) {
  184. return {
  185. code: SUCCESS_CODE,
  186. data: example
  187. }
  188. }
  189. }
  190. }
  191. },
  192. // 删除接口
  193. {
  194. url: '/mock/example/delete',
  195. method: 'post',
  196. response: ({ body }) => {
  197. const ids = body.ids
  198. if (!ids) {
  199. return {
  200. code: 500,
  201. message: '请选择需要删除的数据'
  202. }
  203. } else {
  204. let i = List.length
  205. while (i--) {
  206. if (ids.indexOf(List[i].id) !== -1) {
  207. List.splice(i, 1)
  208. }
  209. }
  210. return {
  211. code: SUCCESS_CODE,
  212. data: 'success'
  213. }
  214. }
  215. }
  216. },
  217. {
  218. url: '/mock/card/list',
  219. method: 'get',
  220. timeout,
  221. response: ({ query }) => {
  222. const { name, pageIndex, pageSize } = query
  223. const mockList = cardList.filter((item) => {
  224. if (name && item.name.indexOf(name) < 0) return false
  225. return true
  226. })
  227. const pageList = mockList.filter(
  228. (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
  229. )
  230. return {
  231. code: SUCCESS_CODE,
  232. data: {
  233. total: mockList.length,
  234. list: pageList
  235. }
  236. }
  237. }
  238. }
  239. ]