-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path.roadhogrc.mock.js
123 lines (115 loc) · 2.63 KB
/
.roadhogrc.mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import _ from 'lodash';
let idIncrease;
const customerStructure = {
id: '',
phone: '',
name: '',
size: 0,
};
const customerList = [];
for (idIncrease = 1; idIncrease < 6; idIncrease++) {
const customer = Object.assign({}, customerStructure, {
id: idIncrease,
});
customer.size = parseInt(Math.random() * 8, 10);
if (idIncrease % 2) {
customer.phone = 13800138000 + idIncrease;
}
else {
customer.name = `customer_${idIncrease}`;
}
customerList.push(customer);
}
export default {
'GET /api': { ok: true },
// 支持值为 Object 和 Array
'GET /api/customer': {
data: {
data: customerList,
total: customerList.length,
page: 1,
pageSize: customerList.length,
},
},
// GET
'GET /api/customer/:id': (req, res) => {
if (req.params && req.params.id && 1 * req.params.id && _.find(customerList, {
id: 1 * req.params.id,
})) {
res.send({
data: _.find(customerList, {
id: 1 * req.params.id,
}),
});
res.end("OK");
}
else {
res.status(404).end();
}
},
// PUT
'DELETE /api/customer/:id': (req, res) => {
if (req.params && req.params.id && 1 * req.params.id && _.find(customerList, {
id: 1 * req.params.id,
})) {
const customer = _.remove(customerList, {
id: 1 * req.params.id,
});
res.send({
data: customer,
ok: true,
});
res.end("OK");
}
else {
res.status(404).end();
}
},
// PUT
'PUT /api/customer/:id': (req, res) => {
if (req.params && req.params.id && 1 * req.params.id && _.find(customerList, {
id: 1 * req.params.id,
})) {
const customer = _.find(customerList, {
id: 1 * req.params.id,
});
for (const [k] of Object.entries(customerStructure)) {
if (k in req.body) {
customer[k] = req.body[k];
}
}
res.send({
data: customer,
ok: true,
});
res.end("OK");
}
else {
res.status(404).end();
}
},
// 新建
'POST /api/customer': (req, res) => {
console.log(req.body);
const body = req.body || {};
const name = _.get(body, 'name') || '';
const phone = _.get(body, 'phone') || '';
const size = _.get(body, 'size') || '';
const sizeInfo = _.get(body, 'sizeInfo') || '';
const customer = Object.assign({}, customerStructure, {
id: idIncrease,
name,
phone,
size,
sizeInfo,
});
customerList.push(customer);
idIncrease ++;
res.send({
ok: true,
length: customerList.length,
data: customer,
});
res.end('OK');
},
};