blob: ec575370b81b644a21e1f375a19005f3db374a21 [file] [log] [blame]
import Mock from 'mockjs'
import { param2Obj } from '@/utils'
const schoolyearList = Mock.mock({
'list|26': [{
'id': '@increment',
'code|1': ['2016', '2017', '2018'],
'name': '@code@cword(6, 15)'
}]
}).list
export default{
getList: config => {
const { code, name, pageIndex = 1, pageSize = 20 } = param2Obj(config.url)
const mockList = schoolyearList.filter(item => {
if (code && item.code !== code) return false
if (name && item.name !== name) return false
return true
})
const pageList = mockList.filter((item, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1))
return {
items: pageList,
recordCount: mockList.length,
code: 200
}
},
getItem: config => {
const { id } = param2Obj(config.url)
const mockList = schoolyearList.filter(item => item.id + '' === id + '')
return {
data: mockList.length > 0 ? mockList[0] : null,
code: 200
}
},
createData: config => {
const schoolyear = JSON.parse(config.body)
if (!schoolyear.id) {
schoolyear.id = '' + parseInt(Math.random() * 100) + 1024 // mock a id
schoolyearList.unshift(schoolyear)
} else {
for (let i = 0; i < schoolyearList.length; i++) {
if (schoolyearList[i].id + '' === schoolyear.id + '') {
schoolyearList.splice(i, 1, schoolyear)
break
}
}
}
return {
item: schoolyear,
code: 200
}
},
deleteData: config => {
const schoolyear = JSON.parse(config.body)
let index = -1
for (let i = 0; i < schoolyearList.length; i++) {
if (schoolyearList[i].id + '' === schoolyear.id + '') {
index = i
break
}
}
if (index > -1) {
schoolyearList.splice(index, 1)
}
return {
code: 200
}
}
}