listTableList.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // @ts-ignore
  40. let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
  41. if (params.sorter) {
  42. const s = params.sorter.split('_');
  43. dataSource = dataSource.sort((prev, next) => {
  44. if (s[1] === 'descend') {
  45. return next[s[0]] - prev[s[0]];
  46. }
  47. return prev[s[0]] - next[s[0]];
  48. });
  49. }
  50. if (params.status) {
  51. const status = params.status.split(',');
  52. let filterDataSource: TableListItem[] = [];
  53. status.forEach((s: string) => {
  54. filterDataSource = filterDataSource.concat(
  55. dataSource.filter((item) => {
  56. if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
  57. return true;
  58. }
  59. return false;
  60. }),
  61. );
  62. });
  63. dataSource = filterDataSource;
  64. }
  65. if (params.name) {
  66. dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  67. }
  68. const result = {
  69. data: dataSource,
  70. total: tableListDataSource.length,
  71. success: true,
  72. pageSize,
  73. current: parseInt(`${params.currentPage}`, 10) || 1,
  74. };
  75. return res.json(result);
  76. }
  77. function postRule(req: Request, res: Response, u: string, b: Request) {
  78. let realUrl = u;
  79. if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
  80. realUrl = req.url;
  81. }
  82. const body = (b && b.body) || req.body;
  83. const { method, name, desc, key } = body;
  84. switch (method) {
  85. /* eslint no-case-declarations:0 */
  86. case 'delete':
  87. tableListDataSource = tableListDataSource.filter((item) => key.indexOf(item.key) === -1);
  88. break;
  89. case 'post':
  90. (() => {
  91. const i = Math.ceil(Math.random() * 10000);
  92. const newRule = {
  93. key: tableListDataSource.length,
  94. href: 'https://ant.design',
  95. avatar: [
  96. 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  97. 'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
  98. ][i % 2],
  99. name,
  100. owner: '曲丽丽',
  101. desc,
  102. callNo: Math.floor(Math.random() * 1000),
  103. status: Math.floor(Math.random() * 10) % 2,
  104. updatedAt: new Date(),
  105. createdAt: new Date(),
  106. progress: Math.ceil(Math.random() * 100),
  107. };
  108. tableListDataSource.unshift(newRule);
  109. return res.json(newRule);
  110. })();
  111. return;
  112. case 'update':
  113. (() => {
  114. let newRule = {};
  115. tableListDataSource = tableListDataSource.map((item) => {
  116. if (item.key === key) {
  117. newRule = { ...item, desc, name };
  118. return { ...item, desc, name };
  119. }
  120. return item;
  121. });
  122. return res.json(newRule);
  123. })();
  124. return;
  125. default:
  126. break;
  127. }
  128. const result = {
  129. list: tableListDataSource,
  130. pagination: {
  131. total: tableListDataSource.length,
  132. },
  133. };
  134. res.json(result);
  135. }
  136. export default {
  137. 'GET /api/rule': getRule,
  138. 'POST /api/rule': postRule,
  139. };