huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 1 | import Mock from 'mockjs' |
| 2 | import { param2Obj } from '@/utils' |
| 3 | |
| 4 | const schoolyearList = Mock.mock({ |
| 5 | 'list|26': [{ |
| 6 | 'id': '@increment', |
huibing.xie | c95b6a2 | 2018-08-29 14:15:43 +0800 | [diff] [blame^] | 7 | 'code|1': ['2016', '2017', '2018'], |
| 8 | 'name': '@code@cword(6, 15)' |
huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 9 | }] |
| 10 | }).list |
| 11 | |
| 12 | export default{ |
| 13 | getList: config => { |
huibing.xie | c95b6a2 | 2018-08-29 14:15:43 +0800 | [diff] [blame^] | 14 | const { code, name, pageIndex = 1, pageSize = 20 } = param2Obj(config.url) |
huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 15 | const mockList = schoolyearList.filter(item => { |
huibing.xie | c95b6a2 | 2018-08-29 14:15:43 +0800 | [diff] [blame^] | 16 | if (code && item.code !== code) return false |
| 17 | if (name && item.name !== name) return false |
huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame] | 18 | 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 | } |