blob: ce07f0b6e9fe02756e3c41d5cdd9325999eb91e9 [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001import Mock from 'mockjs'
2import { param2Obj, parseTime } from '@/utils'
3
4const deptList = Mock.mock({
5 'list|26': [{
6 'id': '@increment',
7 'dwdm': '000@id',
8 'dwmc': '@cword(6, 15)',
9 'dwjc': '@cword(3, 5)',
10 'dwywmc': '@word(6, 10)',
11 'sfqy|1': ['启用', '未启用'],
12 'sfqycode|1': ['1', '0'],
13 'lbm|1': ['院系', '部门'],
14 'lbmid|1': ['1', '2'],
15 'cjsj': '@Date'
16 }]
17}).list
18
19export default{
20 getList: config => {
21 const { dwdm, dwmc, sfqy, lbm, pageIndex = 1, pageSize = 20 } = param2Obj(config.url)
22 const mockList = deptList.filter(item => {
23 if (dwdm && item.dwdm !== dwdm) return false
24 if (dwmc && item.dwmc !== dwmc) return false
25 if (sfqy && item.sfqycode !== sfqy) return false
26 if (lbm && item.lbmid !== lbm) return false
27 return true
28 })
29
30 const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1))
31 return {
32 items: pageList,
33 recordCount: mockList.length,
34 code: 200
35 }
36 },
37 getAllList: config => {
38 return {
39 items: deptList,
40 code: 200
41 }
42 },
43 getItem: config => {
44 const { id } = param2Obj(config.url)
45 const mockList = deptList.filter(item => item.id + '' === id + '')
46 return {
47 data: mockList.length > 0 ? mockList[0] : null,
48 code: 200
49 }
50 },
51 createData: config => {
52 const dept = JSON.parse(config.body)
53 dept.cjsj = parseTime(new Date(), '{y}-{m}-{d}')
54 if (!dept.id) {
55 dept.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id
56 deptList.unshift(dept)
57 } else {
58 for (let i = 0; i < deptList.length; i++) {
59 if (deptList[i].id + '' === dept.id + '') {
60 deptList.splice(i, 1, dept)
61 break
62 }
63 }
64 }
65 return {
66 item: dept,
67 code: 200
68 }
69 },
70 deleteData: config => {
71 const dept = JSON.parse(config.body)
72 let index = -1
73 for (let i = 0; i < deptList.length; i++) {
74 if (deptList[i].id + '' === dept.id + '') {
75 index = i
76 break
77 }
78 }
79 if (index > -1) {
80 deptList.splice(index, 1)
81 }
82 return {
83 code: 200
84 }
85 }
86}