blacklist.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { defineMock } from '@umijs/max';
  2. const blacklistDataSource: any[] = [
  3. {
  4. id: '1',
  5. name: 'Example Company A',
  6. creditCode: '91110108551111111A',
  7. createdAt: '2023-01-01',
  8. },
  9. {
  10. id: '2',
  11. name: 'Example Company B',
  12. creditCode: '91110108551111111B',
  13. createdAt: '2023-02-15',
  14. },
  15. ];
  16. export default defineMock({
  17. 'POST /api/login': (req, res) => {
  18. const { username, password } = req.body || {};
  19. if (username && password) {
  20. res.json({
  21. token: 'mock-token-' + Date.now(),
  22. username,
  23. });
  24. } else {
  25. res.status(400).json({ message: '用户名或密码错误' });
  26. }
  27. },
  28. 'GET /api/blacklist': (req, res) => {
  29. const { current = 1, pageSize = 10, name, creditCode } = req.query;
  30. let dataSource = [...blacklistDataSource];
  31. if (name) {
  32. dataSource = dataSource.filter(item => item.name.includes(name));
  33. }
  34. if (creditCode) {
  35. dataSource = dataSource.filter(item => item.creditCode.includes(creditCode));
  36. }
  37. const total = dataSource.length;
  38. // Simple pagination
  39. const startIndex = (Number(current) - 1) * Number(pageSize);
  40. const endIndex = startIndex + Number(pageSize);
  41. const list = dataSource.slice(startIndex, endIndex);
  42. res.json({
  43. data: list,
  44. total,
  45. success: true,
  46. });
  47. },
  48. 'POST /api/blacklist': (req, res) => {
  49. const newEntry = {
  50. id: Date.now().toString(),
  51. createdAt: new Date().toISOString().split('T')[0],
  52. ...req.body,
  53. };
  54. blacklistDataSource.unshift(newEntry);
  55. res.json({ success: true });
  56. },
  57. 'PUT /api/blacklist/:id': (req, res) => {
  58. const { id } = req.params;
  59. const index = blacklistDataSource.findIndex((item) => item.id === id);
  60. if (index > -1) {
  61. blacklistDataSource[index] = { ...blacklistDataSource[index], ...req.body };
  62. }
  63. res.json({ success: true });
  64. },
  65. 'DELETE /api/blacklist/:id': (req, res) => {
  66. const { id } = req.params;
  67. const index = blacklistDataSource.findIndex((item) => item.id === id);
  68. if (index > -1) {
  69. blacklistDataSource.splice(index, 1);
  70. }
  71. res.json({ success: true });
  72. },
  73. });