huibing.xie | 41e0d91 | 2018-08-28 14:26:11 +0800 | [diff] [blame] | 1 | import Mock from 'mockjs' |
| 2 | import { param2Obj, parseTime } from '@/utils' |
| 3 | |
| 4 | const userList = Mock.mock({ |
| 5 | 'list|26': [{ |
| 6 | 'id': '@increment', |
| 7 | 'rownum': '@id', |
| 8 | 'ghxh': '@id', |
| 9 | 'xm': '000@id', |
| 10 | 'zhlb|1': ['教职工', '学生', '其他'], |
huibing.xie | 8442517 | 2018-08-28 19:41:13 +0800 | [diff] [blame^] | 11 | 'sex|1': ['男', '女'], |
huibing.xie | 41e0d91 | 2018-08-28 14:26:11 +0800 | [diff] [blame] | 12 | 'zt|1': ['启用', '停用'], |
| 13 | 'yx': '@cword(3, 5)', |
| 14 | 'xybm': '@word(6, 10)', |
| 15 | 'gzdw': '@word(6, 10)', |
| 16 | 'sj': '@cword(3, 5)', |
| 17 | 'wx': '@cword(3, 5)', |
| 18 | 'qq': '@cword(3, 5)' |
| 19 | }] |
| 20 | }).list |
| 21 | |
| 22 | const zhlbList = Mock.mock( |
| 23 | [{ |
| 24 | 'value': 'teacher', |
| 25 | 'label': '教职工' |
| 26 | }, |
| 27 | { |
| 28 | 'value': 'student', |
| 29 | 'label': '学生' |
| 30 | }, |
| 31 | { |
| 32 | 'value': 'other', |
| 33 | 'label': '其他' |
| 34 | }] |
| 35 | ) |
| 36 | |
huibing.xie | 8442517 | 2018-08-28 19:41:13 +0800 | [diff] [blame^] | 37 | const sexList = Mock.mock( |
huibing.xie | 41e0d91 | 2018-08-28 14:26:11 +0800 | [diff] [blame] | 38 | [{ |
| 39 | 'value': 'male', |
| 40 | 'label': '男' |
| 41 | }, |
| 42 | { |
| 43 | 'value': 'female', |
| 44 | 'label': '女' |
| 45 | }] |
| 46 | ) |
| 47 | const ztList = Mock.mock( |
| 48 | [{ |
| 49 | 'value': 'on', |
| 50 | 'label': '启用' |
| 51 | }, |
| 52 | { |
| 53 | 'value': 'off', |
| 54 | 'label': '停用' |
| 55 | }] |
| 56 | ) |
| 57 | export default{ |
| 58 | getPage: config => { |
| 59 | const { zhlb, ghxhxm, pageIndex = 1, pageSize = 20 } = param2Obj(config.url) |
| 60 | const mockList = userList.filter(item => { |
| 61 | if (zhlb && item.zhlb !== zhlb) return false |
| 62 | if (ghxhxm && item.ghxh + '' !== ghxhxm + '' && item.xm + '' !== ghxhxm + '') return false |
| 63 | return true |
| 64 | }) |
| 65 | |
| 66 | const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)) |
| 67 | return { |
huibing.xie | 8442517 | 2018-08-28 19:41:13 +0800 | [diff] [blame^] | 68 | sexList: sexList, |
huibing.xie | 41e0d91 | 2018-08-28 14:26:11 +0800 | [diff] [blame] | 69 | ztList: ztList, |
| 70 | zhlbList: zhlbList, |
| 71 | items: pageList, |
| 72 | recordCount: mockList.length, |
| 73 | code: 200 |
| 74 | } |
| 75 | }, |
| 76 | getItem: config => { |
| 77 | const { id } = param2Obj(config.url) |
| 78 | const mockList = userList.filter(item => item.id + '' === id + '') |
| 79 | return { |
| 80 | data: mockList.length > 0 ? mockList[0] : null, |
| 81 | code: 200 |
| 82 | } |
| 83 | }, |
| 84 | createData: config => { |
| 85 | const user = JSON.parse(config.body) |
| 86 | user.cjsj = parseTime(new Date(), '{y}-{m}-{d}') |
| 87 | if (!user.id) { |
| 88 | user.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id |
| 89 | userList.unshift(user) |
| 90 | } else { |
| 91 | for (let i = 0; i < userList.length; i++) { |
| 92 | if (userList[i].id + '' === user.id + '') { |
| 93 | userList.splice(i, 1, user) |
| 94 | break |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return { |
| 99 | item: user, |
| 100 | code: 200 |
| 101 | } |
| 102 | }, |
| 103 | deleteData: config => { |
| 104 | const user = JSON.parse(config.body) |
| 105 | let index = -1 |
| 106 | for (let i = 0; i < userList.length; i++) { |
| 107 | if (userList[i].id + '' === user.id + '') { |
| 108 | index = i |
| 109 | break |
| 110 | } |
| 111 | } |
| 112 | if (index > -1) { |
| 113 | userList.splice(index, 1) |
| 114 | } |
| 115 | return { |
| 116 | code: 200 |
| 117 | } |
| 118 | } |
| 119 | } |