blob: 2901ddc3fc1818140a1e7f963207194284619771 [file] [log] [blame]
huibing.xie41e0d912018-08-28 14:26:11 +08001import Mock from 'mockjs'
2import { param2Obj, parseTime } from '@/utils'
3
4const userList = Mock.mock({
5 'list|26': [{
6 'id': '@increment',
7 'rownum': '@id',
8 'ghxh': '@id',
9 'xm': '000@id',
10 'zhlb|1': ['教职工', '学生', '其他'],
huibing.xie84425172018-08-28 19:41:13 +080011 'sex|1': ['男', '女'],
huibing.xie41e0d912018-08-28 14:26:11 +080012 '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
22const 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.xie84425172018-08-28 19:41:13 +080037const sexList = Mock.mock(
huibing.xie41e0d912018-08-28 14:26:11 +080038 [{
39 'value': 'male',
40 'label': '男'
41 },
42 {
43 'value': 'female',
44 'label': '女'
45 }]
46)
47const ztList = Mock.mock(
48 [{
49 'value': 'on',
50 'label': '启用'
51 },
52 {
53 'value': 'off',
54 'label': '停用'
55 }]
56)
57export 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.xie84425172018-08-28 19:41:13 +080068 sexList: sexList,
huibing.xie41e0d912018-08-28 14:26:11 +080069 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}