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 auditscopeList = Mock.mock({ |
| 5 | 'list|50': [{ |
| 6 | 'id': '@increment', |
| 7 | 'fwkzmc': '@cword(6, 15)', |
| 8 | 'fwkzsxl': '@word(15)' |
| 9 | }] |
| 10 | }).list |
| 11 | |
| 12 | export default{ |
| 13 | getList: config => { |
| 14 | const { fwkzmc, pageIndex = 1, pageSize = 20 } = param2Obj(config.url) |
| 15 | const mockList = auditscopeList.filter(item => { |
| 16 | if (fwkzmc && item.fwkzmc !== fwkzmc) return false |
| 17 | return true |
| 18 | }) |
| 19 | |
| 20 | const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)) |
| 21 | return { |
| 22 | items: pageList, |
| 23 | recordCount: mockList.length, |
| 24 | code: 200 |
| 25 | } |
| 26 | }, |
| 27 | getItem: config => { |
| 28 | const { id } = param2Obj(config.url) |
| 29 | const mockList = auditscopeList.filter(item => item.id + '' === id + '') |
| 30 | return { |
| 31 | data: mockList.length > 0 ? mockList[0] : null, |
| 32 | code: 200 |
| 33 | } |
| 34 | }, |
| 35 | createData: config => { |
| 36 | const auditscope = JSON.parse(config.body) |
| 37 | if (!auditscope.id) { |
| 38 | auditscope.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id |
| 39 | auditscopeList.unshift(auditscope) |
| 40 | } else { |
| 41 | for (let i = 0; i < auditscopeList.length; i++) { |
| 42 | if (auditscopeList[i].id + '' === auditscope.id + '') { |
| 43 | auditscopeList.splice(i, 1, auditscope) |
| 44 | break |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return { |
| 49 | item: auditscope, |
| 50 | code: 200 |
| 51 | } |
| 52 | }, |
| 53 | deleteData: config => { |
| 54 | const auditscope = JSON.parse(config.body) |
| 55 | let index = -1 |
| 56 | for (let i = 0; i < auditscopeList.length; i++) { |
| 57 | if (auditscopeList[i].id + '' === auditscope.id + '') { |
| 58 | index = i |
| 59 | break |
| 60 | } |
| 61 | } |
| 62 | if (index > -1) { |
| 63 | auditscopeList.splice(index, 1) |
| 64 | } |
| 65 | return { |
| 66 | code: 200 |
| 67 | } |
| 68 | } |
| 69 | } |