initial commit
This commit is contained in:
115
mock/user.ts
Normal file
115
mock/user.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { Request, Response } from 'express';
|
||||
|
||||
const genUserList = (current: number, pageSize: number) => {
|
||||
const tableListDataSource: any[] = [];
|
||||
|
||||
for (let i = 0; i < pageSize; i += 1) {
|
||||
const index = (current - 1) * 10 + i;
|
||||
tableListDataSource.push({
|
||||
id: `${index}`,
|
||||
username: `user_${index}`,
|
||||
realName: `用户 ${index}`,
|
||||
mobile: `138001380${index.toString().padStart(2, '0')}`,
|
||||
email: `user_${index}@antgravity.com`,
|
||||
role: index % 2 === 0 ? 'admin' : 'user',
|
||||
status: ['active', 'disabled', 'pending'][index % 3],
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
return tableListDataSource;
|
||||
};
|
||||
|
||||
let tableListDataSource = genUserList(1, 20);
|
||||
|
||||
function getUserList(req: Request, res: Response, u: string) {
|
||||
let realUrl = u;
|
||||
if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
|
||||
realUrl = req.url;
|
||||
}
|
||||
const { current = 1, pageSize = 10, username, realName, role, status } = req.query;
|
||||
|
||||
let dataSource = [...tableListDataSource];
|
||||
|
||||
if (username) {
|
||||
dataSource = dataSource.filter((item) => item.username.includes(username as string));
|
||||
}
|
||||
if (realName) {
|
||||
dataSource = dataSource.filter((item) => item.realName.includes(realName as string));
|
||||
}
|
||||
if (role) {
|
||||
dataSource = dataSource.filter((item) => item.role === role);
|
||||
}
|
||||
if (status) {
|
||||
dataSource = dataSource.filter((item) => item.status === status);
|
||||
}
|
||||
|
||||
const result = {
|
||||
data: dataSource,
|
||||
total: dataSource.length,
|
||||
success: true,
|
||||
pageSize,
|
||||
current: parseInt(`${current}`, 10) || 1,
|
||||
};
|
||||
|
||||
return res.json(result);
|
||||
}
|
||||
|
||||
function postUser(req: Request, res: Response, u: string, b: Request) {
|
||||
const body = (b && b.body) || req.body;
|
||||
const { method, id } = req;
|
||||
|
||||
switch (method) {
|
||||
case 'POST':
|
||||
const i = Math.ceil(Math.random() * 10000);
|
||||
const newUser = {
|
||||
id: `${i}`,
|
||||
...body,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
tableListDataSource.unshift(newUser);
|
||||
return res.json(newUser);
|
||||
|
||||
case 'PUT':
|
||||
// The id in URL is usually available as req.params.id for Express routes,
|
||||
// but Umi mock matches implementation might vary.
|
||||
// Assuming RESTful style: /api/users/:id
|
||||
// We need to parse ID from URL if not provided directly.
|
||||
// Simplification for mock: assume ID is passed or parsed.
|
||||
let updateId = id;
|
||||
if (!updateId) {
|
||||
// rough parsing for /api/users/123
|
||||
const parts = req.url.split('/');
|
||||
updateId = parts[parts.length - 1];
|
||||
}
|
||||
|
||||
tableListDataSource = tableListDataSource.map((item) => {
|
||||
if (item.id === updateId) {
|
||||
return { ...item, ...body, updatedAt: new Date().toISOString() };
|
||||
}
|
||||
return item;
|
||||
});
|
||||
return res.json({ id: updateId, ...body });
|
||||
|
||||
case 'DELETE':
|
||||
let deleteId = id;
|
||||
if (!deleteId) {
|
||||
const parts = req.url.split('/');
|
||||
deleteId = parts[parts.length - 1];
|
||||
}
|
||||
tableListDataSource = tableListDataSource.filter((item) => item.id !== deleteId);
|
||||
return res.json({ success: true });
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return res.json({ result: 'Error' });
|
||||
}
|
||||
|
||||
export default {
|
||||
'GET /api/users': getUserList,
|
||||
'POST /api/users': postUser,
|
||||
'PUT /api/users/:id': postUser,
|
||||
'DELETE /api/users/:id': postUser,
|
||||
};
|
||||
Reference in New Issue
Block a user