huibing.xie | 1f1606f | 2018-08-20 15:46:55 +0800 | [diff] [blame^] | 1 | import Mock from 'mockjs' |
| 2 | import { param2Obj, parseTime } from '@/utils' |
| 3 | |
| 4 | const newspublishList = Mock.mock({ |
| 5 | 'list|26': [{ |
| 6 | 'id': '@increment', |
| 7 | 'ggbt': '@cword(6, 15)', |
| 8 | 'gglx': '离校公告', |
| 9 | 'jszlx|1': ['所有学生', '所有人', '所有教职工'], |
| 10 | 'sfky|1': ['可用', '不可用'], |
| 11 | 'fbqssj': '2018-06-28', |
| 12 | 'fbjssj': '2018-06-28', |
| 13 | 'xswz|1': ['其它', '置顶'], |
| 14 | 'jlckzt': '需要', |
| 15 | 'djs': '@integer(60, 100)', |
| 16 | 'fbr': 'admin', |
| 17 | 'fbrq': '2018-08-08' |
| 18 | }] |
| 19 | }).list |
| 20 | |
| 21 | export default{ |
| 22 | getList: config => { |
| 23 | const { ggbt, fbqssj, fbjssj, sfky, xswz, jlckzt, pageIndex = 1, pageSize = 20 } = param2Obj(config.url) |
| 24 | const mockList = newspublishList.filter(item => { |
| 25 | if (ggbt && item.ggbt !== ggbt) return false |
| 26 | if (fbqssj && item.fbqssj !== fbqssj) return false |
| 27 | if (fbjssj && item.fbjssj !== fbjssj) return false |
| 28 | if (sfky && item.sfky !== sfky) return false |
| 29 | if (xswz && item.xswz !== xswz) return false |
| 30 | if (jlckzt && item.jlckzt !== jlckzt) return false |
| 31 | return true |
| 32 | }) |
| 33 | |
| 34 | const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)) |
| 35 | return { |
| 36 | items: pageList, |
| 37 | recordCount: mockList.length, |
| 38 | code: 200 |
| 39 | } |
| 40 | }, |
| 41 | getItem: config => { |
| 42 | const { id } = param2Obj(config.url) |
| 43 | const mockList = newspublishList.filter(item => item.id + '' === id + '') |
| 44 | return { |
| 45 | data: mockList.length > 0 ? mockList[0] : null, |
| 46 | code: 200 |
| 47 | } |
| 48 | }, |
| 49 | createData: config => { |
| 50 | const newspublish = JSON.parse(config.body) |
| 51 | newspublish.cjsj = parseTime(new Date(), '{y}-{m}-{d}') |
| 52 | if (!newspublish.id) { |
| 53 | newspublish.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id |
| 54 | newspublishList.unshift(newspublish) |
| 55 | } else { |
| 56 | for (let i = 0; i < newspublishList.length; i++) { |
| 57 | if (newspublishList[i].id + '' === newspublish.id + '') { |
| 58 | newspublishList.splice(i, 1, newspublish) |
| 59 | break |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return { |
| 64 | item: newspublish, |
| 65 | code: 200 |
| 66 | } |
| 67 | }, |
| 68 | deleteData: config => { |
| 69 | const newspublish = JSON.parse(config.body) |
| 70 | let index = -1 |
| 71 | for (let i = 0; i < newspublishList.length; i++) { |
| 72 | if (newspublishList[i].id + '' === newspublish.id + '') { |
| 73 | index = i |
| 74 | break |
| 75 | } |
| 76 | } |
| 77 | if (index > -1) { |
| 78 | newspublishList.splice(index, 1) |
| 79 | } |
| 80 | return { |
| 81 | code: 200 |
| 82 | } |
| 83 | } |
| 84 | } |