在项目中用了把mock.js,第一次用造数据花了不少时间,不过整体来说还是挺好用的。
let users = ['anzhihe1', 'anzhihe2', 'atom', 'Messi', 'C.Ronaldo'] Mock.mock({ data: { list: Random.pick(users, 1, 4), //从users中随机选取1-4个元素组成新的数组list } }) Random.extend({ newUsers: users, // 自定义占位符名字为newUserArr newUserArr: function (date) { return this.pick(this.newUsers, 1, 4).join(','); } }) const list = Mock.mock({ 'list|20': [{ 'id|+1': 0, 'groupName': '@Groups', 'groupID': '@integer(50,100)', 'resourceName': '@string', 'lbName': '@string', 'domain': '@domain', 'treePath': '@string', 'cloudVendors|1': ['aws', 'aliyun', 'azure', 'ks'], 'region': '@region', 'account': '@word', 'targetHostNumber|0-100': 0, 'maintainer': '@newUserArr', 'remark': '@word(20)', 'createdTime': '@DATETIME', 'updateTime': '@DATETIME', "special": null, //特殊字段 }] }) // Mock.mock(RegExp(`${process.env.VUE_APP_API_BASE_URL}/elasticGroups` + '.*'),'get', ({url}) => { // Mock.mock(RegExp('.*'),'get', ({url}) => { Mock.mock(RegExp('/list'), 'get', ({ url }) => { const params = parseUrlParams(decodeURI(url)) let { page, pageSize } = params page = eval(page) - 1 || 0 pageSize = eval(pageSize) || 10 delete params.page delete params.pageSize let result = list.list.filter(item => { for (let [key, value] of Object.entries(params)) { if (item[key] != value) { return false } } return true }) const total = result.length if ((page) * pageSize > total) { result = [] } else { result = result.slice(page * pageSize, (page + 1) * pageSize) } return { code: 200, message: 'success', // data: { // page: page + 1, // pageSize, // total, // list: result // }, data: result, total: total, page: page + 1, pageSize: pageSize } }) // 新建弹性组 Mock.mock(RegExp('/add'), 'post', (parameter) => { let params = JSON.parse(parameter.body); params['id'] = parseInt(Math.random()*(100 - 21) + 21) list.list.unshift(params) let result = params return { code: 200, message: '添加成功', data: result } }) // 更新弹性组 Mock.mock(RegExp('/update'), 'put', (parameter) => { const params = JSON.parse(parameter.body); let result = params list.list = list.list.map(item => { return item.id === params.id ? params : item }) return { code: 200, message: '更新成功', data: result } }) // 删除弹性组 Mock.mock(RegExp('/delete'), 'delete', (parameter) => { const params = JSON.parse(parameter.body); const index = list.list.findIndex(item => item.id === params.id) list.list.splice(index, 1) return { code: 200, message: '删除成功', data: list.list } }) // 弹性组快速扩缩容 Mock.mock(RegExp('/scale'), 'post', (parameter) => { const params = JSON.parse(parameter.body); console.log(params.id); console.log(params); let result = list.list.map(item => { // let tmp = Object.assign({}, item) return item.id === params.id ? item.targetHostNumber = params.targetHostNumber : item }) return { code: 200, message: '弹性组快速扩缩容成功', data: result } }) const hosts = [ { "instanceId": "i-0fcd93b7dc18", //InstanceID "instanceType": "m5.large", //实例规格 "launchTime": "2022-12-02T07:17:22Z", //上线时间 "privateIpAddress": "10.15.36.18", //私有IP "state": "running" //运行状态 }, { "instanceId": "i-0fcdK8627dc19", //InstanceID "instanceType": "m5.large", //实例规格 "launchTime": "2022-12-02T07:17:22Z", //上线时间 "privateIpAddress": "10.15.36.89", //私有IP "state": "running" //运行状态 }, { "instanceId": "i-0fcd93K7dc90", //InstanceID "instanceType": "m5.large", //实例规格 "launchTime": "2022-12-02T07:17:22Z", //上线时间 "privateIpAddress": "10.15.36.90", //私有IP "state": "shutdown" //运行状态 } ] // 获取弹性组主机列表 Mock.mock(RegExp('/describe'), 'get', (parameter) => { const params = JSON.parse(parameter.body); // console.log(params.id); console.log(params); return { code: 0, message: 'success', requestId: "5a38c305-f13d-44b7-af48-9de7396213434a", //请求ID data: hosts } }) // 剔除云主机 Mock.mock(RegExp('/terminate'), 'delete', (parameter) => { const params = JSON.parse(parameter.body); console.log('params', params); let instanceId = params.instanceId for (let id of instanceId) { let index = hosts.findIndex(item => item.instanceId === id) if (index != -1) hosts.splice(index, 1) } return { code: 200, message: '删除实例成功', data: hosts } })