listTableList.ts 4.8 KB

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