listTableList.ts 4.4 KB

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