listTableList.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // eslint-disable-next-line import/no-extraneous-dependencies
  2. import { Request, Response } from 'express';
  3. import { parse } from 'url';
  4. import { TableListItem, TableListParams } from '@/pages/ListTableList/data';
  5. // mock tableListDataSource
  6. const genList = (current: number, pageSize: number) => {
  7. const tableListDataSource: TableListItem[] = [];
  8. for (let i = 0; i < pageSize; i += 1) {
  9. const index = (current - 1) * 10 + i;
  10. tableListDataSource.push({
  11. key: index,
  12. disabled: i % 6 === 0,
  13. href: 'https://ant.design',
  14. avatar: [
  15. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  16. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  17. ][i % 2],
  18. name: `TradeCode ${index}`,
  19. owner: '曲丽丽',
  20. desc: '这是一段描述',
  21. callNo: Math.floor(Math.random() * 1000),
  22. status: Math.floor(Math.random() * 10) % 4,
  23. updatedAt: new Date(),
  24. createdAt: new Date(),
  25. progress: Math.ceil(Math.random() * 100),
  26. });
  27. }
  28. tableListDataSource.reverse();
  29. return tableListDataSource;
  30. };
  31. let tableListDataSource = genList(1, 100);
  32. function getRule(req: Request, res: Response, u: string) {
  33. let realUrl = u;
  34. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  35. realUrl = req.url;
  36. }
  37. const { current = 1, pageSize = 10 } = req.query;
  38. const params = (parse(realUrl, true).query as unknown) as TableListParams;
  39. let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
  40. if (params.sorter) {
  41. const s = params.sorter.split('_');
  42. dataSource = dataSource.sort((prev, next) => {
  43. if (s[1] === 'descend') {
  44. return next[s[0]] - prev[s[0]];
  45. }
  46. return prev[s[0]] - next[s[0]];
  47. });
  48. }
  49. if (params.status) {
  50. const status = params.status.split(',');
  51. let filterDataSource: TableListItem[] = [];
  52. status.forEach((s: string) => {
  53. filterDataSource = filterDataSource.concat(
  54. dataSource.filter((item) => {
  55. if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
  56. return true;
  57. }
  58. return false;
  59. }),
  60. );
  61. });
  62. dataSource = filterDataSource;
  63. }
  64. if (params.name) {
  65. dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  66. }
  67. const result = {
  68. data: dataSource,
  69. total: tableListDataSource.length,
  70. success: true,
  71. pageSize,
  72. current: parseInt(`${params.currentPage}`, 10) || 1,
  73. };
  74. return res.json(result);
  75. }
  76. function postRule(req: Request, res: Response, u: string, b: Request) {
  77. let realUrl = u;
  78. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  79. realUrl = req.url;
  80. }
  81. const body = (b && b.body) || req.body;
  82. const { method, name, desc, key } = body;
  83. switch (method) {
  84. /* eslint no-case-declarations:0 */
  85. case 'delete':
  86. tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1);
  87. break;
  88. case 'post':
  89. (() => {
  90. const i = Math.ceil(Math.random() * 10000);
  91. const newRule = {
  92. key: tableListDataSource.length,
  93. href: 'https://ant.design',
  94. avatar: [
  95. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  96. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  97. ][i % 2],
  98. name,
  99. owner: '曲丽丽',
  100. desc,
  101. callNo: Math.floor(Math.random() * 1000),
  102. status: Math.floor(Math.random() * 10) % 2,
  103. updatedAt: new Date(),
  104. createdAt: new Date(),
  105. progress: Math.ceil(Math.random() * 100),
  106. };
  107. tableListDataSource.unshift(newRule);
  108. return res.json(newRule);
  109. })();
  110. return;
  111. case 'update':
  112. (() => {
  113. let newRule = {};
  114. tableListDataSource = tableListDataSource.map((item) => {
  115. if (item.key === key) {
  116. newRule = { ...item, desc, name };
  117. return { ...item, desc, name };
  118. }
  119. return item;
  120. });
  121. return res.json(newRule);
  122. })();
  123. return;
  124. default:
  125. break;
  126. }
  127. const result = {
  128. list: tableListDataSource,
  129. pagination: {
  130. total: tableListDataSource.length,
  131. },
  132. };
  133. res.json(result);
  134. }
  135. export default {
  136. 'GET /api/rule': getRule,
  137. 'POST /api/rule': postRule,
  138. };