blob: ec575370b81b644a21e1f375a19005f3db374a21 [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001import Mock from 'mockjs'
2import { param2Obj } from '@/utils'
3
4const schoolyearList = Mock.mock({
5 'list|26': [{
6 'id': '@increment',
huibing.xiec95b6a22018-08-29 14:15:43 +08007 'code|1': ['2016', '2017', '2018'],
8 'name': '@code@cword(6, 15)'
huibing.xie1f1606f2018-08-20 15:46:55 +08009 }]
10}).list
11
12export default{
13 getList: config => {
huibing.xiec95b6a22018-08-29 14:15:43 +080014 const { code, name, pageIndex = 1, pageSize = 20 } = param2Obj(config.url)
huibing.xie1f1606f2018-08-20 15:46:55 +080015 const mockList = schoolyearList.filter(item => {
huibing.xiec95b6a22018-08-29 14:15:43 +080016 if (code && item.code !== code) return false
17 if (name && item.name !== name) return false
huibing.xie1f1606f2018-08-20 15:46:55 +080018 return true
19 })
20
21 const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1))
22 return {
23 items: pageList,
24 recordCount: mockList.length,
25 code: 200
26 }
27 },
28 getItem: config => {
29 const { id } = param2Obj(config.url)
30 const mockList = schoolyearList.filter(item => item.id + '' === id + '')
31 return {
32 data: mockList.length > 0 ? mockList[0] : null,
33 code: 200
34 }
35 },
36 createData: config => {
37 const schoolyear = JSON.parse(config.body)
38 if (!schoolyear.id) {
39 schoolyear.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id
40 schoolyearList.unshift(schoolyear)
41 } else {
42 for (let i = 0; i < schoolyearList.length; i++) {
43 if (schoolyearList[i].id + '' === schoolyear.id + '') {
44 schoolyearList.splice(i, 1, schoolyear)
45 break
46 }
47 }
48 }
49 return {
50 item: schoolyear,
51 code: 200
52 }
53 },
54 deleteData: config => {
55 const schoolyear = JSON.parse(config.body)
56 let index = -1
57 for (let i = 0; i < schoolyearList.length; i++) {
58 if (schoolyearList[i].id + '' === schoolyear.id + '') {
59 index = i
60 break
61 }
62 }
63 if (index > -1) {
64 schoolyearList.splice(index, 1)
65 }
66 return {
67 code: 200
68 }
69 }
70}