Mock.js production use

Original link: https://chegva.com/5600.html

I used mock.js in the project, and it took a lot of time to create data for the first time, but overall it is quite easy to use.

 let users = ['anzhihe1', 'anzhihe2', 'atom', 'Messi', 'C.Ronaldo'] 
  
Mock.mock({ 
  
data: { 
  
list: Random.pick(users, 1, 4), // Randomly select 1-4 elements from users to form a new array list 
  
} 
  
}) 
  
 
  
Random. extend({ 
  
newUsers: users, 
  
// The custom placeholder name is 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, //special field }] 
  
}) 
  
 
  
// 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 
  
} 
  
}) 
  
 
  
// Create a new elastic group Mock.mock(RegExp('/add'), 'post', (parameter) => { 
  
let params = JSON. parse(parameter. body); 
  
let total = list.list.unshift(params) 
  
let result = params 
  
 
  
return { 
  
code: 200, 
  
message: 'Added successfully', 
  
data: result, 
  
total: total 
  
} 
  
}) 
  
 
  
// Update elastic group 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: 'Update successful', 
  
data: result 
  
} 
  
}) 
  
 
  
// Delete elastic group 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: 'Deleted successfully', 
  
data: list.list 
  
} 
  
}) 
  
 
  
// Rapid scaling of elastic groups 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: 'The rapid expansion and contraction of the elastic group succeeded', 
  
data: result 
  
} 
  
}) 
  
 
  
const hosts = [ 
  
{ 
  
"instanceId": "i-0fcd93b7dc18", //InstanceID 
  
"instanceType": "m5.large", //instance specification "launchTime": "2022-12-02T07:17:22Z", //launch time "privateIpAddress": "10.15.36.18", //private IP 
  
"state": "running" //running state }, 
  
{ 
  
"instanceId": "i-0fcdK8627dc19", //InstanceID 
  
"instanceType": "m5.large", //instance specification "launchTime": "2022-12-02T07:17:22Z", //launch time "privateIpAddress": "10.15.36.89", //private IP 
  
"state": "running" //running state }, 
  
{ 
  
"instanceId": "i-0fcd93K7dc90", //InstanceID 
  
"instanceType": "m5.large", //Instance specification "launchTime": "2022-12-02T07:17:22Z", //Launch time "privateIpAddress": "10.15.36.90", //Private IP 
  
"state": "shutdown" //running state } 
  
] 
  
 
  
// Get elastic group host list 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", //request ID 
  
data: hosts 
  
} 
  
}) 
  
 
  
// Eliminate cloud host 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: 'The instance was deleted successfully', 
  
data: hosts 
  
} 
  
})

This article is transferred from: https://chegva.com/5600.html
This site is only for collection, and the copyright belongs to the original author.